實驗環境:<僅2臺主機之間進行數據雙向傳輸>
A主機:10.1.43.102
B主機:10.1.43.103
一、數據從A推向B
配置流程
先在B主機上配置:
1.vi /etc/rsyncd.conf(用戶,目錄,模塊,虛擬用戶及密碼文件)
uid = root gid = root port = 873 #post rsync使用的端口號 也是默認端口號 www.jbxue.com hosts allow = 10.1.43.102 #allow hosts ip 允許A主機的ip訪問 max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [backup] path =/var/www/html #客服端已rsync服務端同步的文件路徑 comment = from 10.1.43.102 #A主機的IP地址 read only = no write only = no list = no auth users =rsync #配置登陸名稱 secrets file = /etc/rsync.passwd11 #配置用戶名密碼文件
2.創建共享目錄:/test/rsync
mkdri -pv /test/rsync
3、創建密碼文件,文件路徑和文件名參照配置文件里"secrets file"選項的值,然后添加密碼內容。
vim /etc/rsync.password11 rsync:123456
4、密碼文件的權限600
chmod 600 /etc/rsync.password11
5、運行rsync,并且開機啟動
rsync –daemon echo "rsync –daemon" >> /etc/rc.local
6、如果出錯,查看日志
tail /var/log/rsyncd.log
在A主機上配置:
1、密碼文件
vim /etc/rsync.passwd11 123456 #注意:此處只需要寫服務端虛擬帳號的密碼即可
2、密碼文件的權限600
chmod 600 /etc/rsync.passwd11
3.創建共享目錄:/test/rsync
mkdri -pv /test/rsync
4、安裝inotify-tools軟件包
yum -y install inotify-tools
5、編寫同步腳本
vim /root/bin/rsync.sh #!/bin/bash src=/test/rsync des=backup host="10.1.43.103" /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y%H:%M' --format '%T%w%f' -e modify,delete,create,attrib \ #此行尚未完結 $src | while read files; do for hostip in $host; do rsync -avz --delete --progress --password-file=/etc/rsync.passwd11 $src rsync@$hostip::$des done echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
6、后臺自動運行,并且開機自動啟動
nohup /bin/bash /root/bin/rsync.sh & echo "nohup /bin/bash /root/bin/rsync.sh &" >> /etc/rc.loacl
二、數據從B推向A
配置流程
先在A主機上配置:
1.vi /etc/rsyncd.conf(用戶,目錄,模塊,虛擬用戶及密碼文件)
uid = root gid = root port = 873 #post rsync使用的端口號 也是默認端口號 www.jbxue.com hosts allow = 10.1.43.103 #allow hosts ip 允許B主機的ip訪問 max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [backup] path =/var/www/html #客服端已rsync服務端同步的文件路徑 comment = from 10.1.43.103 #B主機的IP地址 read only = no write only = no list = no auth users =rsync #配置登陸名稱 secrets file = /etc/rsync.passwd22 #配置用戶名密碼文件
2.創建共享目錄:/test/rsync
mkdri -pv /test/rsync
3、創建密碼文件,文件路徑和文件名參照配置文件里"secrets file"選項的值,然后添加密碼內容。
vim /etc/rsync.passwd22 rsync:654321
4、密碼文件的權限600
chmod 600 /etc/rsync.passwd22
5、運行rsync,并且開機啟動
rsync –daemon echo "rsync –daemon" >> /etc/rc.local
6、如果出錯,查看日志
tail /var/log/rsyncd.log
在B主機上配置:
1、密碼文件
vim /etc/rsync.passwd22 654321 #注意:此處只需要寫服務端虛擬帳號的密碼即可
2、密碼文件的權限600
chmod 600 /etc/rsync.passwd22
3.創建共享目錄:/test/rsync
mkdri -pv /test/rsync
4、安裝inotify-tools軟件包
yum -y install inotify-tools
5、編寫同步腳本
vim /root/bin/rsync.sh #!/bin/bash src=/test/rsync des=backup host="10.1.43.102" /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y%H:%M' --format '%T%w%f' -e modify,delete,create,attrib \ #此行尚未完結 $src | while read files; do for hostip in $host; do rsync -avz --delete --progress --password-file=/etc/rsync.passwd22 $src rsync@$hostip::$des done echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
6、后臺自動運行,并且開機自動啟動
nohup /bin/bash /root/bin/rsync.sh & echo "nohup /bin/bash /root/bin/rsync.sh &" >> /etc/rc.loacl
注意:以上配置在雙向傳輸數據時,只能增量傳輸,如果是刪除文件,則存在一定的問題
如果再配置過程中出現如下問題
問題一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端的目錄不存在或無權限,創建目錄并修正權限可解決問題
問題二:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端該模塊(backup)需要驗證用戶名密碼,但客戶端沒有提供正確的用戶名密碼,認證失敗
提供正確的用戶名密碼解決此問題
問題三:
@ERROR: Unknown module ‘backup'
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器不存在指定模塊。提供正確的模塊名或在服務器端修改成你要的模塊以解決問題
問題四:
password file must not beother-accessible
continuing without password file
Password:
原因:
這是因為rsyncd.pwdrsyncd.secrets的權限不對,應該設置為600。如:chmod600 rsyncd.pwd
問題五:
rsync: failed to connect to218.107.243.2: No route to host (113)
rsync error: error in socket IO(code 10) at clientserver.c(104) [receiver=2.6.9]
原因:
對方沒開機、防火墻阻擋、通過的網絡上有防火墻阻擋,都有可能。關閉防火墻,其實就是把tcp udp的873端口打開
問題六:
rsync error: error startingclient-server protocol (code 5) at main.c(1524) [Receiver=3.0.7]
原因:
/etc/rsyncd.conf配置文件內容有錯誤。請正確核對配置文件
問題七:
rsync: chown "" failed:Invalid argument (22)
原因:
權限無法復制。去掉同步權限的參數即可。(這種情況多見于Linux向Windows的時候)
問題八:
@ERROR: daemon security issue –contact admin
rsync error: error starting client-server protocol (code 5) at main.c(1530)[sender=3.0.6]
原因:
同步的目錄里面有軟連接文件,需要服務器端的/etc/rsyncd.conf打開use chroot = yes。掠過軟連接文件
原創文章,作者:megedugao,如若轉載,請注明出處:http://www.www58058.com/54979