简体   繁体   中英

How recover deleted crontab (-r option)

I have deleted my crontab using crontab -r command ( r and e are so close on keyboard and there wasn't question sort of Are you sure? [Y/n] .

Its funny, you must use -i option to enable this question - should be enabled by default:-)

My crontab records are definitely lost or there is some backup file, how can I recover it?

Thank you.

I faced the same issue but I had edited the crontab file several times before pressing crontab -r, so I could find the previous versions in tmp directory. So try your luck looking into your tmp directory once.

If you are lucky and this happend fairly recent, you might find it in

/tmp/crontab.<unique_identifier>/crontab

This is a copy of your previous crontab. You can simply copy and paste it to get your cronjobs back

Unfortunately, the crontab mechanism does not provide a way to recover deleted or earlier versions of your crontab file. That being said, it is always worth tracing the following steps:

  1. Check lingering temporaries: When you edit a crontab file, you make use of an editor. By default, this is vi , but it could also be the one defined by the variables EDITOR or VISUAL . Depending on the settings of those editors and your handling, you might have lingering backup files laying around. These could be of the form filename~ or #filename or anything else. Check files that could represent backups of _PATH_TMP/crontab.XXXXXXX . The value of _PATH_TMP is defined in /usr/include/paths.h . If that file does not exist, it defaults to /tmp . Be aware that /tmp is wiped at every reboot of your system.
  2. Check filesystem snapshots or clones: Crontab stores user crontab files in an implementation-dependent location. By default this is /var/cron/tabs/$USER but it could also be /var/spool/cron/$USER or anything else. Check man crontab to disclose the location. There will never be a backup file there. However, your system might have a filesystem that has snapshots or clones configured. These are read-only copies of the filesystem taken at a moment in time. Think of it as a backup on the file system itself. From the more classic filesystems (ext4, ...) BTRFS, OCFS2 and ZFS support snapshots.
  3. Check actual backups: Maybe you are in luck and your system-administrator made backups for you.

Beyond these 3 possibilities, I do not see a way how you could recover your crontab.

The most important thing you should do is to let cron backup your crontab file. You could do this hourly using:

# Example of job definition:
# .----------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  0  *  *  *  *   crontab -l > tmp/crontab.bck

Obviously, you can make it more intelligent with a script, but this would already satisfy your bare needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM