Advertisement
Guest User

automysqlbackup

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