rsync是linux系統下檔案同步和資料傳輸工具,
他有四種應用模式:1.本地模式2.遠程shell模式3.rsync串列模式4.服務器模式
這里我們搭建的是服務器模式,這種模式是基于C/S模式,需要rsync在后臺啟用一個守護行程,這個行程在rsync服務器端永久進行,用于接收檔案傳輸請求,
舉個例子,假設有個網站,資料每天會更新,為保資料安全,建立一個遠程容災系統,但,rsync需要觸發才能更新,雖然可以使用linux守護行程crontab定時觸發,但兩次觸發動作是有一定時間差,可能導致服務端與客戶端資料不一致,無法在出現災害時,完全恢復資料,
在這里我們用到inotify工具,它是一個異步的檔案監控檔案系統,監控檔案系統的各種變化,當檔案有任何變化,就會觸發rsync同步,這剛好解決資料實時同步問題,

|
節點名稱 |
用途 |
IP |
網頁路徑 |
|
server |
內容發布節點 |
193.168.0.20 |
/webdata |
|
web2 |
服務節點 |
193.168.0.22 |
/web2data |
|
web3 |
服務節點 |
193.168.0.23 |
/web3data |
inotify是用來監控檔案系統的,所以一定安裝在內容發布節點,服務節點無需安裝,只需安裝rsync,
1.在服務器節點上配置rsync
[root@web2 ~]# tar xzf apt/rsync-3.0.4.tar.gz [root@web2 ~]# cd rsync-3.0.4/ [root@web2 rsync-3.0.4]# ./configure [root@web2 rsync-3.0.4]# make && make install
1.2.安裝好rsync時沒有rsyncd.con組態檔的,我們自行建一個就好
web2節點的rsyncd.conf
[root@web2 ~# vim /etc/rsyncd.conf uid = nobody gid = nobody use chroot = no max connections = 10 strict modes = yes pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [web2] path = /web2data comment = web2 file ignore errors read only = no write only = no hosts allow = * list = false uid = root gid = root auth users = web2user secrets file = /etc/web2.pass
1.3.web3節點的rsyncd.conf
[root@web3 ~# vim /etc/rsyncd.conf uid = nobody gid = nobody use chroot = no max connections = 10 strict modes = yes pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [web3] path = /web3data comment = web3 file ignore errors read only = no write only = no hosts allow = * list = false uid = root gid = root auth users = web3user secrets file = /etc/web3.pass
auth user 是定義的用戶,與linux系統用戶無關,用來連接該模塊的用戶名,
secretes file 是指定包含“用戶名:密碼”格式檔案,用戶名就是auth user定義的用戶名
[root@web2 ~]# more /etc/web2.pass web2user:admin [root@web2 ~]# chmod 600 /etc/web2.pass [root@web3 ~]# more /etc/web3.pass web3user:admin [root@web3 ~]# chmod 600 /etc/web3.pass
1.4.啟動rsync守護行程(web2,web3)
[root@web2 ~]# /usr/local/bin/rsync –daemon [root@web2 ~]# ps -ef |grep rsync root 38588 1 0 Mar23 ? 00:00:00 /usr/local/bin/rsync --daemon [root@web2 ~]# echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local #加入自啟動檔案
2.配置內容發布點
2.1.發布點主要是將靜態檔案實時同步到兩個服務器節點,這個程序我們用一個shell腳本實作
#!/bin/bash
host1=193.168.0.22
host2=193.168.0.23
src=https://www.cnblogs.com/webdata/
dst1=web2
dst2=web3
user1=web2user
user2=web3user
/usr/local/bin/inotifywait -mrq --timefmt'%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,cre
ate,attrib $src \
| while read files
do
/usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$h
ost1::$dst1
/usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user2@$h
ost2::$dst2
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
done
2.2.連接備份模塊密碼
[root@redhat6 ~]# more /etc/server.pass admin
2.3.這里為了方便把shell腳本就放在webdata目錄下,命名為inotify.sh,并給予執行權限,放到后臺運行
[root@redhat6 ~]# chmod 755 /webdata/inotify.sh [root@redhat6 ~]# nohup /webdata/inotify.sh &
2.4.加入系統自啟動檔案
[root@redhat6 ~]# echo "/webdata/inotify.sh &" >> /etc/rc.local
完成所有配置后,就可以在網站發布節點添加,洗掉,寫入檔案了,會看到2個服務器節點
跟隨發布節點跟更新檔案,(初次如果沒出現同步,稍等片刻,內容發布節點需要跟服務器節點模塊對接,出現同步后,后面的更新就會快多了,)
- 有時候會遇到這樣情況:向inotify監控目錄寫入一個很大的檔案,當寫入檔案需要一段時間,inotify會持續不斷輸出該檔案被更新的資訊,這樣就會持續觸發rsync同步,占用大量系統資源,針對這樣情況,最好方法是等待檔案完全寫入完,再觸發更新,可以修改“-e close_write,delete,create,atrrib”
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/276527.html
標籤:Linux
上一篇:Nginx 學習
下一篇:keepalived安裝及配置
