Script: Amazon EC2 MySQL Backup Toolkit

Tag: 

#!/bin/bash
#
#  Amazon EC2 MySQL Backup Toolkit
#
#  do_mysql_backup v1.0 - David O'Dwyer
#
# Basic wrapper for EBS snapshots / mylvmbackup.
# Assumes ec2-api-bin and mylvmbackup installed
# and MySQL data directory located on LVM partition
#
########################################
 
#  Setup site variables
# 
# Backup destination
DIR=/data/backups/mysql
 
# Amount of days before cycling backups
ARCHLIMIT=14
 
#  Set to the volumeid of the EBS device hosting your MySQL data directory
VOLUMEID=vol-xxxxxxxxx
 
#  Set to the LVM directory holding MySQL data
DATADIR=/data
 
#  MySQL cnf to obtain credentails from
CNF=/root/.my.cnf
 
# LVM volume group / name 
VGPATH=/dev/vg
VGNAME=myvmdisk1
 
#  Location of ec2-api ec2-create-snapshot
SNAPBIN=/usr/local/bin/ec2-create-snapshot
 
#  Setup date variables
TODAY=`date +'%Y%m%d'`
PAST=`date --date="$ARCHLIMIT days ago" +'%Y%m%d'`
 
#  Setup EC2 variables
export EC2_HOME=/root/.ec2
export EC2_PRIVATE_KEY=/root/key.pem
export EC2_CERT=/root/cert.pem
export JAVA_HOME=/usr
 
#  Make directories
mkdir -p $DIR/today
mkdir -p $DIR/current
mkdir -p $DIR/archive/$TODAY
 
#  Archive todays backup files
rotate_backup_files() {
        for i in $(find $DIR/today -name '*tar.gz')
                do
                        mv $i $DIR/archive/$TODAY/
        done
 
                rm -rf $DIR/archive/$PAST
}
 
#  Symlink latest backup file to current
symlink_current_backup_file() {
        current=`find $DIR/today -maxdepth 1 -name 'backup*' | sort | tail -n1`
        ln -f $current $DIR/current/current-mysql-backup.tar.gz
}
 
#  Initiate mylvmbackup then symlink
mylvmbackup_init() {
        mylvmbackup
        symlink_current_backup_file
 
}
 
ec2_snapshot_data_dir () {
 
        user=`grep user= $CNF | cut -f 2 -d =`
        password=`grep password= $CNF | cut -f 2 -d =`
 
        flush_cmd="FLUSH TABLES WITH READ LOCK"
        mysql -u $user -p$password -e "$flush_cmd"
 
        slave_cmd="SHOW SLAVE STATUS\G"
        mysql -u $user -p$password -e "$slave_cmd" > $DATADIR/mysql.backup.pos
 
        lvcreate -s --size=1G --name=backup-mysql $VGPATH/$VGNAME
 
        unlock_cmd="UNLOCK TABLES"
        mysql -u $user -p$password -e "$flush_cmd"
 
        $SNAPBIN $VOLUMEID
        lvremove -f $VGPATH/backup-mysql
 
}
 
reattach_ebs_lvm () {
        mysqladmin shutdown
        umount $DATADIR
        vgscan
        vgchange -ay
        mount $VGPATH/$VGNAME $DATADIR
        /etc/init.d/mysql start
 
}
 
case $1 in
        rotate)
 
                echo "Rotating files up to $ARCHLIMIT days...."
                rotate_backup_files ; echo "Done"
 
        exit
        ;;
 
        symlink)
 
                echo "Creating symlink...."
                symlink_current_backup_file ; echo "Done."
 
        exit
        ;;
 
        backup)
 
                echo "Starting mylvmbackup script...."
                mylvmbackup_init ; echo "Done."
 
        exit
        ;;
 
        snapshot)
 
                echo "Creating snapshot of $DATADIR to S3"
                ec2_snapshot_data_dir ; echo "Done."
 
        exit
        ;;
 
        reattach)
 
                echo "Reattaching EBS to $DATADIR ...."
                reattach_ebs_lvm ; echo "Done."
 
        exit
        ;;
 
        cron)
 
                echo "Nightly CRON run...."
                ec2_snapshot_data_dir ; echo "snapshot Done."
                mylvmbackup_init ; echo "backup Done."
                symlink_current_backup_file ; echo "symlink Done."
                rotate_backup_files ; echo "rotate Done"
 
        exit
        ;;
 
 
esac
 
echo "Usage: $0 [rotate|symlink|backup|snapshot|cron]"