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
--------------------------
# 表示可寫
read only = no
# 端口
port = 873
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端:
1.安裝rsync(centos6.3默認已安裝)
# yum install rsync -y
# yum install xinetd -y
2.啓動rsync依賴服務
# /etc/init.d/xinetd start
# chkconfig xinetd on
3.客戶端必須配置密碼文件
# vi /etc/rsync.pas
------------------------
123456
----------------------
並給與相關權限(重要,必須是600)
# chmod 600 /etc/rsync.pas
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參數。
請注意這部分:
user@172.24.40.30::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端目錄的路徑
@與::均起到字符連接功能
----------------
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
ignore errors
read only = yes
list = no
auth users = root
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
重啓rsync
# pkill rsync
# rsync --daemon --config=/etc/rsyncd.conf
然後在客戶端輸入命令同步
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:
rsync -auzv --progress --password-file=/etc/rsync.pas [email protected]::backup /home/
@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這個文件,裏面也設置了密碼111111,和服務器一致,但是
服務器段設置有錯誤,服務器端應該設置/etc/rsync.pas ,裏面內容root:111111 ,這裏登陸名不可缺少
問題3:
rsync -auzv --progress --password-file=/etc/rsync.pas [email protected]::backup /home/
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到這個問題,是因爲服務器端的/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 error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
啓動服務: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配置文件中
本文出自 “一路向北” 博客,請務必保留此出處http://showerlee.blog.51cto.com/2047005/1132724