Rsync是一款不錯的檔案免費同步軟體,可以鏡像儲存整個分類樹和檔案系統,同時保持原來檔案的許可權、時間、軟永久連結。第一次同步時 rsync 會複製全部內容,下次只傳輸修改過的檔案部分。傳輸資料過程中可以實行壓縮及解壓縮操作,減少頻寬流量。支援scp、ssh及直接socket方式串連,支援匿名傳輸。支援Linux,Window平台。寫本文的時候,window版最新版為4.0.5版
官網:http://rsync.samba.org/
Linux版下載:http://rsync.samba.org/download.html
Windows版下載:https://www.itefix.no/i2/cwrsync-get 選(Free Edition 免費版)
用戶端:cwRsync 4.0.5 Installer
服務端:cwRsyncServer 4.0.5 Installer
現在官方開始了收費版本,建議從雲棲社區下載免費版本
- cwRsync win 伺服器同步軟體 cwRsync_2.1.5_Installer.zip
- 伺服器資料同步 伺服器端 cwRsync_Server_2.1.5_Installer.zip
- 伺服器檔案同步 cwRsync_3.1.0_Installer.zip
- 伺服器檔案同步軟體 伺服器端 cwRsyncServer_3.1.0_Installer.zip
一、安裝配置 Rsync 服務端
Window版服務端:
1. 點擊服務端安裝程式進行安裝,安裝過程中提示輸入服務端程式以服務運行時的使用者名稱,密碼。可以自訂,也可以用預設的使用者名稱密碼設定。
2. 安裝完成之後,進入程式安裝目錄根目錄,開啟設定檔(如:C:\Program Files\ICW\rsyncd.conf ),進入配置。
複製代碼 代碼如下:
use chroot = false
strict modes = false
lock file = rsyncd.lock
hosts allow = 192.168.1.21
max connections = 5
port = 28950
pid = 0
uid = 0
log file = /cygdrive/f/RsyncLog/rsyncd.log
# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygdrive/c/work
[data_backup]
path = /cygdrive/f/dataBackup
auth users = dbbackuper
secrets file = /cygdrive/e/Setting/Rsync/rsync_db.ps
read only = no
list = no
transfer logging = yes
雲棲社區用的
複製代碼 代碼如下:
use chroot = false
strict modes = false
hosts allow = 222.186.55.55
log file = rsyncd.log
pid file = rsyncd.pid
# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
[dddown]
path = /cygdrive/d/webroot/downjb51
read only = false
transfer logging = yes
auto user=wtjb51
secrets file=wtjb51.conf
[scjb51]
path = /cygdrive/d/webroot/scjb51
read only = false
transfer logging = yes
auto user=wtjb51
secrets file=wtjb51.conf
以上Windows目錄的寫法,應該按照POSIX風格來寫,預設設定檔的寫法cygwin貌似不工作,而要寫成cygdrive,如D:/data,要寫成/cygdrive/d/data。
以上配置只允許192.168.1.21訪問,這裡按需修改。
strict modes = false 不驗證使用者密碼,
pid = 0,uid = 0指定匿名訪問。
auth users : 指訪問data_backup的使用者名稱
secrets file : data_backup使用者名稱對應的密碼檔案。
3. 建立密碼檔案:E:\Setting\Rsync\rsync_db.ps 。如:
root:root
admin:12345
密碼檔案格式:使用者名稱:密碼(root:12345),一行一個,有的系統不支援長密碼,另個密碼檔案的許可權對其它使用者組是不可讀的,設定錯了可能不工作。在Windows下,密碼檔案的存取權限一定要設定正確,不然使用者驗證的時候通不過。應將密碼檔案E:\Setting\Rsync\rsync_db.ps的許可權加入Rsycn服務運行服務的使用者名稱 cwRsyncServer 的讀取許可權 及 設定其為該檔案為所有者。
4. 在服務管理員中,找到服務 RsyncServer 服務,並啟動服務。
5. 如果開啟了防火牆,則防火牆規則中要添加Tcp連接埠 28950 允許通訊。
6. 服務驗證,開啟dos命令框,輸入telnet 192.168.1.20 28950 (如果沒有安裝telnet服務端與用戶端,請在控制台->添加刪除程式->開啟關閉windows功能中找到Telnet用戶端和服務端,勾選進行安裝)。如果telnet能成功串連,出現@RSYNCD: 30.0 等類似文字,則說明服務啟動正常。
二、安裝配置 Rsync 用戶端
1. 安裝Rsync用戶端程式,直至安裝完成。
2. 測試伺服器Rsync的連通性。在Rsync用戶端所在電腦telnet Rsync服務端所在計算的相應地址和連接埠
telnet 192.168.1.20 28950
出現@RSYNCD: 30.0 等類似文字,則說明用戶端串連服務端正常。
3. 開啟Dos命令視窗,進到Rsync用戶端安裝目錄的bin目錄下,如:C:\Program Files\cwRsync\bin\。輸入以下命令,開始進行同步:
cd C:\Program Files\cwRsync\bin\rsync --port=28950 -vzrtopg --progress --delete 192.168.1.20::data_backup /cygwin/f/dataBackup --password-file=/cygdrive/e/Setting/Rsync/rsync_db.ps
參數說明:
--port=28950 # 連接埠-vzrtopg --progress # 顯示同步過程詳細資料--delete # 從用戶端目錄中刪除與服務端目錄中不同的資料,保證兩邊資料完全一致/cygwin/f/dataBackup # Window下目錄F:\dataBackupdata_backup # 服務端設定檔rsyncd.conf檔案中定義的模組名稱192.168.1.20 # Rsync服務端IP地址
設定該命令檔案的使用者需要添加密碼檔案的讀取許可權及加其為檔案所有者。如下:
4. 添加系統計劃定期執行
建立命令執行檔案C:\Program Files\cwRsync\bin\SyncDB_NoAuth.cmd。將以下命令儲存到該檔案中:
rsync --port=28950 -vzrtopg --progress --delete 192.168.1.20::data_backup /cygwin/f/dataBackup --password-file=/cygdrive/e/Setting/Rsync/rsync_db.ps
在Window中新增工作計劃,不同的系統,操作有點不一樣。
windows xp/Server 2003 : 開始->設定->控制台->任務計劃->開啟新增工作計劃->下一步
windows 7/Server 2008 : 開始-> 控制台 -> 管理工具 -> 任務計劃
三、配置項解析
四、安裝配置中常見問題
錯誤1: rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(794) [receive r=3.0.2]
解決:很大可能是伺服器端沒有開啟 rsync 服務。開啟服務。
錯誤2:@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1495) [receiver=3.0.2]
解決:伺服器端同步處理目錄沒有許可權,cwrsync預設使用者是Svcwrsync。為同步目錄添加使用者Svcwrsync許可權。
錯誤3:@ERROR: failed to open lock file
rsync error: error starting client-server protocol (code 5) at main.c(1495) [receiver=3.0.2]
解決:設定檔 rsync.conf中添加 lock file = rsyncd.lock 即可解決。
錯誤4: rsync: could not open password file "/cygwin/e/Setting/Rsync/rsync_db.pwd": No such file or directory (2)
解決:密碼檔案的目錄一定要存在,而且要用POSIX風格的寫法:/cygdrive/e/Setting/Rsync/rsync_db.pwd
錯誤5:@ERROR: auth failed on module data_backup rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
解決:密碼錯誤,輸入正確的密碼即可。使用者名稱和密碼如果都正確,可能是遠程rsync伺服器的帳戶密碼檔案的許可權必須為600。
錯誤6: password file must not be other-accessible
解決:這是因為rsyncd.pwd rsyncd.sec的許可權不對,應該設定為600。如:chmod 600 rsyncd.pwd, Windows下應將密碼檔案的所有者改成程式啟動並執行使用者。
錯誤7:@ERROR: invalid uid nobody . rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
解決:在rsyncd.conf檔案中添加下面兩行即可
uid = 0
gid = 0
問題8: @ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:伺服器端的目錄不存在或無許可權。建立目錄並修正許可權可解決問題。
問題9:@ERROR: Unknown module ‘tee_nonexists'
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:伺服器不存在指定模組。提供正確的模組名或在伺服器端修改成你要的模組以解決問題。
問題10:rsync: failed to connect to 218.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連接埠開啟。
問題11:rsync error: error starting client-server protocol (code 5) at main.c(1524) [Receiver=3.0.7]
原因:/etc/rsyncd.conf設定檔內容有錯誤。請正確核對設定檔。
問題12:rsync: chown "" failed: Invalid argument (22)
原因:許可權無法複製。去掉同步許可權的參數即可。(這種情況多見於Linux向Windows的時候)
問題13:@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。掠過軟串連檔案。
問題14:rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(794) [receiver=3.0.2]
解決:很大可能是伺服器端沒有開啟 rsync 服務,開啟服務。
rsyncserver 服務啟動時報錯“rsyncserver服務啟動後又停止了。一些服務自動停止,如果它們沒有什麼可做的,例如“效能記錄檔及警示”服務。”
解決方案:將安裝目錄下的rsyncd.pid檔案刪除,再重新啟動RsyncServer服務。一般是異常關機導致的。
為了省事雲棲社區小編直接寫了一個批處理,定時檢查rsync伺服器端有沒有運行,沒有就運行即可。
複製代碼 代碼如下:
@echo off
for /f "usebackq" %%i in (`"tasklist|find /c "cygrunsrv.exe""`) do (
set chkstat=%%i
)
if %chkstat% == 0 (
del "D:\Program Files\cwRsyncServer\rsyncd.pid" /f
net start RsyncServer
echo ================ >> %date:~0,10%.log
echo %date% %time% check rsync service stop >> rsync%date:~0,10%.log
echo restart rsync service! >> %date:~0,10%.log
echo ================ >> %date:~0,10%.log
)