How to Backup crontab and Load from a File (Linux and UNIX)

If you are a Linux user, then you might know about cron jobs. They are scheduled jobs that runs processes on your system at a scheduled time. If you have been used Task scheduler in Windows, then same thing happens here. You can configure cron job to schedule scripts or any other commands to run automatically in order to perform your tasks which you needs to run daily, weekly, monthly etc.

crontab command is used for list existing cron entries, and editing them. Every user in the system can have a crontab. Cron table which is used by the cron daemon in Linux manages the execution of the cron jobs. There are commands to view cron jobs, edit cron jobs and upload cron jobs from a file. But there is no any command to backup existing cron jobs. We are going to present you a trick to backup cron jobs before editing them or loading a new set of cron jobs.

For many Java projects in these days uses cron jobs for start the process. So once when new process introduced you have to add new entry into cron table for start and stop the process. To do that you have to edit crontab manually by using crontab -e option.

This is a risk for existing jobs in the crontab, which means they can be modify accidentally. This can be avoid uploading all cron jobs from a file without manually editing the crontab to add new jobs. This is really useful if you have to maintain lots of servers that runs same cron job entries.

Create a file with two cron jobs

Let’s create a file with two cron jobs. One is for stop apache server and other one is for start apache server. I have created the file with the named of my_cronjobs.txt in location : /home/my_cron/

Let’s check the created file with cat command.

$cat /home/my_cron/my_cronjobs.txt

5 3 0 0 * * 7 /etc/init.d/stop-apache2.sh
0 1 0 0 * * * /etc/init.d/start-apache2.sh

Upload created file to crontab

We can use crontab command to upload my_cronjobs.txt to crontab of the current user.

$crontab /home/my_cron/my_cronjobs.txt

Check if cron jobs are successfully uploaded

Once uploaded we can check the cron table with crontab -l command to make sure the cron jobs are successfully uploaded. With crontab -l command you can check all cron entries in the cron table.

$crontab -l

5 3 0 0 * * 7 /etc/init.d/stop-apache2.sh
0 1 0 0 * * * /etc/init.d/start-apache2.sh

Get a backup of current crontab

It is important to get a backup of the existing cron entries before making any changers to the cron table. Incase if you wanted to rollback the changers you have done to cron table, then you can restore the backup you have taken. Therefor it is really important that you learn how to backup existing cron entries to a file.

Using following command you can backup existing cron table to a file.

$ crontab -l > backup_date.text

date is the date which we are taking the backup. e.g. backup_22072020 etc. What happens in the above command is, we are listing all cron jobs in the cron table and redirecting the output to the file we are creating. So that we can use this file to restore all cron jobs by using the command we have used before to upload cron jobs from a file. Save and exit once you have done. New cron jobs will be automatically loaded into cron table.

Below are the options mostly use with crontab command.

-l option to display the current crontab on standard output.

-r option to remove the current crontab. (It will remove all cron jobs in the crontab)

-e option is used to edit the current crontab using the editor.

Leave a Reply

Your email address will not be published. Required fields are marked *