【就业班作业】【第八周】每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
一、编写备份的小脚本并用crontab -e 编辑任务
[root@localhost backup]# cat etcback.sh #!/bin/bash #
[-d /backup ] || mkdir /backup /usr/bin/tar -JcPf /backup/etcbak-`date -d -1day +%F-%H`.tar.xz /etc/ > /dev/null [root@localhost backup]# crontab -e [root@localhost backup]# crontab -l 30 1 * * 1-5 /backup/etcback.sh [root@localhost backup]#
二、查看执行结果和日志
[root@localhost backup]# cat /var/log/cron | tail Sep 21 16:20:16 localhost crontab[12728]: (root) REPLACE (root) Sep 21 16:20:16 localhost crontab[12728]: (root) END EDIT (root) Sep 21 16:20:20 localhost crontab[12730]: (root) LIST (root) Sep 21 16:20:27 localhost crontab[12731]: (root) BEGIN EDIT (root) Sep 21 16:20:33 localhost crontab[12731]: (root) REPLACE (root) Sep 21 16:20:33 localhost crontab[12731]: (root) END EDIT (root) Sep 21 16:21:01 localhost crond[10947]: (*system*) RELOAD (/etc/crontab) Sep 21 16:21:01 localhost CROND[12741]: (root) CMD (/backup/etcback.sh) [root@localhost backup]# ll total 7180 -rwxr--r--. 1 root root 98 Sep 21 16:18 etcback.sh -rw-r--r--. 1 root root 7346164 Sep 21 16:21 etcbak-2020-09-20-16.tar.xz
/usr/bin/tar -JcPf /backup/etcbak-`date -d -1day +%F-%H`.tar.xz /etc/ 命令过长不能在crontab直接使用,需要使用脚本调用。日志报以下内容:
Sep 21 16:17:01 localhost crond[10947]: (*system*) RELOAD (/etc/crontab) Sep 21 16:17:01 localhost crond[10947]: (root) RELOAD (/var/spool/cron/root) Sep 21 16:17:01 localhost CROND[12708]: (root) CMD ((/usr/bin/tar -JcPf /backup/etcbak-`date -d '-1day' +)
ps:
hwclock --set --date="09/21/2020 14:45:00"
hwclock -s
clock --set --date="09/21/2020 14:45:00"
hwclock -w
(结束)