Skip to content

Instantly share code, notes, and snippets.

@markstreich
Forked from dalecaru/innobackupex-restore.sh
Created November 23, 2011 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markstreich/1387793 to your computer and use it in GitHub Desktop.
Save markstreich/1387793 to your computer and use it in GitHub Desktop.
Script to run innobackupex script (for all databases on server), check for success, and apply logs to backups.
#!/bin/sh
#
# Script to run innobackupex script (for all databases on server), check for success, and apply logs to backups.
#
# (C)2010 Owen Carter @ Mirabeau BV
# This script is provided as-is; no liability can be accepted for use.
# You are free to modify and reproduce so long as this attribution is preserved.
#
INNOBACKUPEX=innobackupex-1.5.1
INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
USEROPTIONS="--user=root --password=XXXXXX"
BACKUPDIR=/backup
TMPFILE="/tmp/innobackupex-runner.$$.tmp"
# Age of oldest retained backups in minutes.
AGE=5000
# Some info output
echo "----------------------------"
echo
echo "innobackupex-runner.sh: MySQL backup script"
echo "started: `date`"
echo
# Check options before proceeding
if [ ! -x $INNOBACKUPEXFULL ]; then
error
echo "$INNOBACKUPEXFULL does not exist."; echo
exit 1
fi
if [ ! -d $BACKUPDIR ]; then
error
echo "Backup destination folder: $BACKUPDIR does not exist."; echo
exit 1
fi
if [ -z "`/sbin/service mysql status | grep 'MySQL running (.*)'`" ] ; then
echo "HALTED: MySQL does not appear to be running."; echo
exit 1
fi
if ! `echo 'exit' | /usr/bin/mysql -s $USEROPTIONS` ; then
echo "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)"; echo
exit 1
fi
# Now run the command to produce the backup; capture it's output.
echo "Check completed OK; running $INNOBACKUPEX command."
$INNOBACKUPEXFULL $USEROPTIONS --defaults-file=/etc/my.cnf $BACKUPDIR > $TMPFILE 2>&1
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`
rm -f $TMPFILE
echo "Databases backed up successfully to: $THISBACKUP"
echo
echo "Now applying logs to the backuped databases"
# Run the command to apply the logfiles to the backup directory.
$INNOBACKUPEXFULL --apply-log --use-memory=1024 --defaults-file=/etc/my.cnf $THISBACKUP > $TMPFILE 2>&1
if [ -z "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
echo "$INNOBACKUPEX --apply-log failed:"; echo
echo "---------- ERROR OUTPUT from $INNOBACKUPEX --apply-log ----------"
cat $TMPFILE
rm -f $TMPFILE
exit 1
fi
echo "Logs applied to backuped databases"
echo
# Cleanup
echo "Cleaning up old backups (older than $AGE minutes) and temporary files"
rm -f $TMPFILE
cd /tmp ; find $BACKUPDIR -maxdepth 1 -type d -mmin +$AGE -exec echo "removing: "{} \; -exec rm -rf {} \;
echo
echo "completed: `date`"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment