Created
August 11, 2014 00:07
-
-
Save pricejn2/e9ff2da593a7ebb53897 to your computer and use it in GitHub Desktop.
InnoDB database backups if changed since last backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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