Skip to content

Instantly share code, notes, and snippets.

@gene1wood
Created July 24, 2018 15:11
Show Gist options
  • Save gene1wood/1e12797e9b1d44655305d14ce2bf40e5 to your computer and use it in GitHub Desktop.
Save gene1wood/1e12797e9b1d44655305d14ce2bf40e5 to your computer and use it in GitHub Desktop.
Script that exports a MySQL database so it can be backed up
#!/bin/bash
#
# A script to create a daily mysqldump for crashplan
# Defaults
# Override them in /etc/default/mysqlbackupforcrashplan
OUTPUTFILE=/var/local/full-backup.sql
STATEFILE=/var/local/full-backup.sql.state
CRONFILE=/etc/cron.d/mysqlbackupforcrashplan
MYSQLDUMPBIN=/usr/bin/mysqldump
MAXAGE=2 #days
CRONHOUR=4
CRONMINUTE=10
[email protected]
MAILBIN=/usr/bin/mail
if [ -f /etc/default/mysqlbackupforcrashplan ] ; then
. /etc/default/mysqlbackupforcrashplan
fi
if [ ! -e "$MAILBIN" ]; then
echo "Can't fine $MAILBIN"
exit 1
fi
if [ ! -e "$CRONFILE" ]; then
echo "# This file is autogenerated by $0" >> $CRONFILE
echo "$CRONMINUTE $CRONHOUR * * * root `readlink -e \"$0\"`" >> $CRONFILE
fi
if [ -e "$STATEFILE" ]; then
if [ "`find "$STATEFILE" -mtime +$MAXAGE`" ]; then
# STATEFILE is over MAXAGE days old
if [ "$MAILADDR" ]; then
echo -e "WARNING : Last mysqldump on `hostname -f` was on `stat -c %y \"$STATEFILE\"`\n`ls -al \"$OUTPUTFILE\"`" | mail -s "WARNING : `hostname -f` mysqldump exceeds max age of $MAXAGE days" $MAILADDR
fi
fi
else
echo "Running for the first time"
fi
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
ORIGINAL_UMASK="`umask`" && umask 177 # Make output files 600
if MYSQLDUMPSTDERR="`$MYSQLDUMPBIN --defaults-file=/etc/mysql/debian.cnf --all-databases --single-transaction --events --ignore-table=mysql.event 2>&1 > \"$OUTPUTFILE\"`"; then
touch "$STATEFILE"
else
echo -e "WARNING : mysqldump on `hostname -f` experienced and error of \"$MYSQLDUMPSTDERR\"" | mail -s "WARNING : `hostname -f` mysqldump experienced an error" $MAILADDR
fi
umask $ORIGINAL_UMASK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment