Backup scripts for MySQL

I just created a generic backup script to backup selected MySQL databases. It uses the mysqlhotcopy script that comes with MySQL. It's not all that great, but in case it's useful to SOMEBODY, I thought I'd post it here. This example just backs up the mysql database itself, but obviously you could just change the DBNAME to back up some other database.

You'll probably want to edit the ultimate destination of the backups. I put them in /usr/backup.

-----------------------------------------

#!/bin/sh

# Custom backup script by Ben
#
# This will back up the selected databases and gzip the results. It will then
# rotate the results to keep 4 weeks' worth of backups on hand.

DBNAME=mysql

if [ -x /usr/bin/mysqlhotcopy ]
then

   # Create copy of database files in /var/lib/_old
   mysqlhotcopy $DBNAME --allowold --quiet

   # tarball it to a temp file
   cd /var/lib/mysql
   tar -czf /usr/backup/$DBNAME.tar.gz.temp $DBNAME/

   # If the tarball above got created,
   # rotate the old backups -- keep only the last 4 weeks
   if [ -s /usr/backup/$DBNAME.tar.gz.temp ]
   then
        savelog -l -q -c 4 /usr/backup/$DBNAME.tar.gz
        mv /usr/backup/$DBNAME.tar.gz.temp /usr/backup/$DBNAME.tar.gz
   fi
fi