Wednesday, June 10, 2009

(Semi) Secure Wrapper Script for mysqldump

I resurrected this script to do the backup of a client's MySQL database. I run it as a cron job once a day and keep the dump file on a separate server. The MySQL login info is kept in the mysql_access file which I have chmoded to be read-only for me. (chmod 400 mysql_access).

#!/bin/sh

#for debugging
#set -x

TODAY=$(date +"%D - %T")
HOST=$(hostname)
LOGFILE="${HOME}/TFP/logs/backup.log"
SHORTDATE=$(date +"%G%m%d")
DUMPFILE=${HOME}/TFP/dumps/tfp_${SHORTDATE}.sql

source ${HOME}/bin/mysql_access

echo "" >> $LOGFILE
echo "-------------------------------------------" >> $LOGFILE
echo "Date: $TODAY Host:$HOST" >> $LOGFILE
echo "-------------------------------------------" >> $LOGFILE
echo "" >> $LOGFILE

echo "Dumping $MYSQL_DB at $MYSQL_HOST to $DUMPFILE" >> $LOGFILE
mysqldump -h $MYSQL_HOST -u $MYSQL_USER $MYSQL_DB -p$MYSQL_PASSWORD > $DUMPFILE

echo "" >> $LOGFILE
WCSIZE=$(wc $DUMPFILE)
echo "wc for $DUMPFILE is" >> $LOGFILE
echo " $WCSIZE" >> $LOGFILE

echo "" >> $LOGFILE
FILESIZE=$(ls -lh $DUMPFILE)
echo "filesize for $DUMPFILE is" >> $LOGFILE
echo " $FILESIZE" >> $LOGFILE


The contents of the mysql_access include:

MYSQL_USER="some_username"
MYSQL_PASSWORD="password"
MYSQL_HOST="mysql.somedomain.com"
MYSQL_DB="some_database"


My hosting company sends me email with the output of the cron job. I include the size of the dump file in the output so I can just keep a running check on the output in case anything gets out of control.

Comments?