Created
August 25, 2010 07:49
-
-
Save somebox/549062 to your computer and use it in GitHub Desktop.
Simple script that backs up all MySQL user databases separately, with dated filenames
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 | |
# Dumps all databases to separate backup files named with today's date | |
BACKUP_TO="$HOME/backup/db" | |
MYSQLDUMP_OPTIONS="--compact --add-drop-table --skip-set-charset" | |
# --- | |
mkdir -p $BACKUP_TO | |
DATE=`date +'%Y-%m-%d'` | |
mysql -e 'show databases' | ruby -e " | |
to_skip = ['database','information_schema'] | |
STDIN.read.each do |db| | |
db.chop! | |
if !to_skip.include?(db.downcase) | |
puts %Q(backing up #{db}) | |
cmd = %Q(mysqldump $MYSQLDUMP_OPTIONS #{db}) | |
file = %Q($BACKUP_TO/#{db}_$DATE.sql) | |
system %Q(#{cmd} > #{file}) | |
end | |
end | |
" | |
# remove old backups | |
/usr/bin/find $BACKUP_TO -mtime +90 -type f -exec /bin/rm '{}' \; | |
# remove zero-length backups | |
/usr/bin/find $BACKUP_TO -size 0 -type f -exec /bin/rm '{}' \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment