Skip to content

Instantly share code, notes, and snippets.

@caot
Forked from jaygooby/percona-xtrabackup.sh
Created January 14, 2014 18:21
Show Gist options
  • Save caot/8423065 to your computer and use it in GitHub Desktop.
Save caot/8423065 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Put me in cron.daily, cron.hourly or cron.d for your own custom schedule
# Running daily? You'll keep 3 daily backups
# Running hourly? You'll keep 3 hourly backups
NUM_BACKUPS_TO_KEEP=3
# Who wants to know when the backup failed, or
# when the binary logs didn't get applied
EMAIL=you@example.com
# Where you keep your backups
BACKUPDIR=/ebs/mysql/backups
# path to innobackupex
XTRABACKUP=/usr/bin/innobackupex
# Add any other files you never want to remove
NEVER_DELETE="lost\+found|\.|\.."
# The mysql user able to access all the databases
OPTIONS="--user=root"
# Shouldn't need to change these...
APPLY_LOG_OPTIONS="--apply-log"
BACKUP="$XTRABACKUP $OPTIONS $BACKUPDIR"
APPLY_BINARY_LOG="$XTRABACKUP $OPTIONS $APPLY_LOG_OPTIONS"
PREV=`ls -rt $BACKUPDIR | tail -n $((NUM_BACKUPS_TO_KEEP+1)) | head -n1 | egrep -v $NEVER_DELETE`
# run a backup
$BACKUP
if [ $? == 0 ]; then
# we got a backup, now we need to apply the binary logs
MOST_RECENT=`ls -rt $BACKUPDIR | tail -n1`
$APPLY_BINARY_LOG $BACKUPDIR/$MOST_RECENT
if [ $? == 0 ]; then
# remove backups you don't want to keep
rm -rf $BACKUPDIR/$PREV
else
echo "Couldn't apply the binary logs to the backup $BACKUPDIR/$MOST_RECENT" | mail $EMAIL -s "Mysql binary log didn't get applied to backup"
fi
else
# problem with initial backup :(
echo "Couldn't do a mysql backup" | mail $EMAIL -s "Mysql backup failed"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment