Skip to content

Instantly share code, notes, and snippets.

@barttenbrinke
Created June 5, 2013 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barttenbrinke/5714203 to your computer and use it in GitHub Desktop.
Save barttenbrinke/5714203 to your computer and use it in GitHub Desktop.
innobackupex-runner.sh
!/bin/sh
#
# Script to run innobackupex script (for all databases on server), check for success, and apply logs to backups.
#
# (C)2013 Bart ten Brinke @ Retrosync
#
# Based on the following script:
# (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
INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
TARFULL=/usr/bin/tar
PIGZFULL=/usr/bin/pigz
USEROPTIONS="--user=<OURUSER> --password=<OURUSERSPASSWORD> --parallel=2"
BACKUPDIR="<YOURBACKUPDIR>"
TMPFILE="/tmp/innobackupex-runner.$$.tmp"
# Age of oldest retained backups in minutes.
AGE=45000
# 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
# Now run the command to produce the backup; capture it's output.
echo "Check completed OK; running $INNOBACKUPEX command."
$INNOBACKUPEXFULL $USEROPTIONS $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, 0ee327ecdfedb10810811ea40cf826524c87c5afquot;'0ee327ecdfedb10810811ea40cf826524c87c5afquot; ) ; 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 $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
# Compression
echo "Creating gzipped archive of the backups"
$TARFULL czf --use-compress-program=$PIGZFULL $THISBACKUP.tar.gz $THISBACKUP > $TMPFILE 2>&1
# Cleanup
echo "Cleaning the backup dir"
rm -rf $THISBACKUP
echo "Cleaning up old backups (older than $AGE minutes) and temporary files"
rm -f $TMPFILE
cd /tmp ; find $BACKUPDIR -maxdepth 1 -type f -mmin +$AGE -exec echo "removing: "{} \; -exec rm -f {} \;
echo
echo "completed: `date`"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment