Crontabs are very useful in Linux for scheduling repeated jobs. We can schedule any script or command to run on particular time interval. All the jobs executed by cron runs in background. As a system admin, we know the importance of backups of system or application configuration files and we do it through cronjob but most of time to forget to take back of cronjobs.
This article will describe you to how to take backup of scheduled jobs for specific user or all users in system. To install or know more about Crontab refer our earlier articles.
How to Install Crontab in CentOS/RHEL 6/5
Crontab in Linux with 20 Useful Examples to Schedule Jobs
1. Backup Single User Cronjobs
Take the backup of scheduled jobs off current logged in user. This command will save all the output of current jobs listed in a txt file. From where we can simply restore it.
# crontab -l > cron-backup.txt
To backup jobs of other user in system, For example we are taking backup of all jobs scheduled for user john.
# crontab -u john -l > john-cron-backup.txt
2. Restore Single User Cronjobs from Backup
Cronjobs can be restored easily from backups as created above. Below is two commands which will restored jobs from backup created in above step.
# crontab cron-backup.txt # crontab -u john john-cron-backup.txt
3. Backup All Users Cron jobs in CentOS/RHEL
All the cronjobs we scheduled for a user in CentOS/RHEL are physically stored in file with the username under /var/spool/cron directory. So to take backup of all jobs for all users, simply back /var/spool/cron directory.
# zip -r cronjobs-all.zip /var/spool/cron
We can also schedule this as job in crontab to take own backup
0 2 * * * zip -r cronjobs-all.zip /var/spool/cron
4. Backup All Users Cron jobs in Ubuntu and Debian
All the Cronjobs scheduled for a user in Ubuntu and Debian are physically stored in file with the username under /var/spool/cron/crontabs/ directory. So to take backup of all jobs for all users, simply create a backup of /var/spool/cron/crontabs directory.
# zip -r cronjobs-all.zip /var/spool/cron/crontabs
We can also schedule this as job in crontab to take own backup
0 1 * * * zip -r cronjobs-all.zip /var/spool/cron/crontabs