How to efficiently back up and restore crontab

2 min read
Jul 27, 2018 12:00:00 AM

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.

The Problem with Manual Crontab Management

Managing crontab entries by commenting them out during maintenance is error-prone. It's difficult to track which jobs were originally active and which were intentionally disabled prior to the maintenance window.

Current Crontab State

Here is our starting point for the demo:

[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 

A Better Way: Backup, Remove, and Restore

Instead of manual editing, we can export the entire table to a file and clear the crontab for the duration of the patching.

1. Backing Up the Crontab

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 

2. Clearing the Active Schedule

Remove crontab to ensure no jobs run during the patching window.

[oracle@racnode-dc1-1 ~]$ crontab -r; crontab -l  no crontab for oracle 

3. Restoring the Original Schedule

Once maintenance is complete, restoring is a single-step process.

[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 

Efficiency for the "Lazy" Admin

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 ~]$ 

Oracle Database Consulting Services

Ready to optimize your Oracle Database for the future?

On this page

Ready to unlock value from your data?

With Pythian, you can accomplish your data transformation goals and more.