Skip to content

Back up all MySQL databases to separate files

Note to self — so I don’t have to think about it again next time.

#!/bin/bash
export MYSQL_PWD=myrootpassword
BACKUP_DIR=/mnt/backups/mysql_backups
DATE=`date -I`
for dbname in `mysql -uroot --batch --skip-column-names -e "show databases;" | grep -v information_schema`; do
	/usr/local/bin/mysqldump -u root $dbname | gzip -9 > $BACKUP_DIR/$dbname-$DATE.sql.gz
done
 
# Clear backups older than 7 days
/usr/local/bin/find $BACKUP_DIR/*.sql.gz -mtime +7 -delete

Edited to include compression. Remember to include full paths if you’re running it from a crontab, just in case.

Share/save this page:
  • email
  • Google Bookmarks
  • Twitter
  • FriendFeed
  • del.icio.us
  • Digg
  • Reddit
  • StumbleUpon
  • Technorati
  • DZone
  • Slashdot
  • Fark
  • Facebook
  • MySpace
  • LinkedIn
  • Live
  • connotea

Post a Comment

Your email is never published nor shared. Required fields are marked *