I'm trying to get a little more usefulness out of my MySQL backups. I'm wanting to gzip my backup when it's completed from a cron job. Here's the script I have so far to do the backup.
#!/bin/sh
date=`date -Iminutes`
mysqldump --all-databases > /var/sqlbackup/sqlbackup-$date.sql -pmypassword
find /var/sqlbackup/ -mtime 3 | xargs rm
Any help would be beneficial, even if it's a pointer on how to do it better.
Here's a backup/maintenance script I use:
This script generates an individual gzipped backup of each database so that you don't have to restore the entire server's databases if there's just a problem with one DB. It also includes some mysql check sanity and finds old database backups and deletes them.
To restore, as requested:
I've had to restore a couple of times. It happens.
There's probably a more 'one line' way of doing it... but that's how it's worked for me.
You can pipe to
gzip
to compress.I moved the find to before the mysqldump. I'm not sure how big your databases are, but it'll help to keep disk usage down and avoid any space issues.
It's usually better to use
-mtime +2
instead of-mtime 3
to affect anything older than two days rather than files exactly 3 days old. This way if your cron misses a day, you don't get older files missed by the delete.Personally I prefer to restrict
find
to a name pattern, too, to make sure you aren't deleting anything unexpectedly.The
-delete
option in find is helpful if you don't want to usexargs
. Both ways will work, but I prefer to use fewer commands when possible.You can just add
gzip /var/sqlbackup/sqlbackup-$date.sql
to the end of your script.Try this:
Have you ever looked into irsync by R-FX networks?
A great little script that will allow you to take hot copies and gzips of your databases and send it to a remote server. :)