Skip to content

Instantly share code, notes, and snippets.

@gottaloveit
Forked from jmfederico/run-xtrabackup.sh
Last active August 29, 2015 14:08
Show Gist options
  • Save gottaloveit/de9dd178732fc6eea889 to your computer and use it in GitHub Desktop.
Save gottaloveit/de9dd178732fc6eea889 to your computer and use it in GitHub Desktop.
Updated options to make it able to run automatically, non interactive, suitable for daily or even multiple per day MySQL backups using cron, and Percona's Xtrabackup. Option to backup slave replication also, and amount of days to keep backups on the disk or wherever you set the path. Easy options at the top of the file, set them, set cron, and y…
#!/bin/sh
#can change to /tmp, /var has tons of space on our servers
TMPFILE="/var/tmp/innobackupex-runner.$$.tmp"
MYSQL_USER=XXX
MYSQL_PASS=XXX
BACKDIR=/var/backups/mysql
FULL_BACKUP_DIRNAME=full
INCR_BACKUP_DIRNAME=incr
INCLUDE_SLAVE_INFO=yes # or no
# the below value is for how often to create a new full backup (in seconds)
# 3600 = every hour (make sure and set cron accordingly)
# 86400 = 24 hours
# 604800 = 7 days
FULLBACKUPLIFE=604800
# Keep this number of backups, appart form the one currently being incremented
KEEP=1
PARALLEL_THREADS=4
# No need to edit anything below
USEROPTIONS="--user=$MYSQL_USER --password=$MYSQL_PASS"
MYSQLUSEROPTIONS="-u $MYSQL_USER -p$MYSQL_PASS"
BASEBACKDIR=$BACKDIR/$FULL_BACKUP_DIRNAME
INCRBACKDIR=$BACKDIR/$INCR_BACKUP_DIRNAME
if [ "$INCLUDE_SLAVE_INFO" = "yes" ]
then
SLAVE="--slave-info --safe-slave-backup"
else
SLAVE=""
fi
EXTRA_OPTS="$SLAVE --parallel=$PARALLEL_THREADS"
START=`date +%s`
echo "----------------------------"
echo
echo "run-innobackupex.sh: MySQL backup script"
echo "started: `date`"
echo
for i in $INCRBACKDIR $BASEBACKDIR;do mkdir -p $i;done
# Check base dir exists and is writable
if test ! -d $BASEBACKDIR -o ! -w $BASEBACKDIR
then
error
echo $BASEBACKDIR 'does not exist or is not writable'; echo
exit 1
fi
# check incr dir exists and is writable
if test ! -d $INCRBACKDIR -o ! -w $INCRBACKDIR
then
error
echo $INCRBACKDIR 'does not exist or is not writable'; echo
exit 1
fi
if [ -z "`mysqladmin $MYSQLUSEROPTIONS status | grep 'Uptime'`" ]
then
echo "HALTED: MySQL does not appear to be running."; echo
exit 1
fi
if ! `echo 'exit' | /usr/bin/mysql -s $MYSQLUSEROPTIONS`
then
echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)"; echo
exit 1
fi
echo "Check completed OK"
# Find latest backup directory
LATEST=`find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
AGE=`stat -c %Y $BASEBACKDIR/$LATEST`
if [ "$LATEST" -a `expr $AGE + $FULLBACKUPLIFE + 5` -ge $START ]
then
echo 'New incremental backup'
# Create an incremental backup
# Check incr sub dir exists
# try to create if not
if test ! -d $INCRBACKDIR/$LATEST
then
mkdir $INCRBACKDIR/$LATEST
fi
# Check incr sub dir exists and is writable
if test ! -d $INCRBACKDIR/$LATEST -o ! -w $INCRBACKDIR/$LATEST
then
echo $INCRBASEDIR 'does not exist or is not writable'
exit 1
fi
LATESTINCR=`find $INCRBACKDIR/$LATEST -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`
if [ ! $LATESTINCR ]
then
# This is the first incremental backup
INCRBASEDIR=$BASEBACKDIR/$LATEST
else
# This is a 2+ incremental backup
INCRBASEDIR=$LATESTINCR
fi
# Create incremental Backup
innobackupex $USEROPTIONS $EXTRA_OPTS --incremental $INCRBACKDIR/$LATEST --incremental-basedir=$INCRBASEDIR > $TMPFILE 2>&1
else
echo 'New full backup'
# Create a new full backup
innobackupex $USEROPTIONS $EXTRA_OPTS $BASEBACKDIR > $TMPFILE 2>&1
fi
if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ]
then
echo "$INNOBACKUPEX failed:"; echo
echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
cat $TMPFILE
rm -f $TMPFILE
exit 1
fi
THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`
echo "Databases backed up successfully to: $THISBACKUP"
echo
MINS=$(($FULLBACKUPLIFE * ($KEEP + 1 ) / 60))
echo "Cleaning up old backups (older than $MINS minutes) and temporary files"
# Delete tmp file
rm -f $TMPFILE
# Delete old bakcups
for DEL in `find $BASEBACKDIR -mindepth 1 -maxdepth 1 -type d -mmin +$MINS -printf "%P\n"`
do
echo "deleting $DEL"
rm -rf $BASEBACKDIR/$DEL
rm -rf $INCRBACKDIR/$DEL
done
SPENT=$(((`date +%s` - $START) / 60))
echo
echo "took $SPENT minutes"
echo "completed: `date`"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment