佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Linux centos下設置定時備份任務的方法步驟

Linux centos下設置定時備份任務的方法步驟

熱門標簽:百度競價點擊價格的計算公式 智能手機 網(wǎng)站建設 硅谷的囚徒呼叫中心 檢查注冊表項 美圖手機 使用U盤裝系統(tǒng) 阿里云

實現(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
# 追加內(nèi)容如下:
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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

標簽:懷化 湖北 通遼 黃山 賀州 山南 煙臺 湘潭

巨人網(wǎng)絡通訊聲明:本文標題《Linux centos下設置定時備份任務的方法步驟》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    绥棱县| 南开区| 蓬溪县| 长治市| 宜川县| 文山县| 延吉市| 丰镇市| 东乡族自治县| 施秉县| 渑池县| 长阳| 徐州市| 安化县| 通河县| 临夏县| 盖州市| 莆田市| 隆昌县| 玛多县| 哈巴河县| 水城县| 拉萨市| 岢岚县| 秦皇岛市| 松江区| 永城市| 宜阳县| 克山县| 河北省| 尉犁县| 诸城市| 平山县| 封开县| 密山市| 铜川市| 英山县| 托克逊县| 新巴尔虎右旗| 孟州市| 萨嘎县|