Skip to content

Instantly share code, notes, and snippets.

@northox
Created June 12, 2013 03:01
Show Gist options
  • Save northox/5762553 to your computer and use it in GitHub Desktop.
Save northox/5762553 to your computer and use it in GitHub Desktop.
mysql live backup, no downtime via a replication and freebsd snapshot
#!/bin/sh
# @author: Danny Fullerton - Mantor Organization
error=0
errormakesnap=""
errorstop=""
errorstart=""
expr $1 + 0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "'Number of backup' argument is not integer value."
exit 1
fi
if [ $1 -gt 20 -o $1 -lt 0 ]; then
echo "'Number of backup' argument should be between 0 and 20."
exit 1
fi
num_of_backup=$1
/usr/sbin/jexec `/usr/sbin/jls | /usr/bin/grep secor | /usr/bin/awk '{print $1}'` /usr/local/etc/rc.d/mysql-server stop > /dev/null
_return=$?
# make snapshot if mysql exit normaly
# max 20 snapshot tagged 'daily' : /var/db/mysql/.snap/daily.[0-9]{1,2}
if [ $_return -ne 0 ]; then
errorstop="ERROR: mysql-server stop returned $_return"
error=1
else
/usr/local/sbin/snapshot make -g$num_of_backup /jails/secor/var/db/mysql:daily > /dev/null 2>&1
_return=$?
if [ $_return -ne 0 ]; then
sleep 600
/usr/local/sbin/snapshot make -g$num_of_backup /jails/secor/var/db/mysql:daily > /dev/null 2>&1
_return=$?
if [ $_return -ne 0 ]; then
errormakesnap="ERROR: snapshot creation returned $_return"
error=1
fi
fi
fi
/usr/sbin/jexec `/usr/sbin/jls | /usr/bin/grep secor | /usr/bin/awk '{print $1}'` /usr/local/etc/rc.d/mysql-server start > /dev/null
_return=$?
if [ $_return -ne 0 ]; then
errorstart="ERROR: mysql-server start returned $_return"
error=1
fi
# display error (cron will send an email)
if [ $error -eq 1 ]; then
echo $errorstop
echo $errormakesnap
echo $errorstart
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment