简体   繁体   中英

Including Cron/Crontab in my Bash Backup Script with inputs

Im doing a Bash-Backup Script with 3 Options:

  1. Do fullbackup
  2. Do fullbackup at a specific time with cron

First I want to ask for the path like: Path of directory: /home

Then i want the hour for the backup: Hour for the backup (0:00-23:59) : 2:00

Then a simple question like: The backup will execute at 2:00. Do you agree(y/n) The backup will execute at 2:00. Do you agree(y/n)

I know how to do a crontab but I have no idea how to include that in my script so that I choose option 2 and then the script asks me for the directory and time and set then the crontab .

Any ideas or help would be appreciated!

You can get the existing crontab with

crontab -l

and install a new crontab with

crontab file

So your script would probably need something like

crontab -l | grep -v "# install-backup-script" > /tmp/file.$$
echo "$min $hour * * * /full/path/to/script # install-backup-script" >> /tmp/file.$$
crontab /tmp/file.$$
rm -f /tmp/file.$$

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