Avoid multiple local database backups

This commit is contained in:
Bob Mottram 2016-11-02 17:37:11 +00:00
parent ab03b8e159
commit 92edc9d62d
1 changed files with 35 additions and 30 deletions

View File

@ -44,55 +44,60 @@ function backup_database_local {
database_name=$1 database_name=$1
backup_databases_script=/usr/bin/backupdatabases backup_databases_script=/usr/bin/backupdatabases
echo '' >> $backup_databases_script if ! grep -q "# Backup the ${database_name} database" $backup_databases_script; then
echo "# Backup the ${database_name} database" >> $backup_databases_script echo '' >> $backup_databases_script
echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script echo "# Backup the ${database_name} database" >> $backup_databases_script
echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script
echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script
echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script
echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script
echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script
echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script
echo '' >> $backup_databases_script echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script
echo ' # try to restore yesterdays database' >> $backup_databases_script echo '' >> $backup_databases_script
echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script echo ' # try to restore yesterdays database' >> $backup_databases_script
echo '' >> $backup_databases_script echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script
echo ' # Send a warning email' >> $backup_databases_script echo '' >> $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 ' # Send a warning email' >> $backup_databases_script
echo ' else' >> $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 ' # Send a warning email' >> $backup_databases_script echo ' else' >> $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 ' # Send a warning email' >> $backup_databases_script
echo ' fi' >> $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 'else' >> $backup_databases_script echo ' fi' >> $backup_databases_script
echo ' chmod 600 $TEMPFILE' >> $backup_databases_script echo 'else' >> $backup_databases_script
echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script echo ' chmod 600 $TEMPFILE' >> $backup_databases_script
echo '' >> $backup_databases_script echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script
echo ' # Make the backup readable only by root' >> $backup_databases_script echo '' >> $backup_databases_script
echo ' chmod 600 $DAILYFILE' >> $backup_databases_script echo ' # Make the backup readable only by root' >> $backup_databases_script
echo 'fi' >> $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 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 '' >> ${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 "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 " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_2weekly.sql" >> ${weekly_backup_script}
echo 'fi' >> ${weekly_backup_script} echo 'fi' >> ${weekly_backup_script}
echo "if [ -f /var/backups/${database_name}_daily.sql ]; then" >> ${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 " cp -f /var/backups/${database_name}_daily.sql /var/backups/${database_name}_weekly.sql" >> ${weekly_backup_script}
echo 'fi' >> ${weekly_backup_script} echo 'fi' >> ${weekly_backup_script}
echo "# End of backup for ${database_name}" >> ${weekly_backup_script}
fi fi
monthly_backup_script=/etc/cron.monthly/backupdatabasesmonthly 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 '' >> ${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 "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 " cp -f /var/backups/${database_name}_monthly.sql /var/backups/${database_name}_2monthly.sql" >> ${monthly_backup_script}
echo 'fi' >> ${monthly_backup_script} echo 'fi' >> ${monthly_backup_script}
echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${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 " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_monthly.sql" >> ${monthly_backup_script}
echo 'fi' >> ${monthly_backup_script} echo 'fi' >> ${monthly_backup_script}
echo "# End of backup for ${database_name}" >> ${monthly_backup_script}
fi fi
if ! grep -q "${database_name}" /etc/cron.hourly/repair; then if ! grep -q "${database_name}" /etc/cron.hourly/repair; then