2

I run a relatively simple server with a couple of web sites that I maintain.

I set up a crontab to dump the mysql and zip up the PHP daily. Rotating over a week I have 7 increments of whatever has been going on. I then use the same crontab to copy them off the server onto another.

Using the mysql bit as an example I have

16 3 * * tue mysqldump filename > /var/backups/b-Tuesday/filename.sql  
16 3 * * wed mysqldump filename > /var/backups/b-Wednesday/filename.sql
[etc]

This repeats for the PHP getting zipped up for each of the sites and again for the scp part of the process.

Is there a way to have a daily job for each site that knows to save it to the relevant day's backup folder rather than having 7 jobs for each thing I'm backing up?

5 Answers 5

15

How about by using the date function in the call?

16 3 * * * mysqldump filename > /var/backups/b-`date +%A`/filename.sql
5
  • +1 I misread the question, this is a better answer than mine.
    – jj33
    May 8, 2009 at 15:08
  • +1, this is the way to do it. Adding some compression to the file would help on keeping things small, so perhaps a combination of your approach and @CK approach would do the job marvelously.
    – user1797
    May 8, 2009 at 17:56
  • For those of you like me who would never have considered this sort of simplicity, don't get tricked by the `. Just out of ignorance I put in a ' and boy, does that give an fun file name...
    – Humpton
    May 9, 2009 at 13:45
  • That's a "back tick" and an "apostrophe" in the last comment (for clarification)
    – Brent
    May 9, 2009 at 14:24
  • That's why it's better to use $(date +%A) since it's more readable than the backquote / backtick form. May 9, 2009 at 16:22
2

Try a shell script something like this:

#!/bin/bash
/usr/bin/mysqldump filename | /usr/bin/bzip2 -c > /var/backups/b-`date +%A`/filename.sql.bup.bz2

That's similar to what's in my /etc/cron.daily/ but with changes for your paths. I keep meaning to do more with it, but it works and the backups are nicely compressed as well!

My date format is actually date +%Y%m%d to give me more info, but I keep the backups for longer than 7 days.

1
  • yes, better IMHO to have the variable stuff done in a script than in the crontab entry
    – Alnitak
    May 9, 2009 at 13:47
2

As an alternative, the logrotate command will handle compressing, renaming (with dates), removal, etc. of files that are generated every day. You could call your dump commands every day and call logrotate once a day as well. The logrotate configuration file lets you specify which files to rotate and what to do with them.

1

For a quick and workable solution, Brent's spot on with the use of date.

Depending on the need, and the desire for future flexibility, I might consider throwing the backups into a shell script. That way you can use the same code base for daily, weekly, hourly, or whatever. You also gain the benefit of error logging and reporting (if that's of value).

0

Tangentially related, rsnapshot will manage hourly, daily, weekly, monthly backup rotations for you. It uses a clever combination of rsync and hard links to make backups.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .