環境說明:
server:CentOS-02:192.168.1.220 (rsync server)
client:CentOS-01:192.168.1.210 (rsync client、inotify)
同步順序從client-server端。
一、配置server端。
1.準備好軟件包並解壓
[root@CentOS-02 ~]# ll 總用量 876 drwxrwxr-x. 10 root root 4096 12月 10 15:57 rsync-3.0.9 -rwxr-xr-x. 1 root root 792725 12月 10 13:51 rsync-3.0.9.tar.gz
2.安裝rsync軟件包
[root@CentOS-02 ~] cd rsync-3.0.9 [root@CentOS-02 rsync-3.0.9]./configure –prefix=/usr/local/rsync && make && make install
3.手動創建/etc/rsyncd.conf文件
log file = /var/log/rsyncd.log #存放rsync軟件日誌文件位置 pid file = /var/run/rsyncd.pid #存放rsync的pid文件位置 lock file = /var/run/rsyncd.lock #存放rsync的鎖文件位置 uid = root #以root用戶運行rsync軟件 pid = root #以root組運行rsync軟件 port = 873 #配置rsync監控的端口(默認873,可自行更改) max connections = 20 #配置最大客戶端的連接數 [server] #需要同步的模塊名 path = /usr/local/weblogic/webapps #需要同步的模塊路徑 comment = weblogic server project #對該模塊的描述信息 ignore errors #忽略錯誤 read only = no #配置不只讀 write only = no #配置不只寫 auth users = ruser #配置登陸名 secrets file = /etc/rsync.pass #配置指定用戶名及密碼的文件
4.建立/etc/rsyncd.conf文件中所認證的用戶名及密碼
[root@CentOS-02 rsync-3.0.9] echo “ruser:123” >> /etc/rsync.pass
5.將上述兩個文件授權爲600
[root@CentOS-02 rsync-3.0.9]chmod 600 /etc/rsyncd.conf [root@CentOS-02 rsync-3.0.9]chmod 600 /etc/rsync.pass
二、配置client端
1.準備inotify軟件包並解壓
[root@CentOS-01 ~]# ll drwxrwxrwx. 5 1000 1000 4096 12月 10 13:38 inotify-tools-3.14 -rwxr-xr-x. 1 root root 358772 12月 10 13:33 inotify-tools-3.14.tar.gz
2.安裝inotify軟件包
[root@CentOS-01 ~] cd inotify-tools-3.14 [root@CentOS-01 inotify-tools-3.14]./configure && make && make install
3. 寫一個 inotify 的監控腳本
[root@CentOS-01 ~]cat /data/script/rsync_inotify.sh #!/bin/bash src=/usr/local/weblogic/webapps des=server host=192.168.1.220 user=ruser /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read files do /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pass $src $user@$host::$des >>/data/script/rsync.log 2>&1 && echo "src was rsyncd" done
3.配置/etc/rsync.pass密碼
[root@CentOS-01 inotify-tools-3.14]echo “123” >> /etc/rsync.pass
4.授權此文件
[root@CentOS-01 inotify-tools-3.14]chmod 600 /etc/rsync.pass
5.將Inotify腳本放到後臺運行
[root@CentOS-01 inotify-tools-3.14] sh /data/script/rsync_inotify.sh &
三、驗證。
1.分別在server端及client端創建需同步的目錄
[root@CentOS-01 ~]# mkdir -d /usr/local/weblogic/webapps/server/ [root@CentOS-02 ~]# mkdir -d /usr/local/weblogic/webapps/server/
2.在client端進入該目錄
[root@CentOS-01 server]# pwd /usr/local/weblogic/webapps/server [root@CentOS-01 server]# [root@CentOS-01 server]# [root@CentOS-01 server]# ll 總用量 0
3.在server端進入該目錄
[root@CentOS-02 server]# pwd /usr/local/weblogic/webapps/server [root@CentOS-02 server]# ll 總用量 0
4.在client端創建一個文件及目錄,驗證
[root@CentOS-01 server]# touch 111 [root@CentOS-01 server]# src was rsyncd src was rsyncd [root@CentOS-01 server]# [root@CentOS-01 server]# [root@CentOS-01 server]# mkdir icon [root@CentOS-01 server]# src was rsyncd [root@CentOS-01 server]# ll 總用量 4 -rw-r--r-- 1 root root 0 12月 10 17:02 111 drwxr-xr-x 2 root root 4096 12月 10 17:03 icon [root@CentOS-01 server]# pwd /usr/local/weblogic/webapps/server [root@CentOS-01 server]#
5.在server端查看該文件及目錄是否已經同步過來
[root@CentOS-02 server]# pwd /usr/local/weblogic/webapps/server [root@CentOS-02 server]# ll 總用量 4 -rw-r--r-- 1 root root 0 12月 10 17:02 111 drwxr-xr-x 2 root root 4096 12月 10 17:03 icon [root@CentOS-02 server]#
可能遇到的錯誤:
如使用netstat –an | grep 873 未任何結果
原因:rsync未啓動
解決方法:
[root@CentOS-02 server]# vim /etc/xinetd.d/rsync disable = no 將該行改成no [root@CentOS-02 server]/etc/init.d/xinetd restart
如無法啓動xinetd服務
原因:未安裝xinetd
解決方法:
[root@CentOS-02 server]Yum –y install xinetd /etc/init.d/xinetd restart 或 [root@CentOS-02 server]rsync --daemon
rsync: failed to connect to 192.168.1.220: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
如果是這種錯
原因:因爲開了iptables,而未開放873端口
解決方法:
1.關閉Iptables
2.在iptables中開放TCP873端口
@ERROR: auth failed on module server rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
如果是這種錯
原因:用戶名及密碼不對及文件授權不正確
解決方法:
1.檢查client端/etc/rsync.pass文件中的用戶名及密碼和/etc/rsyncd.conf中的auth user是否對應
2.檢查server端inotify腳本中的user和client端/etc/rsyncd.conf是否對應,/etc/rsync.pass中的密碼是否和client端的/etc/rsync.pass文件中的密碼對應
3.檢查server端/etc/rsyncd.conf、/etc/rsync.pass以及client端/etc/rsync.pass文件的權限是否是600權限。