How to Backup and Restore Crontab?

less than a minute of reading

If you deal with crontab on a daily basis, it may be worth to familiarise yourself with a backup / restore strategy in case something goes wrong.

Backup

First, see what is your crontab content:

crontab -l

Now just redirect standard output from your screen to an external file that wil serve as our backup:

crontab -l > /cron_`date '+%F_%T' | sed -e 's/:/_/g'`

This command will create a dump with the name that contains date and time of it’s creation.

cron_2012-09-10_14_54_01
cron_2012-09-11_14_54_26
cron_2012-09-12_14_55_28
Restore

The only thing you have to do is to override it with backup content. Cron’s content (individual for every user) is located at:

/var/spool/cron/crontabs/<YOUR_USER_NAME>

Let’s override it for root account:

cp cron_2012-09-12_14_55_28 /var/spool/cron/crontabs/root

To make sure that override process was correct browse it once again.

crontab -l

You should see new entries. To make sure save them and exit:

:wq

You should see a confirmation quite similar to the following output.

crontab: installing new crontab

Words: 188
Last Revision: December 16, 2022