|
1 | 1 | #!/bin/bash |
2 | 2 | # |
3 | 3 | # MySQL Backup Script |
4 | | -# VER. 2.3 - http://sourceforge.net/projects/automysqlbackup/ |
| 4 | +# VER. 2.4 - http://sourceforge.net/projects/automysqlbackup/ |
5 | 5 | # Copyright (c) 2002-2003 [email protected] |
6 | 6 | # |
7 | 7 | # This program is free software; you can redistribute it and/or modify |
@@ -44,6 +44,7 @@ BACKUPDIR="/backups" |
44 | 44 | # - log : send only log file |
45 | 45 | # - files : send log file and sql files as attachments (see docs) |
46 | 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. |
47 | 48 | MAILCONTENT="stdout" |
48 | 49 |
|
49 | 50 | # Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs]) |
@@ -78,6 +79,9 @@ COMP=gzip |
78 | 79 | # Compress communications between backup server and MySQL server? |
79 | 80 | COMMCOMP=no |
80 | 81 |
|
| 82 | +# Additionally keep a copy of the most recent backup in a seperate directory. |
| 83 | +LATEST=no |
| 84 | + |
81 | 85 | # Command to run before backups (uncomment to use) |
82 | 86 | #PREBACKUP="/etc/mysql-backup-pre" |
83 | 87 |
|
@@ -111,8 +115,9 @@ COMMCOMP=no |
111 | 115 | # email addresses in a space seperated list. |
112 | 116 | # (If you set mail content to "log" you will require access to the "mail" program |
113 | 117 | # on your server. If you set this to "files" you will have to have mutt installed |
114 | | -# on your server. If you set it sto stdout it will log to the screen if run from |
115 | | -# the console or to the cron job owner if run through cron) |
| 118 | +# on your server. If you set it to "stdout" it will log to the screen if run from |
| 119 | +# the console or to the cron job owner if run through cron. If you set it to "quiet" |
| 120 | +# logs will only be mailed if there are errors reported. ) |
116 | 121 | # |
117 | 122 | # MAXATTSIZE sets the largest allowed email attachments total (all backup files) you |
118 | 123 | # want the script to send. This is the size before it is encoded to be sent as an email |
@@ -169,6 +174,9 @@ COMMCOMP=no |
169 | 174 | # it is useful to save bandwidth when backing up a remote MySQL server over |
170 | 175 | # the network. |
171 | 176 | # |
| 177 | +# LATEST is to store an additional copy of the latest backup to a standard |
| 178 | +# location so it can be downloaded bt thrid party scripts. |
| 179 | +# |
172 | 180 | # Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands |
173 | 181 | # or scripts to perform tasks either before or after the backup process. |
174 | 182 | # |
@@ -221,6 +229,14 @@ COMMCOMP=no |
221 | 229 | # Change Log |
222 | 230 | #===================================================================== |
223 | 231 | # |
| 232 | +# VER 2.4 - (2006-01-23) |
| 233 | +# Fixed bug where weekly backups were not being rotated. (Fix by wolf02) |
| 234 | +# Added hour an min to backup filename for the case where backups are taken multiple |
| 235 | +# times in a day. NOTE This is not complete support for mutiple executions of the script |
| 236 | +# in a single day. |
| 237 | +# Added MAILCONTENT="quiet" option, see docs for details. (requested by snowsam) |
| 238 | +# Updated path statment for compatibility with OSX. |
| 239 | +# Added "LATEST" to additionally store the last backup to a standard location. (request by Grant29) |
224 | 240 | # VER 2.3 - (2005-11-07) |
225 | 241 | # Better error handling and notification of errors (a long time coming) |
226 | 242 | # Compression on Backup server to MySQL server communications. |
@@ -304,14 +320,14 @@ COMMCOMP=no |
304 | 320 | #===================================================================== |
305 | 321 | #===================================================================== |
306 | 322 | #===================================================================== |
307 | | -PATH=/usr/local/bin:/usr/bin:/bin |
308 | | -DATE=`date +%Y-%m-%d` # Datestamp e.g 2002-09-21 |
| 323 | +PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin |
| 324 | +DATE=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21 |
309 | 325 | DOW=`date +%A` # Day of the week e.g. Monday |
310 | 326 | DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday |
311 | 327 | DOM=`date +%d` # Date of the Month e.g. 27 |
312 | 328 | M=`date +%B` # Month e.g January |
313 | 329 | W=`date +%V` # Week Number e.g 37 |
314 | | -VER=2.3 # Version Number |
| 330 | +VER=2.4 # Version Number |
315 | 331 | LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log # Logfile Name |
316 | 332 | LOGERR=$BACKUPDIR/ERRORS_$DBHOST-`date +%N`.log # Logfile Name |
317 | 333 | BACKUPFILES="" |
@@ -344,6 +360,14 @@ if [ ! -e "$BACKUPDIR/monthly" ] # Check Monthly Directory exists. |
344 | 360 | mkdir -p "$BACKUPDIR/monthly" |
345 | 361 | fi |
346 | 362 |
|
| 363 | +if [ "$LATEST" = "yes" ] |
| 364 | +then |
| 365 | + if [ ! -e "$BACKUPDIR/latest" ] # Check Latest Directory exists. |
| 366 | + then |
| 367 | + mkdir -p "$BACKUPDIR/latest" |
| 368 | + fi |
| 369 | +eval rm -fv "$BACKUPDIR/latest/*" |
| 370 | +fi |
347 | 371 |
|
348 | 372 | # IO redirection for logging. |
349 | 373 | touch $LOGFILE |
@@ -380,6 +404,9 @@ elif [ "$COMP" = "bzip2" ]; then |
380 | 404 | else |
381 | 405 | echo "No compression option set, check advanced settings" |
382 | 406 | fi |
| 407 | +if [ "$LATEST" = "yes" ]; then |
| 408 | + cp $1$SUFFIX "$BACKUPDIR/latest/" |
| 409 | +fi |
383 | 410 | return 0 |
384 | 411 | } |
385 | 412 |
|
@@ -485,7 +512,7 @@ echo ====================================================================== |
485 | 512 | else |
486 | 513 | REMW=`expr $W - 5` |
487 | 514 | fi |
488 | | - eval rm -fv "$BACKUPDIR/weekly/$DB/week.$REMW.*" |
| 515 | + eval rm -fv "$BACKUPDIR/weekly/$DB_week.$REMW.*" |
489 | 516 | echo |
490 | 517 | dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" |
491 | 518 | compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" |
@@ -602,6 +629,13 @@ then |
602 | 629 | then |
603 | 630 | cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR |
604 | 631 | fi |
| 632 | +elif [ "$MAILCONTENT" = "quiet" ] |
| 633 | +then |
| 634 | + if [ -s "$LOGERR" ] |
| 635 | + then |
| 636 | + cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR |
| 637 | + cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOST - $DATE" $MAILADDR |
| 638 | + fi |
605 | 639 | else |
606 | 640 | if [ -s "$LOGERR" ] |
607 | 641 | then |
|
0 commit comments