1

I want to do a backup every Friday night (no, this is not the whole backup routine, just part of it). Each Friday night's backup will not be overwritten until 4 weeks later. So, essentially, I have a four revolving backups: Week1, week2, week3, and week4.

Now, I need the week1 backup script to run every 4 weeks. But I also want week2's script to run every four weeks. I know that I can tell the crontab to execute something every X weeks/days/hours/whatever. However, how do I set it up so that each of these four scripts actually run on different weeks, how do I avoid all 4 scripts running on the same night, then dutifully waiting for weeks only to all run again?

4 Answers 4

4

instead of designing such complex backups routines, consider using Bacula, which is an Enterprise class Free Open source software. Bacula is very flexible and it can automate backup jobs in almost every possible way you want.

Even if you are able to make your MANUAL solution, there are many drawbacks of it:

  • Traking errors would be big problem
  • Tracking of jobs would be a headache
  • manual setup always needs constant monitoring

The best system admin is that one, who automates everything and sits back relaxing.

2
  • Your last sentence describes precisely what I'm trying to do. I want to avoid adding software to the mix when a few lines of scripting can get the job done. A little rsync and crontab gives me perfect replicas of my hard drives. I'm a minimalist, so I like to build things from the ground up from scratch when possible.
    – Garfonzo
    Nov 26, 2011 at 6:29
  • Use a proper backup application. It's more portable and resilient.
    – ewwhite
    Nov 26, 2011 at 14:16
1

I think frank's answer is the best, but to do as you describe you can have a cronjob do you backup every Friday and then use log rotate to keep the last four copies. Or have a find look for any backup more then four weeks old.

1

One alternative: put the complexity into your script, since it's better able to handle that sort of complexity than cron. So (assuming bash):

  • Have your cron job run the same script every Friday.
  • Modify your script to give you the modulo 4 of the weeks since epoch:

(stupid formatting issue, need to put dummy text here)

epochsecs=`date +%s`   # second since epoch

and

weeknum=`expr $epochsecs / 86400 / 7 % 4`  # weeks since epoch, modulo 4
  • Use that number as part of the backup file name.

    backupfile=/path/to/backup/file.$weeknum

  • Overwrite the old backup files.

    rm $backupfile tar czvf $backupfile /bunch/of /directories

Alternatively, just use a filename suffix with the date on it:

backupfile=/path/to/backup/file.`date +%Y-%m-%d`

then run a find in your script that will delete backup files that are older than 28 days:

find /path/to/backup -maxdepth 1 -ctime +28 -delete

In any case, don't overburden cron with this sort of complexity. Handle it in the script, which is probably better able to handle the complexity.

0

Two alternatives:

  • Schedule a script to run every Friday that keeps track of which job was last run and runs the next job in the sequence.

or:

  • Schedule week1 to run on days 1-7 of the month and only on Fridays.
  • Schedule week2 to run on days 8-14 of the month and only on Fridays.
  • ... and so on.
  • You will notice that this doesn't quite give you a perfect backup - there will be times when you miss a week of backup (when day 29/30/31 falls on a Friday)

This kind of figuring is exactly why, as much Fun as it is to do everything yourself, you will end up further ahead by using a proper backup tool such as amanda or bacula as Frank suggests.

You must log in to answer this question.

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