From 92edc9d62d65b2d133939e146c4b2945c3b5d6c7 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 2 Nov 2016 17:37:11 +0000 Subject: [PATCH] Avoid multiple local database backups --- src/freedombone-utils-database | 65 ++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/freedombone-utils-database b/src/freedombone-utils-database index 5146f0ad..c4688319 100755 --- a/src/freedombone-utils-database +++ b/src/freedombone-utils-database @@ -44,55 +44,60 @@ function backup_database_local { database_name=$1 backup_databases_script=/usr/bin/backupdatabases - echo '' >> $backup_databases_script - echo "# Backup the ${database_name} database" >> $backup_databases_script - echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script - echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script - echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script - echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script - echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script - echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script - echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script - echo '' >> $backup_databases_script - echo ' # try to restore yesterdays database' >> $backup_databases_script - echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script - echo '' >> $backup_databases_script - echo ' # Send a warning email' >> $backup_databases_script - echo " echo \"Unable to create a backup of the ${database_name} database. Attempted to restore from yesterdays backup\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script - echo ' else' >> $backup_databases_script - echo ' # Send a warning email' >> $backup_databases_script - echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script - echo ' fi' >> $backup_databases_script - echo 'else' >> $backup_databases_script - echo ' chmod 600 $TEMPFILE' >> $backup_databases_script - echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script - echo '' >> $backup_databases_script - echo ' # Make the backup readable only by root' >> $backup_databases_script - echo ' chmod 600 $DAILYFILE' >> $backup_databases_script - echo 'fi' >> $backup_databases_script + if ! grep -q "# Backup the ${database_name} database" $backup_databases_script; then + echo '' >> $backup_databases_script + echo "# Backup the ${database_name} database" >> $backup_databases_script + echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script + echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script + echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script + echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script + echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script + echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script + echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script + echo '' >> $backup_databases_script + echo ' # try to restore yesterdays database' >> $backup_databases_script + echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script + echo '' >> $backup_databases_script + echo ' # Send a warning email' >> $backup_databases_script + echo " echo \"Unable to create a backup of the ${database_name} database. Attempted to restore from yesterdays backup\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script + echo ' else' >> $backup_databases_script + echo ' # Send a warning email' >> $backup_databases_script + echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script + echo ' fi' >> $backup_databases_script + echo 'else' >> $backup_databases_script + echo ' chmod 600 $TEMPFILE' >> $backup_databases_script + echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script + echo '' >> $backup_databases_script + echo ' # Make the backup readable only by root' >> $backup_databases_script + echo ' chmod 600 $DAILYFILE' >> $backup_databases_script + echo 'fi' >> $backup_databases_script + echo "# End of ${database_name} database backup" >> $backup_databases_script + fi weekly_backup_script=/etc/cron.weekly/backupdatabasesweekly - if ! grep -q "${database_name}" ${weekly_backup_script}; then + if ! grep -q "Backup ${database_name}" ${weekly_backup_script}; then echo '' >> ${weekly_backup_script} - echo "# ${database_name}" >> ${weekly_backup_script} + echo "# Backup ${database_name}" >> ${weekly_backup_script} echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${weekly_backup_script} echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_2weekly.sql" >> ${weekly_backup_script} echo 'fi' >> ${weekly_backup_script} echo "if [ -f /var/backups/${database_name}_daily.sql ]; then" >> ${weekly_backup_script} echo " cp -f /var/backups/${database_name}_daily.sql /var/backups/${database_name}_weekly.sql" >> ${weekly_backup_script} echo 'fi' >> ${weekly_backup_script} + echo "# End of backup for ${database_name}" >> ${weekly_backup_script} fi monthly_backup_script=/etc/cron.monthly/backupdatabasesmonthly - if ! grep -q "${database_name}" ${monthly_backup_script}; then + if ! grep -q "Backup ${database_name}" ${monthly_backup_script}; then echo '' >> ${monthly_backup_script} - echo "# ${database_name}" >> ${monthly_backup_script} + echo "# Backup ${database_name}" >> ${monthly_backup_script} echo "if [ -f /var/backups/${database_name}_monthly.sql ]; then" >> ${monthly_backup_script} echo " cp -f /var/backups/${database_name}_monthly.sql /var/backups/${database_name}_2monthly.sql" >> ${monthly_backup_script} echo 'fi' >> ${monthly_backup_script} echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${monthly_backup_script} echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_monthly.sql" >> ${monthly_backup_script} echo 'fi' >> ${monthly_backup_script} + echo "# End of backup for ${database_name}" >> ${monthly_backup_script} fi if ! grep -q "${database_name}" /etc/cron.hourly/repair; then