rsync是linux下的檔案同步服務,功能簡單來說就是服務端打開873端口,用戶端連接配接這個端口,并對伺服器端配置的目錄進行同步,可以了解為用戶端比對伺服器端資源後,對增量或者差異的資料進行增删改操作,功能支援上傳(推送)或下載下傳(擷取)比對,也就是遠端資料比對本地資料而後對遠端資料進行增删改操作,以及本地資料比對遠端資料然後對本地資料進行增删改操作。
centos6.3下預設已經安裝,隻需保證依賴服務xinetd開啟即可。
環境搭建:
注:首先關閉selinux與iptables
# vi /etc/sysconfig/selinux
---------
SELINUX=disabled
# setenforce 0
# service iptables stop
配置分為2個部分
server端:
1.安裝rsync(centos6.3預設已安裝)
# yum install rsync -y
# yum install xinetd -y
2.啟動rsync依賴服務
# /etc/init.d/xinetd start
# chkconfig xinetd on
3.配置/etc/rsyncd.conf
----------------------
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
port = 873
address = 172.24.40.30
[mail]
path = /home/domains/
comment = mirror for extmail
ignore errors
read only = no
list = no
auth users = user
secrets file = /etc/rsync.pas
hosts allow = 172.24.40.50
hosts deny = 0.0.0.0/0
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
--------------------------
# 表示可寫
# 端口
4.配置/etc/rsync.pas
在伺服器端,必須加入登陸名和密碼,在client上,隻需要輸入密碼
# vi /etc/rsync.pas
----------------
user:123456
---------------
并給與相關權限(重要,必須是600)
# chmod 600 /etc/rsync.pas
5.配置/etc/rsync.motd
# vi /etc/rsyncd.motd
welcome to use the rsync services!
6.啟動rsync
# rsync --daemon --config=/etc/rsyncd.conf
client端:
3.用戶端必須配置密碼檔案
------------------------
123456
4.然後在用戶端輸入指令同步:
下載下傳(擷取):
# rsync -auzv --progress --delete --password-file=/etc/rsync.pas [email protected]::mail /home/domains
上傳(推送):
# rsync -auzv --progress --delete --password-file=/etc/rsync.pas /home/domains/* [email protected]::mail
注:後面加 --port 873 可添加端口号資訊,若在配置檔案中自定義端口,這裡需要加--port參數。
請注意這部分:
[email protected]::mail /home/domains
user對應server端配置檔案的auth users = user
和server端/etc/rsync.pas内使用者名:密碼(user:123456)
172.24.40.30對應服務端IP位址
mail對應server端配置檔案的[mail]
/home/domains表示client端同步server端資料後資料儲存在client端目錄的路徑
@與::均起到字元連接配接功能
以上方法為明文傳輸,傳輸過程中密碼可能會被sniffer截取:
是以推薦線上使用ssh秘鑰認證+rsync加密傳輸,
按照上面的格式加密重寫(完整格式):
# rsync -auzvP --delete -e "ssh -p22" [email protected]:/home/domains/ /home/domains/ --port 873
# rsync -auzvP --delete -e "ssh -p22" /home/domains/ [email protected]:/home/domains/ --port 873
注: --progress 可以簡寫為 -P
5.定時計劃任務:
在crontab中增加一條指令,設定每分鐘自動執行一次。
# crontab -e
* * * * * /usr/bin/rsync -auzv --progress --delete --password-file=/etc/rsync.pas [email protected]::mail /home/domains
---------------------
6.為了安全,若系統啟用防火牆,建議增加一條iptables指令
# iptables -A INPUT -P tcp --dport:873 -j ACCEPT
7.如果需要多個不同目錄支援同步,再增加一個
[MYSQL]
path = /usr/local/mysql/data
comment = mysql mirror
read only = yes
auth users = root
重新開機rsync
# pkill rsync
然後在用戶端輸入指令同步
rsync -auzv --progress --delete --password-file=/etc/rsync.pas [email protected]::MYSQL /usr/local/mysql/data
-----------------大功告成-----------------------------
注:配置過程問題:
問題1:
在client上遇到問題:
rsync -auzv --progress --password-file=/etc/rsync.pas [email protected]::backup /home/
rsync: could not open password file "/etc/rsync.pas": No such file or directory (2)
Password:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到這個問題:client端沒有設定/etc/rsync.pas這個檔案,而在使用rsync指令的時候,加了這個參數--
password-file=/etc/rsync.pas
問題2:
遇到這個問題:client端已經設定/etc/rsync.pas這個檔案,裡面也設定了密碼111111,和伺服器一緻,但是
伺服器段設定有錯誤,伺服器端應該設定/etc/rsync.pas ,裡面内容root:111111 ,這裡登陸名不可缺少
問題3:
@ERROR: chdir failed
遇到這個問題,是因為伺服器端的/home/backup 其中backup這個目錄并沒有設定,是以提示:chdir failed
問題4:
rsync: write failed on "/home/backup2010/wensong": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7]
rsync: connection unexpectedly closed (2721 bytes received so far) [generator]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]
磁盤空間不夠,是以無法操作。
可以通過df /home/backup2010 來檢視可用空間和已用空間
問題5:網絡收集問題
1、權限問題
類似如下的提示:rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)注意檢視同步的目錄權限是否為755
2、time out
rsync: failed to connect to 203.100.192.66: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
檢查伺服器的端口netstat –tunlp,遠端telnet測試。
3、服務未啟動
rsync: failed to connect to 10.10.10.170: Connection refused (111)
啟動服務:rsync --daemon --config=/etc/rsyncd.conf
4、磁盤空間滿
rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
5、Ctrl+C或者大量檔案
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5]
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5]
6、xnetid啟動
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]
檢視rsync日志
rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
xnetid查找的配置檔案位置預設是/etc下,根據具體情況建立軟連結。例如:
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
或者更改指定預設的配置檔案路徑,在/etc/xinetd.d/rsync配置檔案中
本文轉自 showerlee 51CTO部落格,原文連結:http://blog.51cto.com/showerlee/1132724,如需轉載請自行聯系原作者