rsycn+inotify-tools實(shí)現(xiàn)遠(yuǎn)程實(shí)時(shí)備份數(shù)據(jù)

目的:
源主機(jī)A數(shù)據(jù)實(shí)時(shí)備份數(shù)據(jù)到遠(yuǎn)程備份主機(jī)B上

1、設(shè)置主機(jī)A免密訪問(wèn)備份主機(jī)B
2、安裝inotify-tools(監(jiān)控源主機(jī)文件變化)
3、編寫(xiě)實(shí)時(shí)同步腳本
4、配置檢查
5、設(shè)置定時(shí)全數(shù)據(jù)備份

一、設(shè)置主機(jī)A免密訪問(wèn)備份主機(jī)B

1、在服務(wù)器A生成密鑰(注意:P為大寫(xiě))

[root@A ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

2、將生成的密匙拷貝到服務(wù)器B上,根據(jù)提示輸入服務(wù)器B的密碼

[root@A ~]# ssh-copy-id root@121.33.7.1  //服務(wù)器B的IP
root@121.33.7.1's password:      //輸入服務(wù)器B的root賬號(hào)登錄密碼
Now try logging into the machine, with "ssh 'root@121.33.7.1'", and check in:
.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

3、檢查是否成功

[root@A ~]# ssh 121.33.7.1(服務(wù)器B的IP)
Last login: Mon Oct 28 21:02:57 2019 from 121.33.7.1

    Welcome to Huawei Cloud Service
[root@B ~]#      //已成功連接到服務(wù)器B上

二、安裝inotify-tools(監(jiān)控源主機(jī)文件變化)

1、yum命令安裝

[root@A ~]#yum -y install inotify-tools

2、檢查監(jiān)控是否成功

[root@A ~]#inotifywait -mrq -e create,close_write,move,delete,modify /home/rsbak/

輸入命令后使用另一個(gè)終端窗口打開(kāi)源主機(jī)A,進(jìn)入rsbak目錄進(jìn)行增刪改查操作,返回監(jiān)控終端查看是否有監(jiān)測(cè)動(dòng)態(tài)日志

三、編寫(xiě)實(shí)時(shí)同步腳本

1、創(chuàng)建腳本文件(in.sh)

[root@A ~]# vi /home/rsbak/in.sh

2、腳本內(nèi)容

#!/bin/bash 
#inotifywait監(jiān)控源目錄動(dòng)態(tài)
inotifywait -mrq -e create,close_write,move,delete,modify /home/rsbak/ |  while read file  
do

#同步到目的主機(jī)bak目錄下,--delete 參數(shù)慎重使用,不使用去掉即可
 rsync -av --delete /home/rsbak/*.txt root@192.xx.xx.xx:/home/bak/

    #echo "  ${file} was rsynced" >>/opt/soft/log/rsync.log 2>&1 
done

#將腳本加入后臺(tái)執(zhí)行
sh in.sh &  
bash in.sh &

3、rsync服務(wù)參考
http://www.itdecent.cn/p/4beb57b99927

四、檢查配置是否成功

1、啟動(dòng)inotify-tools

 [root@A ~]# /home/rsbak/in.sh

2、啟動(dòng)后Ctrl+z退出監(jiān)聽(tīng)狀態(tài)

3、在源主機(jī)A的rsbak目錄下新建123.txt文件

4、進(jìn)入備份主機(jī)B存放備份數(shù)據(jù)的bak目錄下查看是否存在123.txt文件

腳本配置了后臺(tái)運(yùn)行權(quán)限,inotify-tools可在后臺(tái)監(jiān)測(cè)數(shù)據(jù)變化,同時(shí)進(jìn)行rsync備份

五、定時(shí)全數(shù)據(jù)備份

為避免實(shí)時(shí)備份出現(xiàn)的數(shù)據(jù)遺漏、數(shù)據(jù)錯(cuò)誤、備份出錯(cuò)等情況,建議定時(shí)執(zhí)行全數(shù)據(jù)備份

1、編寫(xiě)rsycn定時(shí)全數(shù)據(jù)備份腳本(rs.sh)

[root@A ~]# vi /home/rsbak/rs.sh
腳本加入內(nèi)容:
#! /bin/sh
 #源主機(jī)(rsbak)同步至備份主機(jī)(bak):
 #rsync -av /home/rsbak/ root@172.xx.xx.xxx:/home/bak/

2、設(shè)置定時(shí)
在crontab末尾加入自動(dòng)執(zhí)行腳本命令(每天0點(diǎn)執(zhí)行)

[root@A ~]# vi /etc/crontab
末尾加入命令:00 00 * * * root /home/rsbak/rs.sh

3、 保存退出,重啟cron。

[root@A ~]# /etc/rc.d/init.d/crond restart
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]

4、定時(shí)格式參考
http://www.itdecent.cn/p/4beb57b99927

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容