Can anyone help me with some questions about backup scripts?
 
I've written a bash script to take database-level backups of each of the databases in our copy of MySQL. We are running MySQL 4.0.15 on Linux Mandrake 9.1 and using a mix of MyISAM and InnoDB databases. This is my script, with the userid and password faked for obvious reasons:
 
#------------------------------------------------------------------------------------------------------
#!/bin/bash
 
#This script makes a separate database-level backup of each of the current MySQL databases and
#deletes backups older than a certain number of days.
#This script is normally invoked via a cron job so that it runs once per day in the middle of the night.
#The crontab entry looks like this:
#0 3 * * * sh /home/foo/MySQL/backup.bash > /home/foo/MySQL/backup.out 2>&1
 
DEBUG=1; #DEBUG switch
USERID="foo"; #The userid to use for creating the backup
PASSWORD="bar"; #The password to use for creating the backup
BACKUP_TIMESTAMP=`/bin/date +%Y%m%d"-"%H%M%S`; #The timestamp (YYYYMMDD-HHMMSS) of the backup
BACKUP_PATH="/home/foo/MySQL/backup"; #The directory into which the backup will be written
NUMBER_OF_DAILY_BACKUPS_TO_KEEP=7; #The number of generations of backups to keep
 
#Display the non-secret values used in this run.
if test $DEBUG -eq 1
then
   echo "BACKUP VALUES:";
   echo "  Backup timestamp is" $BACKUP_TIMESTAMP;
   echo "  Backup path is" $BACKUP_PATH;
   echo "  Number of daily backups to keep =" $NUMBER_OF_DAILY_BACKUPS_TO_KEEP;
   fi
 
#For each database currently in MySQL, take a database-level backup, then list any backups older than a certain number of days, then delete these old backups
for ONE_DBNAME in `echo show databases | mysql -s -u $USERID -p$PASSWORD`
do
   echo
   echo "Backing up database" $ONE_DBNAME;
   /usr/bin/mysqldump --opt --verbose -u${USERID} -p${PASSWORD} ${ONE_DBNAME} -r ${BACKUP_PATH}"/"${ONE_DBNAME}"."${BACKUP_TIMESTAMP}".sql"
   echo ">> Deleting these old backups for this database..."
   /usr/bin/find ${BACKUP_PATH} -mtime +$NUMBER_OF_DAILY_BACKUPS_TO_KEEP -name $ONE_DBNAME'*' -print; #display old backups (if any)
   /usr/bin/find ${BACKUP_PATH} -mtime +$NUMBER_OF_DAILY_BACKUPS_TO_KEEP -name $ONE_DBNAME'*' -exec rm '{}' ';'; #delete old backups (if any)
done
#---------------------------------------------------------------------------------------------------------
 
The script seems to work just fine at the moment but I'm concerned that it isn't equipped to handle errors very well. Unfortunately, I'm not very clear on exactly what sort of errors *can* occur when doing backups, let alone how they can be handled. Can anyone tell me from their own experience what errors we are likely to encounter in doing mysqldumps and how we can recognize them when they happen? Also, since I'm pretty new to bash, I'd appreciate some hints on how to catch errors with a bash script. For instance, if we encounter a full disk while writing the backup, what sort of error will we get and how will bash know what the error was so that it can tell the user of the script?
 
In a nutshell, I'd like to generate a simple email in any case where the backup doesn't work correctly and send it to the appropriate administrators so that they can take action. The first action they would take is to look at the output from the script (which is redirected to a file) so the email should contain the name of that file.

Rhino
---
rhino1 AT sympatico DOT ca
"If you want the best seat in the house, you'll have to move the cat."

Reply via email to