views:

100

answers:

3

What is a command line I can use to back up a MySql database every single week into a file name with the date (so that it doesn't collide with previous backups)?

Also, is this a reasonable backup strategy? My database is relatively small (a complete export is only 3.2 megs right now). The churn rate is relatively low. I need to be able to get the complete DB back if something goes wrong. And it would be extra cool if there's a way that I could see the changes that occur across a time span.

A: 

You could try MySQL Replication

Replication enables data from one MySQL database server (the master) to be replicated to one or more MySQL database servers (the slaves). Replication is asynchronous - slaves need not to connected permanently to receive updates from the master. This means that updates can occur over long-distance connections and even over temporary or intermittent connections such as a dial-up service.

This way, if something goes wrong with your primary database (the master), the slave can serve as a ready-to-use replacement.

Bakkal
Replication != backup
OMG Ponies
I am not concerned right now with the database itself being down. I am concerned with data loss or inadvertent editing by users.
KevinM
Totally agree. This was my think-out-of-the-box shot at "I need to be able to get the complete DB back if something goes wrong." I think replication does this and fast.
Bakkal
+2  A: 

You could try something like this:

0 4 1 * * DATE=`date +%y-%m-%d`; mysqldump -u <dbuser> -p<dbpasswd> <dbname> | gzip > dbname-$DATE.sql.gz

Which will dump your database (and gzip it) on the first of each month at 4:00 AM.

timdev
+1  A: 

Weekly backup with cron and mysqldump:

Credit goes to @timdev answer (+1) for providing the command line.

The cron syntax goes like this :

* * * * * = [minute] [hour] [day of month] [month] [day of week]

However since you wanted weekly backups, you can specify the days on which to run the backup in the third start above (day of month), to simulate the weekly interval.

0 4 1,8,15,21,28 * * DATE=`date +%y-%m-%d`; mysqldump -u <dbuser> -p<dbpasswd> <dbname> | gzip > dbname-$DATE.sql.gz

This will run it at 4:00 AM on 1st, 8th, 15th, 21st, and 28th of each month.

Bakkal
+1 for correcting my answer. Don't know why I thought he was asking for monthly when "weekly" is in the question subject...
timdev
It looks like 0 0 * * 0 is the better syntax to get this to be truly weekly.
KevinM