rsync+notify 同步(異步方式)文件

同步文件,多個主機。能夠作圖片服務同步,代碼管理同步等。經過異步方式同步,監控到文件的變化。同步更新變化的內容,效率比較好。linux

環境說明

服務類型 IP地址 應用 操做系統
源服務器 192.168.217.151 rsync inotify-tools 腳本 centos7/redhat7
目標服務器 192.168.217.150 rsync centos7/redhat7

在目標服務器上作如下配置

1.關閉防火牆與SELINUX

# systemctl stop firewalld # systemctl disable firewalld # sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux # getenforce 0

2.安裝rsync服務端軟件

#yum -y install rsync

3.設置rsyncd.conf配置文件(先後不要有空格)test爲同步的文件夾



log file = /var/log/rsyncd.log
//日誌文件位置,啓動rsync後自動產生,無需提早建立
pidfile = /var/run/rsyncd.pid
//pid文件存放位置
lock file = /var/run/rsync.lock
//支持max connections參數的鎖文件
secrets file = /etc/rsync.pass
//用戶認證配置文件,裏面存放用戶名稱和密碼,必須手動建立這個文件
   
[etc_from_client]
//自定義同步名稱
path = /test/
//rsync服務端存放路徑,客戶端的數據將同步到此目錄
comment = sync etc from client
uid = root
//設置rsync運行權限爲root
gid = root
//設置rsync運行權限爲root
port = 873
//默認端口爲873
ignore errors
//表示出現錯誤忽視錯誤
use chroot = no
//默認爲true ,修改成no,增長對目錄軟連接的備份
read only = no
//設置rsync服務端爲讀寫權限
list = no
//不顯示rsync服務端資源列表
max connections = 200
//最大鏈接數
timeout = 600
//設置超時時間
auth users = admin
//執行數據同步的用戶名,能夠設置多個,用英文逗號隔開
hosts allow = 192.168.217.151
//容許進行數據同步的IP地址,能夠設置多個,用英文逗號隔開

# vim /etc/rsyncd.conf

4.建立存放路徑目錄

# mkdir /test

5.建立用戶認證文件


echo 'admin:111' > /etc/rsync.passcat /etc/rsync.pass

6.設置文件權限


chmod 600 /etc/rsync* ll /etc/rsync*

7.啓動rsync服務並設置開機自啓動

# systemctl start rsyncd # systemctl enable rsyncd


在源服務器上作如下部署:

1.關閉防火牆與SELINUX

# systemctl stop firewalld # systemctl disable firewalld # sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux # setenforce 0 
2.安裝rsync服務端軟件

 yum -y install rsyncc++

3.建立認證密碼文件

echo '111' > /etc/rsync.pass # cat /etc/rsync.pass

4.設置文件權限,只設置文件全部者具備讀取、寫入的權限

# chmod 600 /etc/rsync.pass # ll /etc/rsync.pass

5.在源服務器上建立測試目錄,而後在源服務器上運行如下命令

rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.217.150::etc_from_client --password-file=/etc/rsync.pass

6.運行完成後在目標服務器上查看,在/test/目錄下有test目錄,說明數據同步成功

test

ls /

7.安裝inotify-tools工具,實時觸發rsync同步

檢查服務器內核是否支持inotify,若是有這三個max開頭的文件則表示服務器內核支持inotify
ll /proc/sys/fs/inotify/



yum -y install make gcc gcc-c++ inotify-tools

8.寫同步腳本

# mkdir /scripts vim

# touch /scripts/inotify.sh # chmod 755 /scripts/inotify.sh # ll  # vim /scripts/inotify.sh host=192.168.217.150 //目標服務器的ip(備份服務器) src=/test //在源服務器上所要監控的備份目標 des=etc_from_client //自定義的模塊名,須要與目標服務器上的定義名稱同步 password=/etc/rsync.pass //執行數據同步的密碼文件 user=admin //執行數據同步的名 inotifywait=/usr/bin/inotifywait $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files ; do rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done

//檢查腳本



//啓動腳本

# bash -x /scripts/inotify.sh
nohup bash /scripts/inotify.sh & # ps -ef|grep inotify

9.設置腳本開機自動啓動


# chmod +x /etc/rc.d/rc.localll /etc/rc.d/rc.local
echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
tail /etc/rc.d/rc.local

10.誰便在根目錄test裏邊建點文件,到目標服務器上查看是否把新生成的文件自動傳上去了

相關文章
相關標籤/搜索