Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a command for cron to do automatic backups for my flashdrive.
I use tar ... -N <some date> to make the backup.
The issue is how to write the command which gives me the right date.

For weekly backup I thought about:
date -d '1 week ago' +%Y%m%d
date -d 'monday week ago' +%Y%m%d

But it gets complicated when when my laptop is turned off on the cron scheduled time (monday after). Let's say anacron pick up the job on Tuesday. With the first variant I miss all changes from previous Monday. The second variant misses the whole previous week.
date -d 'monday 1 week ago' +%Y%m%d is the same as date -d 'monday week ago' +%Y%m%d
date -d 'monday 2 week ago' +%Y%m%d is correct in the case when backup is made by anacron, but it has one week extra in (the regular case) of cron.

Any idea, how could I make the backup time anacron compatible?

share|improve this question

Here's an idea: Every time a backup is run, the backup script records the current date by touching a file. The next time the backup runs, it uses the previous backup's datestamp file as the argument to tar -N. For example, in a shell script, the logic could look like this:

# Begin the latest backup run now.
touch /backups/new-backup-time

tar -N /backups/prev-backup-time -c ... ...

# Save the latest backup time for the next run.
mv /backups/new-backup-time /backups/prev-backup-time

This depends on the fact that the tar -N option can either be a date, OR it can be the pathname of a file whose data modification time specifies the date.

share|improve this answer

Your Answer


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.