Skip to content

Instantly share code, notes, and snippets.

@ninjix
Forked from jaygooby/percona-xtrabackup.sh
Last active August 29, 2015 14:13
Show Gist options
  • Save ninjix/89a66ac62c6308b5234c to your computer and use it in GitHub Desktop.
Save ninjix/89a66ac62c6308b5234c 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 protected]
# 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="--defaults-file=/etc/mysql/debian.cnf"
# 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
# only remove if $PREV is set
if [ -n "$PREV" ]; then
# remove backups you don't want to keep
rm -rf $BACKUPDIR/$PREV
fi
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