Rsync是一款不错的文件免费同步软件,可以镜像保存整个目录树和文件系统,同时保持原来文件的权限、时间、软硬链接。第一次同步时 rsync 会复制全部内容,下次只传输修改过的文件部分。传输数据过程中可以实行压缩及解压缩操作,减少带宽流量。支持scp、ssh及直接socket方式连接,支持匿名传输。支持Linux,Window平台。
客户端:cwRsync 4.0.3 Installer
服务端:cwRsyncServer 4.0.3 Installer
下载地址:
http://down.liberalism.top/cwRsyncServer_4.0.3_Installer.zip
http://down.liberalism.top/cwRsync_4.0.3_Installer.zip
拓扑:
ip | role | |
192.168.10.231 | cwRsyncServer | e:/web(同步source) |
192.168.10.109 | cwRsync | e:/web(同步destination) |
一、安装配置 Rsync 服务端
Window版服务端:
- 点击服务端安装程序进行安装,安装过程中提示输入服务端程序以服务运行时的用户名,密码。可以自定义,也可以用默认的用户名密码设置。我这里采用默认的。
- 安装完成之后,进入程序安装目录根目录,打开配置文件(如:E:\Program Files\ICW\rsyncd.conf ),进入配置。
use chroot = false strict modes = false hosts allow = * max connections = 30 port = 55555 #pid = 0 #uid = 0 log file = rsyncd.log pid file = rsyncd.pid lock file= rsyncd.lock # Module definitions # Remember cygwin naming conventions : c:\work becomes /cygwin/c/work # [web] path = /cygdrive/e/web auth users = web secrets file = /cygdrive/e/rsync.ps.txt read only = no list = no transfer logging = yes
以上Windows目录的写法,应该按照POSIX风格来写,默认配置文件的写法cygwin貌似不工作,而要写成cygdrive,如D:/data,要写成/cygdrive/d/data。
以上配置只允许192.168.1.21访问,这里按需修改。
strict modes = false 不验证用户密码, pid = 0,uid = 0指定匿名访问。 auth users : 指访问web的用户名 secrets file :web用户名对应的密码文件。
- 新建密码文件:E:\ rsync_db.ps 。如:
root:root admin:12345
密码文件格式:用户名:密码,一行一个,有的系统不支持长密码,另个密码文件的权限对其它用户组是不可读的,设置错了可能不工作。在Windows下,密码文件的访问权限一定要设置正确,不然用户验证的时候通不过。应将密码文件E:\ rsync_db.ps的权限加入Rsycn服务运行服务的用户名?cwRsyncServer 的读取权限 及 设置其为该文件为所有者。
- 在服务管理器中,找到服务 RsyncServer 服务,并启动服务。
- 如果开启了防火墙,则防火墙规则中要添加Tcp端口 55555 允许通信
- 服务验证,打开dos命令框,输入telnet 192.168.10.231 5555 (如果没有安装telnet服务端与客户端,请在控制面板->添加删除程序->打开关闭windows功能中找到Telnet客户端和服务端,勾选进行安装)。如果telnet能成功连接,出现@RSYNCD: 30.0 等类似文字,则说明服务启动正常。
二、安装配置 Rsync 客户端
- 安装Rsync客户端程序,直至安装完成。
- 测试服务器Rsync的连通性。在Rsync客户端所在计算机telnet Rsync服务端所在计算的相应地址和端口
略
- 打开Dos命令窗口,进到Rsync客户端安装目录的bin目录下,如:E:\Program Files\cwRsync\bin\。输入以下命令,开始进行同步:
E:\Program Files (x86)\cwRsync\bin>rsync -vzrt --port=55555 --progress --password-file=/cygdrive/e/rsync.ps.txt [email protected]::web /cygdrive/e/web/
参数说明:
rsync -vzrt --delete --port=55555 --progress --password-file=/cygdrive/e/rsync.ps.txt --exclude "/new" [email protected]::web /cygdrive/e/web/ --exclude "/new" 不同步/new 文件夹 -v, --verbose increase verbosity 显示详细的输出 -r, --recursive recurse into directories向下递归所有的目录 -R, --relative ? ? ? ? ? ? ?use relative path names -t, --times preserve modification times? 保留文件的修改时间 -z, --compress compress file data during the transfer?? 在数据传输过程中使用压缩 --delete delete extraneous files from dest dirs?? 在目的目录中,删除不相干的文件。?即,带上delete的参数后, 当源目录中的文件删除,同步后目标目录中的文件也会被删除
- 添加系统计划定期执行
添加一下文件和内容:
@echo off echo "开始同步!" cd E:\Program Files (x86)\cwRsync\bin e: rsync -vzrt --port=55555 --progress --password-file=/cygdrive/e/rsync.ps.txt [email protected]::web /cygdrive/e/web/ echo %date:~0,10%日%time%同步完成!>>e:\rsync.log echo.
将该文件放到E:\Program Files (x86)\cwRsync\bin下面
添加一个计划任务:
P.S:路径地址名称中不能有下划线,否则会报错
安装配置中常见问题
错误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下应将密码文件的所有者改成程序运行的用户。
执行完以后,如果还有错误提示,可以使用chown.exe命令将其文件的属主做下更改。具体操作如下:
服务端:
chmod.exe -c 600 /cygdrive/c/etc/password.txt
chown.exe SvcCWRSYNC /cygdrive/c/etc/password.txt
SvcCWRSYNC为windows上的cwrsync-server安装时默认新建的一个用户。
客户端:
chmod.exe -c 600 /cygdrive/c/etc/password.txt
chown.exe administrator /cygdrive/c/etc/password.txt
错误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 服务,开启服务。