實現(xiàn)準(zhǔn)備
# 需要備份文件路徑:/opt/apollo/logs/access_log
[root@localhost opt]# cd apollo/
[root@localhost apollo]# tree
.
├── logs
│ └── access_log
└── test.sh
# 文件備份存放路徑:/tmp/logs
# 備份文件加上時間戳date + %Y%m%d%H%M%S
1.編寫shell腳本
[root@localhost tmp]# vi /opt/apollo/test.sh
# 編譯器
# !/bin/bash
# 日志備份到該目錄下,定義變量使用單引號
mypath='/tmp/logs'
# 回應(yīng)/tmp/logs
echo ${mypath}
# 要備份的日志
mylog='/opt/apollo/logs/access_log'
# 回應(yīng)/opt/apollo/logs/access_log
echo ${mylog}
# 時間戳,執(zhí)行命令使用``,esc下面的
time=`date +%Y%m%d%H%M%S`
# 回應(yīng)時間戳
echo ${time}
# 備份日志access_log到/tmp/logs路徑下
cp ${mylog} ${mypath}/${time}_access.log
# 回應(yīng)
echo ${mypath} ${mypath}/${time}_access.log
2.執(zhí)行test.sh
[root@localhost apollo]# ./test.sh
-bash: ./test.sh: Permission denied
3.執(zhí)行l(wèi)s -la
[root@localhost apollo]# ls -la
total 8
drwxr-xr-x 2 root root 21 Jan 20 08:00 .
drwxr-xr-x. 14 root root 4096 Jan 20 07:07 ..
-rw-r--r-- 1 root root 489 Jan 20 08:00 test.sh
4.給文件test.sh賦與執(zhí)行權(quán)限
[root@localhost apollo]# chmod +x ./test.sh
[root@localhost apollo]# ls -la
total 8
drwxr-xr-x 2 root root 21 Jan 20 08:00 .
drwxr-xr-x. 14 root root 4096 Jan 20 07:07 ..
-rwxr-xr-x 1 root root 489 Jan 20 08:00 test.sh
5.再次執(zhí)行,腳本沒有報錯
[root@localhost apollo]# ./test.sh
/tmp/logs
/opt/apollo/logs/access_log
20190120080932
/tmp/logs /tmp/logs/20190120080932_access.log
6.編輯定時任務(wù)
[root@localhost logs]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
7.查看定時任務(wù)
# 每分鐘執(zhí)行一次test.sh
* * * * * sh /opt/apollo/test.sh
8.重啟crond
[root@localhost logs]# service crond reload
Redirecting to /bin/systemctl reload crond.service
You have new mail in /var/spool/mail/root
9.編寫文件access_log
# 需要備份文件路徑:
/opt/apollo/logs/access_log
# 編輯文件
[root@localhost logs]# vi /opt/apollo/logs/access_log
# 追加內(nèi)容如下:
mmmmmmmmmmmmmmmmmmmmm
10.過1分鐘,再去查備份存放目錄
[root@localhost logs]# cat 20190120083101_access.log
djddjsjsjsjjsjsjsj
mmmmmmmmmmmmmmmmmmmmm
11.到此為止,定時備份任務(wù)完成.
恭喜你,學(xué)會備份了!
12.刪除定時任務(wù)
[root@localhost logs]# crontab -r
You have new mail in /var/spool/mail/root
13.查看定時任務(wù)
[root@localhost logs]# crontab -l
no crontab for root
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。