crontab recover accidentally deleted

Often use crontab -e to edit a scheduled task, a second hand cheap knock became a command crontab -r, sad reminders

usage:    crontab [-u user] file
    crontab [ -u user ] [ -i ] { -e | -l | -r }
        (default operation is replace, per 1003.2)
    -e    (edit user's crontab)
    -l    (list user's crontab)
    -r    (delete user's crontab)
    -i    (prompt before deleting user's crontab)

 

Then my instinct, Google quickly find solutions are found all through the / var / log / cron * File Recovery, but I have not found this document ah

As I disheartened intend to write on several tasks from memory, others slowly to find someone to recover, I had an idea, might log files in this directory does not

So, I global search for a moment and "cron" relevant documents, 

sudo find / --name "*cron*"

We find a lot, which has a file extremely loud: "/tmp/crontab.VgsSIK/crontab"

I first checked the file date, file 15 days before, shaking hands, knocking down vim command, wow, my world has returned to color, crontab file is the backup. Although the file is 15 days before, but also enough to make me back to life

 

>>>>>>>>>>>>>>>>>>>>>>>>

The above is my story, here to talk about that if there is no backup, there is a log file, how should do

to recover according to the log file:

CAT / var / log / the cron * | grep -i " a user " | grep " the CMD " | awk -F ' ( '  ' {}. 3 Print $ ' | awk -F ' ) '  ' {}. 1 Print $ ' | Sort -u> crontab_tmp
Explanation:
grep - i "a user": to filter information from other users
grep " CMD " : is the need to filter non-command line
awk -F ' ( '  ' {}. 3 Print $ ' : is '(' as a separator, where the third element extraction result "/ Home / scripts /. check_alive.sh)"
awk -F ' ) '  ' {}. 1 Print $ ' : is '') as a separator, extracting a first element. Here the result is "/ Home / scripts / check_alive.sh"
At this time, we have been extracted to command their own needs, but because of the timing of the trigger crontab, there will be a lot of duplication. We need to go back heavy
Sort > cmd_tmp: output to a file to re cmd_tmp
The subsequent command again extracted / var / log / file confirm the cron time interval, to recover / manner specified var / spool / the cron /

 

>>>>>>>>>>>>>>>>>>>>>>> crontab scheduled backup script
from this article and pasted it to thank the original author

Description ⚠️: seven every morning to crontab backup, delete data 7 days ago

Script is as follows:

backup_crontab.sh

#!/usr/bin/env bash

BACKUP_DIRECTORY="${HOME}/crontab_backup"

if [ ! -e "${BACKUP_DIRECTORY}" ]; then
        mkdir -p ${BACKUP_DIRECTORY}
fi

crontab -l > ${BACKUP_DIRECTORY}/$(date '+%Y%m%d').txt
find ${HOME}/crontab_backup -mtime +7 -name '*.txt' -exec rm -rf {} \;

Description:

The above script " Find the HOME} {$ / crontab_backup -mtime +7 -name '* .txt' -rf RM -exec {} \; " , can be replaced with " Find the HOME} {$ / crontab_backup -mtime +7 -name '* .txt' | xargs RM -rf " .

 

The above script in crontab scheduled task to perform:

## backup_crontab
0 7 * * * cd /data/siterecoffline && sh backup_crontab.sh

 

Guess you like

Origin www.cnblogs.com/zhang-can/p/11764139.html