Linux centos下設置定時備份任務的方法步驟
更新時間:2019年01月21日 15:19:37 作者:apollo1616
這篇文章主要介紹了Linux centos下設置定時備份任務的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實現(xià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' # 回應/tmp/logs echo ${mypath} # 要備份的日志 mylog='/opt/apollo/logs/access_log' # 回應/opt/apollo/logs/access_log echo ${mylog} # 時間戳,執(zhí)行命令使用``,esc下面的 time=`date +%Y%m%d%H%M%S` # 回應時間戳 echo ${time} # 備份日志access_log到/tmp/logs路徑下 cp ${mylog} ${mypath}/${time}_access.log # 回應 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í)行權限
[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.編輯定時任務
[root@localhost logs]# crontab -e no crontab for root - using an empty one crontab: installing new crontab
7.查看定時任務
# 每分鐘執(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 # 追加內容如下: mmmmmmmmmmmmmmmmmmmmm
10.過1分鐘,再去查備份存放目錄
[root@localhost logs]# cat 20190120083101_access.log djddjsjsjsjjsjsjsj mmmmmmmmmmmmmmmmmmmmm
11.到此為止,定時備份任務完成.
恭喜你,學會備份了!
12.刪除定時任務
[root@localhost logs]# crontab -r You have new mail in /var/spool/mail/root
13.查看定時任務
[root@localhost logs]# crontab -l no crontab for root
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 阿里云Centos7安裝svn與配置講解
- CentOS7將Nginx添加系統(tǒng)服務的方法步驟
- Centos7.x下Nginx安裝及SSL配置與常用命令詳解
- centos6.5安裝python3.7.1之后無法使用pip的解決方案
- CentOS 7.3配置Nginx虛擬主機的方法步驟
- CentOS版本問題安裝Docker報錯的解決方案
- Centos7修改主機名hostname的三種方法
- Linux centOS安裝JDK和Tomcat的教程
- 在CentOS7上搭建Jenkins+Maven+Git持續(xù)集成環(huán)境的方法
- Centos7.5配置java環(huán)境安裝tomcat的講解
相關文章
error while loading shared libraries xx.so處理方法
一般我們在Linux下執(zhí)行某些外部程序的時候可能會提示找不到共享庫(error while loading shared libraries xx.so)的錯誤2013-04-04linux下搭建scala環(huán)境并寫個簡單的scala程序
今天小編就為大家分享一篇關于linux下搭建scala環(huán)境并寫個簡單的scala程序,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04