歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> 用rsync實現windows與linux文件同步的方法

用rsync實現windows與linux文件同步的方法

日期:2017/3/2 16:32:10   编辑:Linux服務器
windows做為文件服務器,使用rsync的windows服務版本:cwRsyncServer 下載地址:http://rsync.samba.org

安裝過程要設置用於系統服務的帳號和密碼,可以默認。
注:此帳號用於啟用crsync server服務,需要分配給帳號對要同步文件的相應權限,否則無法操作被同步的文件。

安裝完成後修改配置文件 rsyncd.conf, 配置文件內容如下:
. 代碼如下:
port = 52326
use chroot = false
strict modes = false
#hosts allow = * #允許所有的訪問
hosts allow = 192.168.10.2 #指定特定的IP允許訪問
log file = rsyncd.log
lock file = rsyncd.lock
max connections = 10
UID = 0
GID = 0
# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
[test]
path = /cygdrive/c/work
read only = false
transfer logging = no

[test2]
path = /cygdrive/d/wwwroot/yanghengfei_com
read only = yes
transfer logging = no
auth users = coldstar #同步使用的帳號
secrets file = rsyncd.secrets #密碼文件

注:
rsyncd.secrets配置文件的格式為 用戶名:密碼,如:
coldstar:123456

即添加了一個用戶 coldstar,密碼為 123456。

Linux服務器上執行同步命令:

rsync -vzrtopg --progress --delete rsync://[email protected]:52326/test2 /root/test2

然後在password: 提示符下輸入密碼即可。

也可以把密碼寫入配置文件引用,如:
rsync -vzrtopg --progress --delete --password-file=/etc/rsync.pass rsync://[email protected]:52326/test2 /www/users/yanghengfei_com

將以上命令寫入計劃任務,每1分鐘執行,即可實現實時同步的效果。

注:
上面這個命令行中-vzrtopg裡的v是verbose,z是壓縮,r是recursive,topg都是保持文件原有屬性如屬主、時間的參數。-- progress是指顯示出詳細的進度情況,--delete是指如果服務器端刪除了這一文件,那麼客戶端也相應把文件刪除,保持真正的一致。

附,rsync使用時的常見問題:

錯誤1: 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 服務。開啟服務。 或者開啟了防火牆指定的端口無法訪問。

錯誤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]
解決:服務器端配置文件 rsyncd.conf中添加 lock file = rsyncd.lock 即可解決。

錯誤4:@ERROR: invalid uid nobody
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.2]
解決:在rsyncd.conf文件中添加下面兩行即可解決問題
UID = 0
GID = 0

錯誤5:@ERROR: auth failed on module test2
rsync error: error starting client-server protocol (code 5) at main.c(1296) [receiver=3.0.2]
解決:服務端沒有指定正確的secrets file,請在 [test2]配置段添加如下配置行:
auth users = coldstar #同步使用的帳號
secrets file = rsyncd.secrets #密碼文件

錯誤6:password file must not be other-accessible
解決:客戶端的pass文件要求權限為600, chmod 600 /etc/rsync.pass 即可。
Copyright © Linux教程網 All Rights Reserved