Pythian Blog: Technical Track

How to efficiently back up and restore crontab

When a database environment is being patched, entries from crontab are commented and then removed when patching is completed. The process works well when there are only a few entries from crontab, but it does not scale when crontab has dozens of entries and when some entries are commented and some are not. I believe a more efficient method is to backup and restore the existing crontab. Here's a demo: Crontab entries.
[oracle@racnode-dc1-1 ~]$ crontab -l
 */05 * * * * /bin/date > /tmp/date.out 2>&1
 */15 * * * * /bin/date > /tmp/date.out 2>&1
 #*/25 * * * * /bin/date > /tmp/date.out 2>&1
 */35 * * * * /bin/date > /tmp/date.out 2>&1
 */45 * * * * /bin/date > /tmp/date.out 2>&1
 */55 * * * * /bin/date > /tmp/date.out 2>&1
 
Backup crontab and display contents. Extra precaution in case crontab.save.dinh is removed.
[oracle@racnode-dc1-1 ~]$ crontab -l > crontab.save.dinh; cat crontab.save.dinh
 */05 * * * * /bin/date > /tmp/date.out 2>&1
 */15 * * * * /bin/date > /tmp/date.out 2>&1
 #*/25 * * * * /bin/date > /tmp/date.out 2>&1
 */35 * * * * /bin/date > /tmp/date.out 2>&1
 */45 * * * * /bin/date > /tmp/date.out 2>&1
 */55 * * * * /bin/date > /tmp/date.out 2>&1
 
Remove crontab.
[oracle@racnode-dc1-1 ~]$ crontab -r; crontab -l
 no crontab for oracle
 
Restore crontab.
[oracle@racnode-dc1-1 ~]$ crontab crontab.save.dinh; crontab -l
 */05 * * * * /bin/date > /tmp/date.out 2>&1
 */15 * * * * /bin/date > /tmp/date.out 2>&1
 #*/25 * * * * /bin/date > /tmp/date.out 2>&1
 */35 * * * * /bin/date > /tmp/date.out 2>&1
 */45 * * * * /bin/date > /tmp/date.out 2>&1
 */55 * * * * /bin/date > /tmp/date.out 2>&1
 
If you are lazy like me, then backup can be done in one command.
[oracle@racnode-dc1-1 ~]$ crontab -l > crontab.save.dinh; cat crontab.save.dinh; crontab -r; crontab -l
 */05 * * * * /bin/date > /tmp/date.out 2>&1
 */15 * * * * /bin/date > /tmp/date.out 2>&1
 #*/25 * * * * /bin/date > /tmp/date.out 2>&1
 */35 * * * * /bin/date > /tmp/date.out 2>&1
 */45 * * * * /bin/date > /tmp/date.out 2>&1
 */55 * * * * /bin/date > /tmp/date.out 2>&1
 no crontab for oracle
 [oracle@racnode-dc1-1 ~]$
 
 

No Comments Yet

Let us know what you think

Subscribe by email