Skip to content

Instantly share code, notes, and snippets.

@mnathani
Created October 11, 2015 11:11
Show Gist options
  • Save mnathani/ebea31cf5be750c46f68 to your computer and use it in GitHub Desktop.
Save mnathani/ebea31cf5be750c46f68 to your computer and use it in GitHub Desktop.
tarsnap database backup cron script
#!/bin/bash
# This script backups every MySQL database to its own file
#Some variables you can set how you like
USER='root'
PASSWORD='X'
OUTPUTDIR='/home/backup/butterfly-sql'
MYSQLDUMP="$(which mysqldump)"
MYSQL="$(which mysql)"
TARSNAP="$(which tarsnap)"
#Clean up any old backups
rm -f $OUTPUTDIR/*
#Get a list of databases names except the system one
databases=`$MYSQL --user=$USER --password=$PASSWORD -e 'SHOW DATABASES;' | grep -Ev '(Database|information_schema|mysql|cphulkd|eximstats|horde|leechprotect|logaholicDB|performance_schema|roundcube|whmxfer)'`
#Dump each database in turn and compress the output
for db in $databases; do
$MYSQLDUMP --opt --force --user=$USER --password=$PASSWORD $db > $OUTPUTDIR/$db"_"`date +%Y-%m-%d`.sql
done
$TARSNAP -c -f butterfly-mysql_`date +%F_%H-%M-%S` $OUTPUTDIR
#rm -f $OUTPUTDIR/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment