Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 13 down vote accepted

How about by using the date function in the call?

16 3 * * * mysqldump filename > /var/backups/b-`date +%A`/filename.sql
share|improve this answer
    
+1 I misread the question, this is a better answer than mine. –  jj33 May 8 '09 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 '09 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 '09 at 13:45
    
That's a "back tick" and an "apostrophe" in the last comment (for clarification) –  Brent May 9 '09 at 14:24
    
That's why it's better to use $(date +%A) since it's more readable than the backquote / backtick form. –  Dennis Williamson May 9 '09 at 16:22

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).

share|improve this answer

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.

share|improve this answer
    
yes, better IMHO to have the variable stuff done in a script than in the crontab entry –  Alnitak May 9 '09 at 13:47

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.

--
bmb

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.