Advertisement
Guest User

autobackupmysql-1.4.sh

a guest
Feb 2nd, 2011
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 22.97 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Automatic MySQL Backup Script
  4. # VER. 1.4 - http://sourceforge.net/projects/autobackupmysql/
  5. # Copyright (c) 2009 infyquest@gmail.com
  6. # Copyright (c) 2002-2006 wipe_out@lycos.co.uk
  7. #
  8. # This is a clone of AutoMySQLBack V2.5 Script from wipeout
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. #
  24. #=====================================================================
  25. #=====================================================================
  26. # Set the following variables to your system needs
  27. # (Detailed instructions below variables)
  28. #=====================================================================
  29.  
  30. # Check configuration iles
  31. if [ -f "autobackupmysql.conf" ] ; then
  32.     source "autobackupmysql.conf"
  33. elif [ -f "/etc/autobackupmysql.conf" ] ; then
  34.     source "/etc/autobackupmysql.conf"
  35. elif [ -f "/usr/local/etc/autobackupmysql.conf" ] ; then
  36.     source "/usr/local/etc/autobackupmysql.conf"
  37. else
  38. # Username to access the MySQL server e.g. dbuser
  39. USERNAME='tg3793_arch'
  40.  
  41. # Username to access the MySQL server e.g. password
  42. PASSWORD='F1k3P1ssw0rd'
  43.  
  44. # Host name (or IP address) of MySQL server e.g localhost
  45. DBHOST='localhost'
  46.  
  47. # Port on which the MySQL server is reachable e.g. 5710
  48. # So what port is GatorHost MySQL on?
  49. PORT=5710
  50.  
  51. # List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
  52. # DBNAMES="tg3793_sope1 tg3793_wdps1"
  53. DBNAMES="all"
  54.  
  55. # Backup directory location e.g /backups
  56. BACKUPDIR="/home/tg3793/website_backups"
  57.  
  58. # Mail setup
  59. # What would you like to be mailed to you?
  60. # - log    : send only log file
  61. # - files  : send log file and sql files as attachments (see docs)
  62. # - stdout : will simply output the log to the screen if run manually.
  63. # - quiet  : Only send logs if an error occurs to the MAILADDR.
  64. MAILCONTENT="files"
  65.  
  66. # Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])
  67. MAXATTSIZE="8000"
  68.  
  69. # Email Address to send mail to? (user@domain.com)
  70. MAILADDR="gottlists@gmail.com"
  71.  
  72.  
  73. # ============================================================
  74. # === ADVANCED OPTIONS ( Read the doc's below for details )===
  75. #=============================================================
  76.  
  77. # List of DBBNAMES for Monthly Backups.
  78. MDBNAMES="mysql $DBNAMES"
  79.  
  80. # List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes)
  81. DBEXCLUDE="information_schema"
  82.  
  83. # List of tables to exclude from the backup (in form db.table)
  84. TABLEEXCLUDE=""
  85.  
  86. # Include CREATE DATABASE in backup?
  87. CREATE_DATABASE=no
  88.  
  89. # Separate backup directory and file for each DB? (yes or no)
  90. SEPDIR=yes
  91.  
  92. # Which day do you want weekly backups? (1 to 7 where 1 is Monday)
  93. DOWEEKLY=7
  94.  
  95. # Choose Compression type. (gzip or bzip2)
  96. COMP=bzip2
  97.  
  98. # Enable inline or piped compression.
  99. PIPECOMP=yes
  100.  
  101. # Compress communications between backup server and MySQL server?
  102. COMMCOMP=no
  103.  
  104. # Additionally keep a copy of the most recent backup in a seperate directory.
  105. LATEST=no
  106.  
  107. #  The maximum size of the buffer for client/server communication. e.g. 16MB (maximum is 1GB)
  108. MAX_ALLOWED_PACKET=
  109.  
  110. #  For connections to localhost. Sometimes the Unix socket file must be specified.
  111. SOCKET=
  112.  
  113. # Command to run before backups (uncomment to use)
  114. #PREBACKUP="/etc/mysql-backup-pre"
  115.  
  116. # Command run after backups (uncomment to use)
  117. #POSTBACKUP="/etc/mysql-backup-post"
  118. fi
  119.  
  120. #=====================================================================
  121. # Options documantation
  122. #=====================================================================
  123. # Set USERNAME and PASSWORD of a user that has at least SELECT permission
  124. # to ALL databases.
  125. #
  126. # Set the DBHOST option to the server you wish to backup, leave the
  127. # default to backup "this server".(to backup multiple servers make
  128. # copies of this file and set the options for that server)
  129. #
  130. # Put in the list of DBNAMES(Databases)to be backed up. If you would like
  131. # to backup ALL DBs on the server set DBNAMES="all".(if set to "all" then
  132. # any new DBs will automatically be backed up without needing to modify
  133. # this backup script when a new DB is created).
  134. #
  135. # If the DB you want to backup has a space in the name replace the space
  136. # with a % e.g. "data base" will become "data%base"
  137. # NOTE: Spaces in DB names may not work correctly when SEPDIR=no.
  138. #
  139. # You can change the backup storage location from /backups to anything
  140. # you like by using the BACKUPDIR setting..
  141. #
  142. # The MAILCONTENT and MAILADDR options and pretty self explanitory, use
  143. # these to have the backup log mailed to you at any email address or multiple
  144. # email addresses in a space seperated list.
  145. # (If you set mail content to "log" you will require access to the "mail" program
  146. # on your server. If you set this to "files" you will have to have mutt installed
  147. # on your server. If you set it to "stdout" it will log to the screen if run from
  148. # the console or to the cron job owner if run through cron. If you set it to "quiet"
  149. # logs will only be mailed if there are errors reported. )
  150. #
  151. # MAXATTSIZE sets the largest allowed email attachments total (all backup files) you
  152. # want the script to send. This is the size before it is encoded to be sent as an email
  153. # so if your mail server will allow a maximum mail size of 5MB I would suggest setting
  154. # MAXATTSIZE to be 25% smaller than that so a setting of 4000 would probably be fine.
  155. #
  156. # Finally copy autobackupmysql.sh to anywhere on your server and make sure
  157. # to set executable permission. You can also copy the script to
  158. # /etc/cron.daily to have it execute automatically every night or simply
  159. # place a symlink in /etc/cron.daily to the file if you wish to keep it
  160. # somwhere else.
  161. # NOTE:On Debian copy the file with no extention for it to be run
  162. # by cron e.g just name the file "autobackupmysql"
  163. #
  164. # Thats it..
  165. #
  166. #
  167. # === Advanced options doc's ===
  168. #
  169. # The list of MDBNAMES is the DB's to be backed up only monthly. You should
  170. # always include "mysql" in this list to backup your user/password
  171. # information along with any other DBs that you only feel need to
  172. # be backed up monthly. (if using a hosted server then you should
  173. # probably remove "mysql" as your provider will be backing this up)
  174. # NOTE: If DBNAMES="all" then MDBNAMES has no effect as all DBs will be backed
  175. # up anyway.
  176. #
  177. # If you set DBNAMES="all" you can configure the option DBEXCLUDE. Other
  178. # wise this option will not be used.
  179. # This option can be used if you want to backup all dbs, but you want
  180. # exclude some of them. (eg. a db is to big).
  181. #
  182. # Set CREATE_DATABASE to "yes" (the default) if you want your SQL-Dump to create
  183. # a database with the same name as the original database when restoring.
  184. # Saying "no" here will allow your to specify the database name you want to
  185. # restore your dump into, making a copy of the database by using the dump
  186. # created with automysqlbackup.
  187. # NOTE: Not used if SEPDIR=no
  188. #
  189. # The SEPDIR option allows you to choose to have all DBs backed up to
  190. # a single file (fast restore of entire server in case of crash) or to
  191. # seperate directories for each DB (each DB can be restored seperately
  192. # in case of single DB corruption or loss).
  193. #
  194. # To set the day of the week that you would like the weekly backup to happen
  195. # set the DOWEEKLY setting, this can be a value from 1 to 7 where 1 is Monday,
  196. # The default is 6 which means that weekly backups are done on a Saturday.
  197. #
  198. # COMP is used to choose the compression used, options are gzip or bzip2.
  199. # bzip2 will produce slightly smaller files but is more processor intensive so
  200. # may take longer to complete.
  201. #
  202. # PIPECOMP is used to make the compression inline. This may  reduce the time
  203. # taken for completion.
  204. #
  205. # COMMCOMP is used to enable or diable mysql client to server compression, so
  206. # it is useful to save bandwidth when backing up a remote MySQL server over
  207. # the network.
  208. #
  209. # LATEST is to store an additional copy of the latest backup to a standard
  210. # location so it can be downloaded bt thrid party scripts.
  211. #
  212. # If the DB's being backed up make use of large BLOB fields then you may need
  213. # to increase the MAX_ALLOWED_PACKET setting, for example 16MB..
  214. #
  215. # When connecting to localhost as the DB server (DBHOST=localhost) sometimes
  216. # the system can have issues locating the socket file.. This can now be set
  217. # using the SOCKET parameter.. An example may be SOCKET=/private/tmp/mysql.sock
  218. #
  219. # Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands
  220. # or scripts to perform tasks either before or after the backup process.
  221. #
  222. #
  223. #=====================================================================
  224. # Backup Rotation..
  225. #=====================================================================
  226. #
  227. # Daily Backups are rotated weekly..
  228. # Weekly Backups are run by default on Saturday Morning when
  229. # cron.daily scripts are run...Can be changed with DOWEEKLY setting..
  230. # Weekly Backups are rotated on a 5 week cycle..
  231. # Monthly Backups are run on the 1st of the month..
  232. # Monthly Backups are NOT rotated automatically...
  233. # It may be a good idea to copy Monthly backups offline or to another
  234. # server..
  235. #
  236. #=====================================================================
  237. # Please Note!!
  238. #=====================================================================
  239. #
  240. # I take no responsibility for any data loss or corruption when using
  241. # this script..
  242. # This script will not help in the event of a hard drive crash. If a
  243. # copy of the backup has not be stored offline or on another PC..
  244. # You should copy your backups offline regularly for best protection.
  245. #
  246. # Happy backing up...
  247. #
  248. #=====================================================================
  249. # Restoring
  250. #=====================================================================
  251. # Firstly you will need to uncompress the backup file.
  252. # eg.
  253. # gunzip file.gz (or bunzip2 file.bz2)
  254. #
  255. # Next you will need to use the mysql client to restore the DB from the
  256. # sql file.
  257. # eg.
  258. # mysql --user=username --pass=password --host=dbserver database < /path/file.sql
  259. # or
  260. # mysql --user=username --pass=password --host=dbserver -e "source /path/file.sql" database
  261. #
  262. # NOTE: Make sure you use "<" and not ">" in the above command because
  263. # you are piping the file.sql to mysql and not the other way around.
  264. #
  265. # Lets hope you never have to use this.. :)
  266. #
  267. #=====================================================================
  268. # Change Log
  269. #=====================================================================
  270. #
  271. # VER 1.4 - (2010-11-17)
  272. #   Add mysql server port support (based on patch by Eike Starkman)
  273. # VER 1.3 - (2009-12-22)
  274. #   Add FreeBSD specific configuration check (patch by Frank Wall)
  275. #   Fix typo in the configuration file check (patch by Frank Wall)
  276. #   Fix for Socket file ignored when DBNAMES="all" given
  277. #   Minor IO redirection fix
  278. # VER 1.2 - (2009-12-18)
  279. #   Add support for configuration files
  280. # VER 1.1 - (2009-12-10)
  281. #   Fix for missing suffix
  282. #   Some text changes
  283. #   Add ignore table(s) feature
  284. #   Make bzip2 as default compression
  285. # VER 1.0 - (2009-12-09)
  286. #   Initial Revamped Release
  287. #
  288. #=====================================================================
  289. #=====================================================================
  290. #=====================================================================
  291. #
  292. # Should not need to be modified from here down!!
  293. #
  294. #=====================================================================
  295. #=====================================================================
  296. #=====================================================================
  297. PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin
  298. DATE=`date +%Y-%m-%d_%Hh%Mm`                # Datestamp e.g 2002-09-21
  299. DOW=`date +%A`                          # Day of the week e.g. Monday
  300. DNOW=`date +%u`                     # Day number of the week 1 to 7 where 1 represents Monday
  301. DOM=`date +%d`                          # Date of the Month e.g. 27
  302. M=`date +%B`                            # Month e.g January
  303. W=`date +%V`                            # Week Number e.g 37
  304. VER=1.3                                 # Version Number
  305. LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log       # Logfile Name
  306. LOGERR=$BACKUPDIR/ERRORS_$DBHOST-`date +%N`.log     # Logfile Name
  307. BACKUPFILES=""
  308. OPT="--quote-names --opt"           # OPT string for use with mysqldump ( see man mysqldump )
  309.  
  310. # Add --compress mysqldump option to $OPT
  311. if [ "$COMMCOMP" = "yes" ]; then
  312.     OPT="$OPT --compress"
  313. fi
  314.  
  315. # Add --compress mysqldump option to $OPT
  316. if [ "$MAX_ALLOWED_PACKET" ]; then
  317.     OPT="$OPT --max_allowed_packet=$MAX_ALLOWED_PACKET"
  318. fi
  319.  
  320. # Set the suffix for compressed file
  321. if [ "$COMP" = "gzip" ]; then
  322.     SUFFIX=".gz"
  323. elif [ "$COMP" = "bzip2" ]; then
  324.     SUFFIX=".bz2"
  325. else
  326.     SUFFIX=""
  327. fi
  328.  
  329. # Create required directories
  330. if [ ! -e "$BACKUPDIR" ]        # Check Backup Directory exists.
  331.     then
  332.     mkdir -p "$BACKUPDIR"
  333. fi
  334.  
  335. if [ ! -e "$BACKUPDIR/daily" ]      # Check Daily Directory exists.
  336.     then
  337.     mkdir -p "$BACKUPDIR/daily"
  338. fi
  339.  
  340. if [ ! -e "$BACKUPDIR/weekly" ]     # Check Weekly Directory exists.
  341.     then
  342.     mkdir -p "$BACKUPDIR/weekly"
  343. fi
  344.  
  345. if [ ! -e "$BACKUPDIR/monthly" ]    # Check Monthly Directory exists.
  346.     then
  347.     mkdir -p "$BACKUPDIR/monthly"
  348. fi
  349.  
  350. if [ "$LATEST" = "yes" ]
  351.     then
  352.     if [ ! -e "$BACKUPDIR/latest" ] # Check Latest Directory exists.
  353.         then
  354.         mkdir -p "$BACKUPDIR/latest"
  355.     fi
  356.     eval rm -fv "$BACKUPDIR/latest/*"
  357. fi
  358.  
  359. # IO redirection for logging.
  360. touch $LOGFILE
  361. exec 6>&1           # Link file descriptor #6 with stdout.
  362.                     # Saves stdout.
  363. exec > $LOGFILE     # stdout replaced with file $LOGFILE.
  364. touch $LOGERR
  365. exec 7>&2           # Link file descriptor #7 with stderr.
  366.                     # Saves stderr.
  367. exec 2> $LOGERR     # stderr replaced with file $LOGERR.
  368.  
  369.  
  370. # Functions
  371.  
  372. # Database dump function
  373. dbdump () {
  374. if [ "$PIPECOMP" = "yes" ]; then
  375.     mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST --port=$PORT $OPT $1 | $COMP > "$2$SUFFIX"
  376. else
  377.     mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST --port=$PORT $OPT $1 > $2
  378. fi
  379. return 0
  380. }
  381.  
  382. # Compression function plus latest copy
  383. compression () {
  384. if [ "$COMP" = "gzip" ]; then
  385.     if [ "$PIPECOMP" = "no" ]; then
  386.         gzip -f "$1"
  387.         echo
  388.         echo Backup Information for "$1"
  389.         gzip -l "$1.gz"
  390.     fi
  391. elif [ "$COMP" = "bzip2" ]; then
  392.     if [ "$PIPECOMP" = "no" ]; then
  393.         echo Compression information for "$1.bz2"
  394.         bzip2 -f -v $1 2>&1
  395.     fi
  396. else
  397.     echo "No compression option set, check advanced settings"
  398. fi
  399. if [ "$LATEST" = "yes" ]; then
  400.     cp "$1$SUFFIX" "$BACKUPDIR/latest/"
  401. fi 
  402. return 0
  403. }
  404.  
  405.  
  406. # Run command before we begin
  407. if [ "$PREBACKUP" ]
  408.     then
  409.     echo ======================================================================
  410.     echo "Prebackup command output."
  411.     echo
  412.     eval $PREBACKUP
  413.     echo
  414.     echo ======================================================================
  415.     echo
  416. fi
  417.  
  418. # Add --ignore-table options to $OPT
  419. if [ -n "$TABLEEXCLUDE" ]; then
  420.     for table in $TABLEEXCLUDE ; do
  421.         OPT="${OPT} --ignore-table=${table}"
  422.     done
  423. fi
  424.  
  425.  
  426. if [ "$SEPDIR" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump
  427.     if [ "$CREATE_DATABASE" = "no" ]; then
  428.         OPT="$OPT --no-create-db"
  429.     else
  430.         OPT="$OPT --databases"
  431.     fi
  432. else
  433.     OPT="$OPT --databases"
  434. fi
  435.  
  436. # Hostname for LOG information
  437. if [ "$DBHOST" = "localhost" ]; then
  438.     HOST=`hostname`
  439.     if [ "$SOCKET" ]; then
  440.         OPT="$OPT --socket=$SOCKET"
  441.     fi
  442. else
  443.     HOST=$DBHOST
  444. fi
  445.  
  446. # If backing up all DBs on the server
  447. if [ "$DBNAMES" = "all" ]; then
  448.     if [ "$SOCKET" ]; then
  449.         DBNAMES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --port=$PORT --socket=$SOCKET --batch --skip-column-names -e "show databases"| sed 's/ /%/g'`"
  450.     else
  451.         DBNAMES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --port=$PORT --batch --skip-column-names -e "show databases"| sed 's/ /%/g'`"
  452.     fi
  453.     # If DBs are excluded
  454.     for exclude in $DBEXCLUDE
  455.     do
  456.         DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"`
  457.     done
  458.  
  459.         MDBNAMES=$DBNAMES
  460. fi
  461.    
  462. echo ======================================================================
  463. echo AutoBackupMySQL Version $VER
  464. echo http://sourceforge.net/projects/autobackupmysql/
  465. echo
  466. echo Backup of Database Server - $HOST
  467. echo ======================================================================
  468.  
  469. # Test is seperate DB backups are required
  470. if [ "$SEPDIR" = "yes" ]; then
  471. echo Backup Start Time `date`
  472. echo ======================================================================
  473.     # Monthly Full Backup of all Databases
  474.     if [ $DOM = "01" ]; then
  475.         for MDB in $MDBNAMES
  476.         do
  477.  
  478.              # Prepare $DB for using
  479.                 MDB="`echo $MDB | sed 's/%/ /g'`"
  480.  
  481.             if [ ! -e "$BACKUPDIR/monthly/$MDB" ]       # Check Monthly DB Directory exists.
  482.                 then
  483.                 mkdir -p "$BACKUPDIR/monthly/$MDB"
  484.             fi
  485.             echo Monthly Backup of $MDB...
  486.                 dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
  487.                 compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
  488.                 BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX"
  489.             echo ----------------------------------------------------------------------
  490.         done
  491.     fi
  492.  
  493.     for DB in $DBNAMES
  494.     do
  495.     # Prepare $DB for using
  496.     DB="`echo $DB | sed 's/%/ /g'`"
  497.    
  498.     # Create Seperate directory for each DB
  499.     if [ ! -e "$BACKUPDIR/daily/$DB" ]      # Check Daily DB Directory exists.
  500.         then
  501.         mkdir -p "$BACKUPDIR/daily/$DB"
  502.     fi
  503.    
  504.     if [ ! -e "$BACKUPDIR/weekly/$DB" ]     # Check Weekly DB Directory exists.
  505.         then
  506.         mkdir -p "$BACKUPDIR/weekly/$DB"
  507.     fi
  508.    
  509.     # Weekly Backup
  510.     if [ $DNOW = $DOWEEKLY ]; then
  511.         echo Weekly Backup of Database \( $DB \)
  512.         echo Rotating 5 weeks Backups...
  513.             if [ "$W" -le 05 ];then
  514.                 REMW=`expr 48 + $W`
  515.             elif [ "$W" -lt 15 ];then
  516.                 REMW=0`expr $W - 5`
  517.             else
  518.                 REMW=`expr $W - 5`
  519.             fi
  520.         eval rm -fv "$BACKUPDIR/weekly/$DB/${DB}_week.$REMW.*"
  521.         echo
  522.             dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
  523.             compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
  524.             BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX"
  525.         echo ----------------------------------------------------------------------
  526.    
  527.     # Daily Backup
  528.     else
  529.         echo Daily Backup of Database \( $DB \)
  530.         echo Rotating last weeks Backup...
  531.         eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*"
  532.         echo
  533.             dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
  534.             compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
  535.             BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX"
  536.         echo ----------------------------------------------------------------------
  537.     fi
  538.     done
  539. echo Backup End `date`
  540. echo ======================================================================
  541.  
  542.  
  543. else # One backup file for all DBs
  544. echo Backup Start `date`
  545. echo ======================================================================
  546.     # Monthly Full Backup of all Databases
  547.     if [ $DOM = "01" ]; then
  548.         echo Monthly full Backup of \( $MDBNAMES \)...
  549.             dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
  550.             compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
  551.             BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX"
  552.         echo ----------------------------------------------------------------------
  553.     fi
  554.  
  555.     # Weekly Backup
  556.     if [ $DNOW = $DOWEEKLY ]; then
  557.         echo Weekly Backup of Databases \( $DBNAMES \)
  558.         echo
  559.         echo Rotating 5 weeks Backups...
  560.             if [ "$W" -le 05 ];then
  561.                 REMW=`expr 48 + $W`
  562.             elif [ "$W" -lt 15 ];then
  563.                 REMW=0`expr $W - 5`
  564.             else
  565.                 REMW=`expr $W - 5`
  566.             fi
  567.         eval rm -fv "$BACKUPDIR/weekly/week.$REMW.*"
  568.         echo
  569.             dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql"
  570.             compression "$BACKUPDIR/weekly/week.$W.$DATE.sql"
  571.             BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX"
  572.         echo ----------------------------------------------------------------------
  573.        
  574.     # Daily Backup
  575.     else
  576.         echo Daily Backup of Databases \( $DBNAMES \)
  577.         echo
  578.         echo Rotating last weeks Backup...
  579.         eval rm -fv "$BACKUPDIR/daily/*.$DOW.sql.*"
  580.         echo
  581.             dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql"
  582.             compression "$BACKUPDIR/daily/$DATE.$DOW.sql"
  583.             BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX"
  584.         echo ----------------------------------------------------------------------
  585.     fi
  586. echo Backup End Time `date`
  587. echo ======================================================================
  588. fi
  589. echo Total disk space used for backup storage..
  590. echo Size - Location
  591. echo `du -hs "$BACKUPDIR"`
  592. echo
  593. echo ======================================================================
  594. #echo If you find AutoBackupMySQL valuable please make a donation at
  595. #echo http://sourceforge.net/project/project_donations.php?group_id=101066
  596. echo ======================================================================
  597.  
  598. # Run command when we're done
  599. if [ "$POSTBACKUP" ]; then
  600.     echo ======================================================================
  601.     echo "Postbackup command output."
  602.     echo
  603.     eval $POSTBACKUP
  604.     echo
  605.     echo ======================================================================
  606. fi
  607.  
  608. #Clean up IO redirection
  609. exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
  610. exec 2>&7 7>&-      # Restore stderr and close file descriptor #7.
  611.  
  612. if [ "$MAILCONTENT" = "files" ]; then
  613.     if [ -s "$LOGERR" ]; then
  614.         # Include error log if is larger than zero.
  615.         BACKUPFILES="$BACKUPFILES $LOGERR"
  616.         ERRORNOTE="WARNING: Error Reported - "
  617.     fi
  618.     #Get backup size
  619.     ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//`
  620.     if [ $MAXATTSIZE -ge $ATTSIZE ]; then
  621.         BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"` #enable multiple attachments
  622.         mutt -s "$ERRORNOTE MySQL Backup Log and SQL Files for $HOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE     #send via mutt
  623.     else
  624.         cat "$LOGFILE" | mail -s "WARNING! - MySQL Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDR
  625.     fi
  626. elif [ "$MAILCONTENT" = "log" ]; then
  627.     cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOST - $DATE" $MAILADDR
  628.     if [ -s "$LOGERR" ]; then
  629.         cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR
  630.     fi 
  631. elif [ "$MAILCONTENT" = "quiet" ]; then
  632.     if [ -s "$LOGERR" ]; then
  633.         cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR
  634.         cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOST - $DATE" $MAILADDR
  635.     fi
  636. else
  637.     if [ -s "$LOGERR" ]; then
  638.         cat "$LOGFILE"
  639.         echo
  640.         echo "###### WARNING ######"
  641.         echo "Errors reported during AutoBackupMySQL execution.. Backup failed"
  642.         echo "Error log below.."
  643.         cat "$LOGERR"
  644.     else
  645.         cat "$LOGFILE"
  646.     fi 
  647. fi
  648.  
  649. if [ -s "$LOGERR" ]; then
  650.     STATUS=1
  651. else
  652.     STATUS=0
  653. fi
  654.  
  655. # Clean up Logfile
  656. eval rm -f "$LOGFILE"
  657. eval rm -f "$LOGERR"
  658.  
  659. exit $STATUS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement