Advertisement
Guest User

Untitled

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