Skip to content

Instantly share code, notes, and snippets.

@troex
Created September 27, 2011 15:20
Show Gist options
  • Save troex/1245354 to your computer and use it in GitHub Desktop.
Save troex/1245354 to your computer and use it in GitHub Desktop.
Backup all mysql databases to separate files
#!/bin/sh
# echo "11 5 * * * root /etc/backup-sql.sh | logger -t backup-sql" >> /etc/cron.d/backup
P="/var/www/sql_dump/"
MO="--skip-lock-tables"
MO="$MO --single-transaction" # for InnoDB
GZIP="gzip --rsyncable"
GZIP_ON_THE_FLY=true
SCOPE=`date +%u` # weekly rotation
echo "Dumping databases to ${P}"
for DB in `mysql -N -B -e 'show databases'`
do
[ "${DB}" = "information_schema" ] && continue
[ "${DB}" = "performance_schema" ] && continue
FILE="${P}${DB}-${SCOPE}.sql"
rm -f $FILE $FILE.gz
echo " -> ${DB}"
if [ $GZIP_ON_THE_FLY ]; then
mysqldump $MO $DB | $GZIP > $FILE.gz
else
mysqldump $MO $DB > $FILE
$GZIP $FILE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment