Skip to content

Instantly share code, notes, and snippets.

@valkum
Last active August 14, 2016 20:06
Show Gist options
  • Save valkum/2de895909d6aec48f7a4 to your computer and use it in GitHub Desktop.
Save valkum/2de895909d6aec48f7a4 to your computer and use it in GitHub Desktop.
Use innobackupex with backupninja. Place under /usr/share/backupninja/innobackupex
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
#
# duplicity script for backupninja
# requires duplicity >= 0.4.4, and >= 0.4.9 when using a custom tmpdir.
#
######################################
# backupdir: Where to backup to
# incremantal: yes/np crate incremental backups
# full_on: Iso 3 Char Day of Week on which a new full backip should be made.
# max_age: Max Age in days of how long a backup is stored.
# nicelevel: nice lebel
# options: custom options to pass
##############################
getconf basebackupdir /var/backups/mysql/base
getconf incrbackupdir /var/backups/mysql/incr
getconf incremental yes
getconf full_on mon
getconf max_age 14
getconf nicelevel 0
getconf options
# authentication:
getconf user
getconf dbusername
getconf dbpassword
getconf configfile /etc/mysql/debian.cnf
### COMMAND-LINE MANGLING ######################################################
### initialize $execstr*
execstr_precmd=
execstr_command=
options="$options --user=$user --password=$dbpassword"
day_of_week=$(date +%a)
# Check base dir exists and is writable
if test ! -d $basebackupdir -o ! -w $basebackupdir
then
fatal "$basebackupdir does not exist or is not writable"
fi
# check incr dir exists and is writable
if test ! -d $incrbackupdir -o ! -w $incrbackupdir
then
echo "$incrbackupdir does not exist or is not writable"
fi
if [ -z "`mysqladmin $USEROPTIONS status | grep 'Uptime'`" ]
then
fatal "HALTED: MySQL does not appear to be running."
fi
debug "Check completed OK"
###Check for empty backupdir, if so force full backup
if [ "$(ls -A $basebackupdir)" ]; then
incremental=$incremental
else
incremental="no"
fi
latest=`find $basebackupdir -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`
### Incremental or full backup mode
# If incremental==yes, use the default duplicity behaviour: perform an
# incremental backup if old signatures can be found, else switch to
# full backup.
# If incremental==no, force a full backup anyway.
if [ "$incremental" == "no" ]; then
execstr_command="$basebackupdir"
else
if [ "${day_of_week,,}" == "$full_on" ] ; then
execstr_command="$basebackupdir"
else
latestincr=`find $incrbackupdir/$latest -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`
if [ ! $latestincr ]
then
# This is the first incremental backup
incrbasedir=$basebackupdir/$latest
else
# This is a 2+ incremental backup
incrbasedir=$latestincr
fi
execstr_command="--incremental $incrbackupdir/$latest --incremental-basedir=$incrbasedir"
fi
fi
### Backup command
debug "$execstr_precmd innobackupex $options $execstr_command"
if [ ! $test ]; then
outputfile=`maketemp backupout`
output=`nice -n $nicelevel \
su -c \
"$execstr_precmd innobackupex $options $execstr_command >$outputfile 2>&1"`
exit_code=$?
debug $output
cat $outputfile | (while read output ; do
if [ $exit_code -eq 0 ]; then
info $output
else
error $output
fi
done
)
if [ $exit_code -eq 0 ]; then
info "innobackupex finished successfully."
else
fatal "innobackupex failed."
fi
rm $outputfile
# Delete old bakcups
info "Cleanup"
for DEL in `find $basebackupdir -mindepth 1 -maxdepth 1 -type d -mtime +$max_age -printf "%P\n"`
do
echo "deleting $DEL"
rm -rf $basebackupdir/$DEL
rm -rf $incrbackupdir/$DEL
done
fi
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment