Skip to content

Instantly share code, notes, and snippets.

@pricejn2
Created August 11, 2014 00:07
Show Gist options
  • Save pricejn2/e9ff2da593a7ebb53897 to your computer and use it in GitHub Desktop.
Save pricejn2/e9ff2da593a7ebb53897 to your computer and use it in GitHub Desktop.
InnoDB database backups if changed since last backup
#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
BACKUPDIR=/data/disk/arch/sql
HOST=`uname -n`
DATE=`date +%y%m%d-0008`
HOUR=`date +%H`
LASTHOUR=`date --date="2 hours ago" +%H`
SAVELOCATION=$BACKUPDIR/$HOST-$DATE/hourly
backup_this_database () {
n=$((RANDOM%15+5))
echo waiting $n sec
sleep $n
mysqldump --opt --skip-lock-tables --order-by-primary --single-transaction --default-character-set=utf8 -Q --hex-blob $DB | gzip -c > $SAVELOCATION/$DB-$HOUR.sql.gz
}
[ ! -a $SAVELOCATION ] && mkdir -p $SAVELOCATION ;
for DB in `ls -l /var/lib/mysql/ | grep "^d" | grep -v "\." | sed 's/\/$//' | awk '{print $9}'`;
do
if [ /var/lib/mysql/$DB -nt $SAVELOCATION/$DB-$lASTHOUR.sql.gz ] ; then
if [ "$DB" != "Database" ] && [ "$DB" != "information_schema" ] && [ "$DB" != "performance_schema" ] ; then
backup_this_database &> /dev/null
echo "Backup completed for $DB"
echo " "
fi
fi
done
exit 0
###EOF2014###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment