Scheduling mySQL backups

I’m sure there is no need to explain the virtues of regular backups. Below are three steps to peace of mind

1) Configure a backup directory on the server

mkdir /var/lib/backupfolder
cd /var/lib/backupfolder

2) Make a backup script

vi mysqlbackup.sh

#!/bin/sh
# MySQL server username goes here
USERNAME="username"
# MySQL server password goes here
PASSWORD="password"
# List of DBNAMES for Backup
DBNAME="dbname"
#date timestamp used in the log
DATE=`/bin/date +%Y-%m-%d_%Hh%Mm`
# format the output file
OUTDIR="/var/lib/backupfolder/"
OUTFILE="ip_bindass".$DATE."sql.gz"
#working directory
DIR="/var/lib/backupfolder/"
#cd $DIR
# Do the MySQL Backup
/usr/bin/mysqldump --database $DBNAME --opt --single-transaction -u$USERNAME -p$PASSWORD | /usr/bin/gzip -9 > $OUTDIR$OUTFILE

Dont forget to change permissions

chmod +x mysqlbackup.sh

3) Schedule the backup in crontab

crontab -e

Add something along the lines of:

30 23 * * * /var/lib/backupfolder/mysqlbackup.sh

To backup at 11:30PM every day. Just as important as to backup up is to test restores; – it’s no good having backups and feeling at ease if you cant recover in the event of catastrophe!

Leave a Comment

Your email address will not be published. Required fields are marked *