PDA

View Full Version : Backup mysql databases script.


Crouse
05-18-2005, 04:16 PM
This script keeps 8 days worth of compressed mysql backups in /var/backups/mysql . Another good thing about this script is it backs each individual database seperately.

for i in `mysql -B -e "show databases" | tail +2`; do
mysqldump --opt $i | gzip -9 > /var/backups/mysql/$i.`date --iso-8601`.gz
ls -t /var/backups/mysql/$i.* | tail +7 | xargs rm -f
done

This is the mysql backup script that I use on our servers, worst case scenario (and it's happened), you can restore from any of the last 7 days worth of backups. Came in handy for me ;) Multiple cron jobs, and using different directories for storage, you could backup several times per day if you wanted too.

Set the script in a cron job, and your databases get backed up regularly. The mysql databases are probably the most important part of my server really, the server software itself can be restored, as can the bulk of the static html/php that doesn't change.... but the mysql data changes by the minute, so it's nice to have backups that you can rely on to restore from if you need to.