Skip to content

Instantly share code, notes, and snippets.

@bryden
Forked from jmfederico/run-xtrabackup.sh
Last active May 31, 2017 14:42
Show Gist options
  • Save bryden/415310be7b478ad01fad to your computer and use it in GitHub Desktop.
Save bryden/415310be7b478ad01fad to your computer and use it in GitHub Desktop.
Script to create full/incremental backups with innobackupex script.
#!/bin/bash
USER="root"
PASS="root"
BACKDIR="mysql-bak"
BASEBACKDIR="$BACKDIR/base"
INCRBACKDIR="$BACKDIR/incr"
FULLBACKUPLIFE=3600
START=`date +%s`
TMPFILE="$$.sql"
echo "Backup script commencing..."
if test ! -d $BASEBACKDIR -o ! -w $BASEBACKDIR
then
error
echo "$BASEBACKDIR does not exist or is not writable"; echo
exit 1
fi
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 -u $USER --password=$PASS status | grep 'Uptime'`" ]
then
echo "HALTED: MySQL does not appear to be running."; echo
exit 1
fi
echo "Setup check completed fantastically. Good work."
echo "Checking for latest backup file..."
LATEST=`find $BASEBACKDIR -mindepth 1 -maxdepth 1 -printf "%P\n" | sort -nr | head -1`
AGE=`stat -c %Y $BASEBACKDIR/$LATEST`
echo "$AGE"
if [ "$LATEST" -a `expr $AGE + $FULLBACKUPLIFE + 5` -ge $START ]
then
echo 'New incremental backup'
# Create an inremental 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
mysqlbackup -u $USER -p $PASS --incremental
else
echo "New full backup"
# Create a new full backup
echo "Saving to $BASEBACKDIR/$TMPFILE"
mysqldump -u $USER --password=$PASS webholistics > $BASEBACKDIR/$TMPFILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment