From: Adam F. <af...@ci...> - 2007-07-10 16:23:49
|
When I added the per table patch to the script. The rotation for daily, weekly, and monthly backups has not worked. In the weekly directory I have a folder for every day of the week that gets created but only the 7 day actually contains any data. drwxr-xr-x 2 nobody wheel 4096 Jul 3 01:07 circlepix_pertable.week.27.2007-07-03_01h00m drwxr-xr-x 2 nobody wheel 4096 Jul 4 01:07 circlepix_pertable.week.27.2007-07-04_01h00m drwxr-xr-x 2 nobody wheel 4096 Jul 5 01:07 circlepix_pertable.week.27.2007-07-05_01h00m drwxr-xr-x 2 nobody wheel 4096 Jul 6 01:07 circlepix_pertable.week.27.2007-07-06_01h00m drwxr-xr-x 2 nobody wheel 4096 Jul 7 01:07 circlepix_pertable.week.27.2007-07-07_01h00m drwxr-xr-x 2 nobody wheel 12288 Jul 8 01:22 circlepix_pertable.week.27.2007-07-08_01h00m drwxr-xr-x 2 nobody wheel 4096 Jul 9 01:08 circlepix_pertable.week.28.2007-07-09_01h00m drwxr-xr-x 2 nobody wheel 4096 Jul 10 01:08 circlepix_pertable.week.28.2007-07-10_01h00m Here is my script. I have added the per table patch and the weekly rotation patch but to no avail. Please let me know what I need to change. thanks Adam PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/mysql/bin DATE=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21 DOW=`date +%A` # Day of the week e.g. Monday DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday DOM=`date +%d` # Date of the Month e.g. 27 M=`date +%B` # Month e.g January W=`date +%V` # Week Number e.g 37 VER=2.5 # Version Number LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log # Logfile Name LOGERR=$BACKUPDIR/ERRORS_$DBHOST-`date +%N`.log # Logfile Name BACKUPFILES="" OPT="--quote-names --opt" # OPT string for use with mysqldump ( see man mysqldump ) # Add --compress mysqldump option to $OPT if [ "$COMMCOMP" = "yes" ]; then OPT="$OPT --compress" fi # Add --compress mysqldump option to $OPT if [ "$MAX_ALLOWED_PACKET" ]; then OPT="$OPT --max_allowed_packet=$MAX_ALLOWED_PACKET" fi # Create required directories if [ ! -e "$BACKUPDIR" ] # Check Backup Directory exists. then mkdir -p "$BACKUPDIR" fi if [ ! -e "$BACKUPDIR/daily" ] # Check Daily Directory exists. then mkdir -p "$BACKUPDIR/daily" fi if [ ! -e "$BACKUPDIR/weekly" ] # Check Weekly Directory exists. then mkdir -p "$BACKUPDIR/weekly" fi if [ ! -e "$BACKUPDIR/monthly" ] # Check Monthly Directory exists. then mkdir -p "$BACKUPDIR/monthly" fi if [ "$LATEST" = "yes" ] then if [ ! -e "$BACKUPDIR/latest" ] # Check Latest Directory exists. then mkdir -p "$BACKUPDIR/latest" fi eval rm -fv "$BACKUPDIR/latest/*" fi # IO redirection for logging. touch $LOGFILE exec 6>&1 # Link file descriptor #6 with stdout. # Saves stdout. exec > $LOGFILE # stdout replaced with file $LOGFILE. touch $LOGERR exec 7>&2 # Link file descriptor #7 with stderr. # Saves stderr. exec 2> $LOGERR # stderr replaced with file $LOGERR. # Functions # Database dump function dbdump () { mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 > $2 return 0 } dbdump_table () { mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 $2 > $3 return 0 } # Database dump function with pipe compression plus latest copy dbdump_comp () { if [ "$COMP" = "gzip" ]; then echo echo Backup Information for "$1.gz" SUFFIX=".gz" mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 | gzip > $2$SUFFIX gzip -l "$1.gz" elif [ "$COMP" = "bzip2" ]; then echo Compression information for "$1.bz2" SUFFIX=".bz2" mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 | bzip2 -v 2>&1 > $2$SUFFIX else echo "No compression option set, check advanced settings" fi return 0 } dbdump_table_comp () { if [ "$COMP" = "gzip" ]; then echo echo Backup Information for "$3.gz" SUFFIX=".gz" mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 $2 | gzip > $3$SUFFIX gzip -l "$3.gz" elif [ "$COMP" = "bzip2" ]; then echo Compression information for "$3.bz2" SUFFIX=".bz2" mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 $2 | bzip2 -v 2>&1 > $3$SUFFIX else echo "No compression option set, check advanced settings" fi return 0 } # Compression function plus latest copy SUFFIX="" compression () { if [ "$COMP" = "gzip" ]; then gzip -f "$1" echo echo Backup Information for "$1.gz" gzip -l "$1.gz" SUFFIX=".gz" elif [ "$COMP" = "bzip2" ]; then echo Compression information for "$1.bz2" bzip2 -f -v $1 2>&1 SUFFIX=".bz2" else echo "No compression option set, check advanced settings" fi if [ "$LATEST" = "yes" ]; then cp $1$SUFFIX "$BACKUPDIR/latest/" fi return 0 } # Run command before we begin if [ "$PREBACKUP" ] then echo ====================================================================== echo "Prebackup command output." echo eval $PREBACKUP echo echo ====================================================================== echo fi # Add --ignore-table options to $OPT if [ -n "$TABLEEXCLUDE" ]; then for table in $TABLEEXCLUDE ; do OPT="${OPT} --ignore-table=${table}" done fi if [ "$SEPDIR" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump if [ "$CREATE_DATABASE" = "no" ]; then OPT="$OPT --no-create-db" elif [ "$PERTABLE" = "yes" ]; then OPT="$OPT --no-create-db" else OPT="$OPT --databases" fi else OPT="$OPT --databases" fi # Hostname for LOG information if [ "$DBHOST" = "localhost" ]; then HOST=`hostname` if [ "$SOCKET" ]; then OPT="$OPT --socket=$SOCKET" fi else HOST=$DBHOST fi # If backing up all DBs on the server if [ "$DBNAMES" = "all" ]; then DBNAMES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show databases"| sed 's/ /%/g'`" # If DBs are excluded for exclude in $DBEXCLUDE do DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"` done MDBNAMES=$DBNAMES fi echo ====================================================================== echo AutoMySQLBackup VER $VER echo http://sourceforge.net/projects/automysqlbackup/ echo echo Backup of Database Server - $HOST echo ====================================================================== # Test is seperate DB backups are required if [ "$SEPDIR" = "yes" ]; then echo Backup Start Time `date` echo ====================================================================== # Monthly Full Backup of all Databases if [ $DOM = "01" ]; then for MDB in $MDBNAMES do # Prepare $DB for using MDB="`echo $MDB | sed 's/%/ /g'`" if [ "$PERTABLE" = "yes" ]; # Check backup per table then # Start Monthly DB backup per table echo Monthly Backup of $MDB per table... TABLES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show tables" $MDB| sed 's/ /%/g'`" for TABLE in $TABLES do if [ ! -e "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB" ] # Check Monthly DB per table Directory exists. then mkdir -p "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB" fi if [ "$PIPECOMP" = "yes" ]; then dbdump_table_comp "$MDB" "$TABLE" "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql$SUFFIX" else dbdump_table "$MDB" "$TABLE" "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql" compression "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql$SUFFIX" fi done echo ---------------------------------------------------------------------- else # Start Monthly DB full if [ ! -e "$BACKUPDIR/monthly/$MDB" ] # Check Monthly DB Directory exists. then mkdir -p "$BACKUPDIR/monthly/$MDB" fi echo Monthly Backup of $MDB... if [ "$PIPECOMP" = "yes" ]; then dbdump_comp "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX" else dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql" compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX" fi echo ---------------------------------------------------------------------- fi done fi for DB in $DBNAMES do # Prepare $DB for using DB="`echo $DB | sed 's/%/ /g'`" # Create Seperate directory for each DB if [ "$PERTABLE" = "yes" ]; then if [ ! -e "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW" ] # Check Daily DB per table Directory exists. then mkdir -p "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW" fi if [ ! -e "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE" ] # Check Weekly DB per table Directory exists. then mkdir -p "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE" fi else if [ ! -e "$BACKUPDIR/daily/$DB" ] # Check Daily DB Directory exists. then mkdir -p "$BACKUPDIR/daily/$DB" fi if [ ! -e "$BACKUPDIR/weekly/$DB" ] # Check Weekly DB Directory exists. then mkdir -p "$BACKUPDIR/weekly/$DB" fi fi # Weekly Backup if [ $DNOW = $DOWEEKLY ]; then echo Weekly Backup of Database \( $DB \) if [ "$PERTABLE" = "yes" ]; then echo Rotating 5 weeks Backups... if [ "$W" -le 05 ];then REMW=`expr 48 + $W` elif [ "$W" -lt 15 ];then REMW=0`expr $W - 5` else REMW=`expr $W - 5` fi eval rm -rfv "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$REMW.*" echo TABLES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show tables" $DB| sed 's/ /%/g'`" for TABLE in $TABLES do if [ "$PIPECOMP" = "yes" ]; then dbdump_table_comp "$DB" "$TABLE" "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql$SUFFIX" else dbdump_table "$DB" "$TABLE" "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql" compression "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql$SUFFIX" fi done echo ---------------------------------------------------------------------- else echo Rotating 5 weeks Backups... if [ "$W" -le 05 ];then REMW=`expr 48 + $W` elif [ "$W" -lt 15 ];then REMW=0`expr $W - 5` else REMW=`expr $W - 5` fi eval rm -fv "$BACKUPDIR/weekly/$DB/${DB}_week.$REMW.*" echo if [ "$PIPECOMP" = "yes" ]; then dbdump_comp "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX" else dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX" fi echo ---------------------------------------------------------------------- fi # Daily Backup else echo Daily Backup of Database \( $DB \) if [ "$PERTABLE" = "yes" ]; then echo Rotating last weeks Backup... eval rm -rfv "$BACKUPDIR/daily/$DB/${DB}_pertable/*_pertable.*.$DOW" TABLES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show tables" $DB| sed 's/ /%/g'`" for TABLE in $TABLES do if [ "$PIPECOMP" = "yes" ]; then dbdump_table_comp "$DB" "$TABLE" "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql$SUFFIX" else dbdump_table "$DB" "$TABLE" "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql" compression "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql$SUFFIX" fi done echo ---------------------------------------------------------------------- else echo Rotating last weeks Backup... eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*" echo if [ "$PIPECOMP" = "yes" ]; then dbdump_comp "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX" else dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql" compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX" fi echo ---------------------------------------------------------------------- fi fi done echo Backup End `date` echo ====================================================================== else # One backup file for all DBs echo Backup Start `date` echo ====================================================================== # Monthly Full Backup of all Databases if [ $DOM = "01" ]; then echo Monthly full Backup of \( $MDBNAMES \)... if [ "$PIPECOMP" = "yes" ]; then dbdump_comp "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX" else dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql" compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX" fi echo ---------------------------------------------------------------------- fi # Weekly Backup if [ $DNOW = $DOWEEKLY ]; then echo Weekly Backup of Databases \( $DBNAMES \) echo echo Rotating 5 weeks Backups... if [ "$W" -le 05 ];then REMW=`expr 48 + $W` elif [ "$W" -lt 15 ];then REMW=0`expr $W - 5` else REMW=`expr $W - 5` fi eval rm -fv "$BACKUPDIR/weekly/week.$REMW.*" echo if [ "$PIPECOMP" = "yes" ]; then dbdump_comp "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX" else dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql" compression "$BACKUPDIR/weekly/week.$W.$DATE.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX" fi echo ---------------------------------------------------------------------- # Daily Backup else echo Daily Backup of Databases \( $DBNAMES \) echo echo Rotating last weeks Backup... eval rm -fv "$BACKUPDIR/daily/*.$DOW.sql.*" echo if [ "$PIPECOMP" = "yes" ]; then dbdump_comp "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX" else dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql" compression "$BACKUPDIR/daily/$DATE.$DOW.sql" BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX" fi echo ---------------------------------------------------------------------- fi echo Backup End Time `date` echo ====================================================================== fi echo Total disk space used for backup storage.. echo Size - Location echo `du -hs "$BACKUPDIR"` echo echo ====================================================================== echo If you find AutoMySQLBackup valuable please make a donation at echo http://sourceforge.net/project/project_donations.php?group_id=101066 echo ====================================================================== # Run command when we're done if [ "$POSTBACKUP" ] then echo ====================================================================== echo "Postbackup command output." echo eval $POSTBACKUP echo echo ====================================================================== fi #Clean up IO redirection exec 1>&6 6>&- # Restore stdout and close file descriptor #6. exec 1>&7 7>&- # Restore stdout and close file descriptor #7. if [ "$MAILCONTENT" = "files" ] then if [ -s "$LOGERR" ] then # Include error log if is larger than zero. BACKUPFILES="$BACKUPFILES $LOGERR" ERRORNOTE="WARNING: Error Reported - " fi #Get backup size ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//` if [ $MAXATTSIZE -ge $ATTSIZE ] then BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"` #enable multiple attachments mutt -s "$ERRORNOTE MySQL Backup Log and SQL Files for $HOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE #send via mutt else cat "$LOGFILE" | mail -s "WARNING! - MySQL Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDR fi elif [ "$MAILCONTENT" = "log" ] then cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOST - $DATE" $MAILADDR if [ -s "$LOGERR" ] then cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR fi elif [ "$MAILCONTENT" = "quiet" ] then if [ -s "$LOGERR" ] then cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOST - $DATE" $MAILADDR cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOST - $DATE" $MAILADDR fi else if [ -s "$LOGERR" ] then cat "$LOGFILE" echo echo "###### WARNING ######" echo "Errors reported during AutoMySQLBackup execution.. Backup failed" echo "Error log below.." cat "$LOGERR" else cat "$LOGFILE" fi fi if [ -s "$LOGERR" ] then STATUS=1 else STATUS=0 fi # Clean up Logfile eval rm -f "$LOGFILE" eval rm -f "$LOGERR" exit $STATUS |