Windwos cwRsync 服务端及客户端安装配置全攻略

此文章发布于 60 个月前,部分信息可能已经过时,请自行斟酌确认。

微酷友情提示

这个 windows 版的 rsync 可以说是微酷见过的最难搞的工具,各种问题,心脏不好真的会被爆掉的!!!做好心理准备。

Rsync 是什么

rsync 是一个 linux 下的快速、多功能、远程(本地)文件同步复制工具。

官方网站:http://rsync.samba.org/
维基百科:http://zh.wikipedia.org/wiki/Rsync

rsync 能同步更新两台计算机的档案与目录,并适当利用差分编码以减少数据传输。rsync 中一项与其他大部分类似程式或协定中所未见的重要特性是镜像对每个目标只需要一次传送。rsync可拷贝/显示目录属性,以及拷贝档案,并可选择性的压缩以及递归拷贝。

微酷一句话简单说:rsync 可实现两台计算机的数据差异同步,非常合适做备份用,也可用于发布网站等。

cwRsync 是什么

cwRsync 是 Rsync 在 Windows 上的实现版本,也是本文章的主角。


软件准备

服务端:cwRsyncServer-v4.1.0
客户端:cwRsync-v4.1.0(不要用5.5:cwRsync_5.5.0_x86_Free ,经折磨性测试最终发现 5.5 不适合微酷使用)

安装服务端

安装

直接一步一步安装至完成,其中会提示新建服务的用户SvcCWRSYNC,不用管直接下一步。

因为不喜欢多出一个用户SvcCWRSYNC,所以直接将这个用户删除,services.msc 服务中找到RsyncServer登录账号改成了Network Service,这时因为修改了用户,而服务要求对安装目录下有修改权限,因为要写pid文件(如配置中定义的rsyncd.pid),所以修改安装目录,Users用户可修改,否则服务启动不起来。

服务端为没有启动并且是手动启动模式,可以根据情况改成自动。

配置

编辑安装目录中服务端配置文件:rsyncd.conf

use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
pid file = rsyncd.pid
port = 873
uid = 0
gid = 0

# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
[test]
path = /cygdrive/d/Admin/Desktop/autosync
read only = false
transfer logging = yes
auth users = weiku-co-rsync
secrets file = weiku-co.passwd

其中需要新建密码配置文件 weiku-co.passwd 内容为:

weiku-co-rsync:password

其中冒号前是用户名,后面是密码,自己定义。
之后就可以在服务中启动服务了。

安装客户端

安装

直接解压到喜欢的目录,将bin添加到环境变量 path 中

建立 fstab 文件

建立和 bin 平级的etc/fstab文件添加以下代码,用于设置noacl权限解决windows文件权限混乱问题。

# 设置以下内容解决 Windows 文件权限混乱问题,但会造成密码文件一直提示权限太大:
none /cygdrive cygdrive binary,user,noacl,posix=0 0 0

建立 .ssh 目录

建立和 bin 平级的 home/(当前win用户如admin)/.ssh 目录用于实现 ssh 免密码登录

在客户端目录中有个 cwrsync.cmd 用来创建用户目录和环境变量,但微酷没用,前面环境变量已经添加,下面手动创建 home 目录。
和 bin 目录同级新建目录 home,进入后新建目录为当前登录用户名(如administrator、admin等),进入后按Shift+鼠标右键,在此处打开命令提示符,然后执行 mkdir .ssh 创建 .ssh 目录(因为 win 下没法直接创建点开头的目录)

生成 ssh 秘钥用于免密码连接

执行 bin 目录下 ssh-keygen.exe 可生成秘钥,默认为放在上一步建好的目录,如/home/admin/.ssh/
将生成的 id_rsa.pub 秘钥添加到服务器 authorized_keys 列表中如:/home/www/.ssh/authorized_keys。(注意拥有者为打算ssh的用户如微酷这里用www

注意:如果是用 www 用户的话需要开启ssh登录权限,否则一直会提示输入密码:usermod -s /bin/sh www

备注:如果后面用私钥时提示Permissions 0770 for '/home/Admin/.ssh/id_rsa' are too open.,则是因为私钥文件权限过大,将除拥有者以外的权限都删除即可。

这样在rsync执行ssh登录时会自动读取上面建立的 /home/admin/.ssh/ 下的私钥文件。不需要 -i 参数指定。

安装完成~


执行案例

无密码

rsync -avz --progress --delete rsync://192.168.1.100:873/test /cygdrive/d/Admin/Desktop/autosync2

有密码

rsync -avz --progress --delete --password-file=/cygdrive/d/admin/desktop/rsync.passwd ifu25-rsync@192.168.1.100::test /cygdrive/d/Admin/Desktop/autosync2

# 上面这种方式在存在 etc/fstab 文件时会提示密码文件权限过大,怎么设置都不行,最终找到方案可以用如下方式写
# 但经测试需要用 cwRsyncServer-v4.1.0.zip 中的 rsync.exe (v3.0.8),下载的高版本 cwRsync_5.5.0_x86_Free.zip 不行:

rsync -avz --progress --delete ifu25-rsync@192.168.1.100::test /cygdrive/d/Admin/Desktop/autosync2 < D:/d/admin/desktop/rsync.passwd

指定端口

rsync -avz --delete /cygdrive/D/Uploads/ rsync://username@xxx.com:873/weiku/Uploads/ < D:/rsync.passwd

其中密码文件 rsync.passwd 和服务端的不一样,客户端不需要用户名,直接填写密码,如下:

password

从服务器拷贝文件到本地

rsync -avz --progress --delete -e "ssh -p 22" www@weiku.co:/www/weiku.co/ /cygdrive/d/webbackup

排除单个文件,如 d.cmd

rsync -avz --delete --exclude "d.cmd" -e 'ssh -p 22' www@weiku.co:/www/weiku.co/Tpl/ /cygdrive/D/webbackup

排除多个文件或目录

rsync -avz --progress --delete --exclude-from=/cygdrive/d/rsync.exclude --password-file=/cygdrive/d/rsync.passwd ifu25-rsync@192.168.1.100::test /cygdrive/d//autosync

其中文件rsync.exclude内容为需要排除的文件或目录,可以多行,但经微酷使用中发现多行时第一行读不到,那就第一行写成注释吧:

# 第一行竟然不起作用,所以写个注释吧
node_modules
.gradle
.idea
.svn
.git
*.iml
build
release

本机双硬盘备份.bat

--本机同步就不用设置压缩了吧,去掉z参数
rsync -av --delete /cygdrive/d/Zone/ /cygdrive/H/AutoSync/Zone
rsync -av --delete --exclude-from=/cygdrive/d/rsync.exclude.e /cygdrive/e/ /cygdrive/H/AutoSync/E

特殊说明

  1. 同步目录经测试服务端中文目录会出错,提示:@ERROR: chdir failed,可用--iconv参数解决。见下。
  2. 配置文件中有中文可能不识别(文件编码为GB2312时),查资料发现可以指定参数--iconv=本地编码,远程编码来解决这个问题。然后又尝试将配置文件(如rsync.exclude)编码方式改为UTF8就可以识别中文了,并且不需要添加--iconv参数。(另发现密码文件编码格式要用默认的,微酷的为GB2312,如用UTF-8会提示:auth failed on module ****
  3. 忽略文件rsync.exclude中的第一行竟然不起作用,所以从第二行开始写,我是在第一行写了注释:# 第一行竟然不起作用,所以写个注释吧
  4. 同步的目录最后面有/就是同步里面的文件,没有/则会这个文件夹也会同步过去
  5. 目标服务器的路径中user@ip::name可以加/目录名,这样同步过去后会在指定module下面新建目录,可用于一个 module 同步多个本地目录,比如我家电脑做服务器,建了个module叫[WorkSync]用于备份办公室电脑文件,则从办公室同步时可写homeip::WorkSync/androidprojecthomeip::WorkSync/vsproject等等。
  6. 目录中有空格,可以将整个路径用双引号括起来,如:"/cygdrive/e/Visual Studio/Project/"
  7. Windows 下使用 cwrsync 同步文件夹的权限问题(其实是 cgywin 的问题):微酷在 windows 本机下双硬盘同步备份,结果发现备份后的目录无法打开,提示没权限,经查需要在 rsync.exe 的上级目录新建 etc 目录,然后建名为 fstab 的文件,文件中输入以下内容:(经微酷测试有效,但会造成密码认证文件就一直提示权限过大无法解决),另外安装的服务端目录里其实已经有 rsync.exe 和 fstab 文件了。实际上原理是禁止 Cygwin 采用 POSIX 体系的权限管理。
# 设置以下内容解决 Windows 文件权限混乱问题,但会造成密码文件一直提示权限太大
none /cygdrive cygdrive binary,user,noacl,posix=0 0 0

常见问题

问题:@ERROR: auth failed on module wwwroot
方案:经测试发现服务器和本地的密码文件编码格式必须同时设置为GB2312才行。

问题:@ERROR:chdir failed
方案:要同步到服务器上的目录,右键权限中没有 cwRsync 服务登录用户的权限(微酷设置的是 Network Service),添加权限就好了。

问题:ERROR: password file must not be other-accessible
方案:

妈的这个问题弄个整整一天,网上说是密码文件权限要设置成600,windows哪来的600,只设置了当前用户(admin)权限及拥有者,仍报这个错。各种折腾,终于发现办公室电脑可以,最终反复搞无果,把办公室电脑的cwRsync程序拷贝到家里,设置环境变量指向新拷贝的,竟然成功了。对比分析发现我家电脑cwRsync目录多着一个etc目录,里面有fstab文件,对,这是上次同步文件发现权限有问题后加的,没想到这个东东的原因,至今没有解决:有fstab时传输文件的权限就不混乱,但认证密码文件读不了,反过来一样,冲突。

终于解决了:使用 cwRsyncServer-v4.1.0 中自带的 rsync.exe 才行,将密码加到命令的最后面,如下:

rsync -avz --progress --delete ifu25-rsync@192.168.1.100::test /cygdrive/d/autosync < D:/d/rsync.passwd

妈蛋,这工具太坑了!!!!

问题:本地计算机 上的 RsyncServer 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。
方案:服务安装目录有个rsyncd.pid文件,删除后再尝试启动应该就 OK 了。不知道为什么经常这样。

参考文章

https://download.samba.org/pub/rsync/rsync.html
https://www.cnblogs.com/xwdreamer/p/3361647.html
https://www.cnblogs.com/l1pe1/p/4901031.html
https://blog.csdn.net/xygaj/article/details/78889777

附:Rsync 常用参数

-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-t, --times 保持文件时间信息
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete-after 传输结束以后再删除
--ignore-errors 及时出现IO错误也进行删除
--force 强制删除目录,即使不为空
--timeout=TIME IP超时时间,单位为秒
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件

附:Rsync 参数英文版

-v, --verbose               increase verbosity
     --info=FLAGS            fine-grained informational verbosity
     --debug=FLAGS           fine-grained debug verbosity
     --msgs2stderr           special output handling for debugging
 -q, --quiet                 suppress non-error messages
     --no-motd               suppress daemon-mode MOTD (see caveat)
 -c, --checksum              skip based on checksum, not mod-time & size
 -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
     --no-OPTION             turn off an implied OPTION (e.g. --no-D)
 -r, --recursive             recurse into directories
 -R, --relative              use relative path names
     --no-implied-dirs       don't send implied dirs with --relative
 -b, --backup                make backups (see --suffix & --backup-dir)
     --backup-dir=DIR        make backups into hierarchy based in DIR
     --suffix=SUFFIX         backup suffix (default ~ w/o --backup-dir)
 -u, --update                skip files that are newer on the receiver
     --inplace               update destination files in-place
     --append                append data onto shorter files
     --append-verify         --append w/old data in file checksum
 -d, --dirs                  transfer directories without recursing
 -l, --links                 copy symlinks as symlinks
 -L, --copy-links            transform symlink into referent file/dir
     --copy-unsafe-links     only "unsafe" symlinks are transformed
     --safe-links            ignore symlinks that point outside the tree
     --munge-links           munge symlinks to make them safer
 -k, --copy-dirlinks         transform symlink to dir into referent dir
 -K, --keep-dirlinks         treat symlinked dir on receiver as dir
 -H, --hard-links            preserve hard links
 -p, --perms                 preserve permissions
 -E, --executability         preserve executability
     --chmod=CHMOD           affect file and/or directory permissions
 -A, --acls                  preserve ACLs (implies -p)
 -X, --xattrs                preserve extended attributes
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
     --devices               preserve device files (super-user only)
     --specials              preserve special files
 -D                          same as --devices --specials
 -t, --times                 preserve modification times
 -O, --omit-dir-times        omit directories from --times
 -J, --omit-link-times       omit symlinks from --times
     --super                 receiver attempts super-user activities
     --fake-super            store/recover privileged attrs using xattrs
 -S, --sparse                turn sequences of nulls into sparse blocks
     --preallocate           allocate dest files before writing
 -n, --dry-run               perform a trial run with no changes made
 -W, --whole-file            copy files whole (w/o delta-xfer algorithm)
     --checksum-choice=STR   choose the checksum algorithms
 -x, --one-file-system       don't cross filesystem boundaries
 -B, --block-size=SIZE       force a fixed checksum block-size
 -e, --rsh=COMMAND           specify the remote shell to use
     --rsync-path=PROGRAM    specify the rsync to run on remote machine
     --existing              skip creating new files on receiver
     --ignore-existing       skip updating files that exist on receiver
     --remove-source-files   sender removes synchronized files (non-dir)
     --del                   an alias for --delete-during
     --delete                delete extraneous files from dest dirs
     --delete-before         receiver deletes before xfer, not during
     --delete-during         receiver deletes during the transfer
     --delete-delay          find deletions during, delete after
     --delete-after          receiver deletes after transfer, not during
     --delete-excluded       also delete excluded files from dest dirs
     --ignore-missing-args   ignore missing source args without error
     --delete-missing-args   delete missing source args from destination
     --ignore-errors         delete even if there are I/O errors
     --force                 force deletion of dirs even if not empty
     --max-delete=NUM        don't delete more than NUM files
     --max-size=SIZE         don't transfer any file larger than SIZE
     --min-size=SIZE         don't transfer any file smaller than SIZE
     --partial               keep partially transferred files
     --partial-dir=DIR       put a partially transferred file into DIR
     --delay-updates         put all updated files into place at end
 -m, --prune-empty-dirs      prune empty directory chains from file-list
     --numeric-ids           don't map uid/gid values by user/group name
     --usermap=STRING        custom username mapping
     --groupmap=STRING       custom groupname mapping
     --chown=USER:GROUP      simple username/groupname mapping
     --timeout=SECONDS       set I/O timeout in seconds
     --contimeout=SECONDS    set daemon connection timeout in seconds
 -I, --ignore-times          don't skip files that match size and time
     --size-only             skip files that match in size
 -@, --modify-window=NUM     set the accuracy for mod-time comparisons
 -T, --temp-dir=DIR          create temporary files in directory DIR
 -y, --fuzzy                 find similar file for basis if no dest file
     --compare-dest=DIR      also compare received files relative to DIR
     --copy-dest=DIR         ... and include copies of unchanged files
     --link-dest=DIR         hardlink to files in DIR when unchanged
 -z, --compress              compress file data during the transfer
     --compress-level=NUM    explicitly set compression level
     --skip-compress=LIST    skip compressing files with suffix in LIST
 -C, --cvs-exclude           auto-ignore files in the same way CVS does
 -f, --filter=RULE           add a file-filtering RULE
 -F                          same as --filter='dir-merge /.rsync-filter'
                             repeated: --filter='- .rsync-filter'
     --exclude=PATTERN       exclude files matching PATTERN
     --exclude-from=FILE     read exclude patterns from FILE
     --include=PATTERN       don't exclude files matching PATTERN
     --include-from=FILE     read include patterns from FILE
     --files-from=FILE       read list of source-file names from FILE
 -0, --from0                 all *from/filter files are delimited by 0s
 -s, --protect-args          no space-splitting; wildcard chars only
     --address=ADDRESS       bind address for outgoing socket to daemon
     --port=PORT             specify double-colon alternate port number
     --sockopts=OPTIONS      specify custom TCP options
     --blocking-io           use blocking I/O for the remote shell
     --outbuf=N|L|B          set out buffering to None, Line, or Block
     --stats                 give some file-transfer stats
 -8, --8-bit-output          leave high-bit chars unescaped in output
 -h, --human-readable        output numbers in a human-readable format
     --progress              show progress during transfer
 -P                          same as --partial --progress
 -i, --itemize-changes       output a change-summary for all updates
 -M, --remote-option=OPTION  send OPTION to the remote side only
     --out-format=FORMAT     output updates using the specified FORMAT
     --log-file=FILE         log what we're doing to the specified FILE
     --log-file-format=FMT   log updates using the specified FMT
     --password-file=FILE    read daemon-access password from FILE
     --list-only             list the files instead of copying them
     --bwlimit=RATE          limit socket I/O bandwidth
     --write-batch=FILE      write a batched update to FILE
     --only-write-batch=FILE like --write-batch but w/o updating dest
     --read-batch=FILE       read a batched update from FILE
     --protocol=NUM          force an older protocol version to be used
     --iconv=CONVERT_SPEC    request charset conversion of filenames
     --checksum-seed=NUM     set block/file checksum seed (advanced)
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6
     --version               print version number
(-h) --help                  show this help (see below for -h comment)
Rsync can also be run as a daemon, in which case the following options are accepted:


     --daemon                run as an rsync daemon
     --address=ADDRESS       bind to the specified address
     --bwlimit=RATE          limit socket I/O bandwidth
     --config=FILE           specify alternate rsyncd.conf file
 -M, --dparam=OVERRIDE       override global daemon config parameter
     --no-detach             do not detach from the parent
     --port=PORT             listen on alternate port number
     --log-file=FILE         override the "log file" setting
     --log-file-format=FMT   override the "log format" setting
     --sockopts=OPTIONS      specify custom TCP options
 -v, --verbose               increase verbosity
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6
 -h, --help                  show this help (if used after --daemon)
最后修改:2019 年 04 月 21 日 10 : 50 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论