cwRsync-windows下的rsync工具

软件版本

服务端:cwRsyncServer_4.1.0_Installer.exe

客户端:cwRsync_4.1.0_Installer.exe

应用场景

服务端:windows server 2016   IP : 192.168.67.1

客户端:windows10                   IP : 192.168.67.2

每次打开我的windows10时,自动同步server端的指定目录到本机,并且要在联网的情况下才同步文件。

 

服务端安装

运行 cwRsyncServer_4.0.5_Installer.exe ,一步步默认安装完毕后,

安装目录在 C:\Program Files (x86)\ICW

cwRsync-windows下的rsync工具_第1张图片

配置文件 rsyncd.conf 默认内容如下

use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log

# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
[test]
path = /cygdrive/c/work
read only = false
transfer logging = yes

其中

hosts allow  -- 设定允许访问本服务端的客户端的IP, *代表任何客户端都可以访问。

[test] -- 是一个初始的模块

path -- 模块对应的文件路径,这里work后面有没有正向斜线"/"都一样。后面客户端的中的斜线比较重要。

read only  -- 设置目录是否只读

transfer logging  -- 是否记录日志

现在,我打算修改原来的配置

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


[Repositories]
path =  /cygdrive/d/backup/Repositories
secrets file = /cygdrive/c/Program Files (x86)/ICW/rsyncd.secrets
list = true
auth users = user01
read only = false

上面我增加了

uid = 0 和 gid = 0 -- 如果不增加,从linux下同步文件过来会有损坏。

secrets file -- 该文件保存客户端使用的用户和密码

                      /cygdrive/c/ICW/rsyncd.secrets 是unix风格的文件路径,对应windows下的文件路径为 C:\Program Files (x86)\ICW\rsyncd.secrets,并且 rsyncd.secrets默认没有的,需要手动建立,文件内容的格式如:“username:password”

Windows Server 对权限管理更严格,服务端存放文件的目录需要做一定修改,将安装cwRsyncServer_4.0.5_Installer.exe时创建的服务器运行用户 SvcCWRSYNC 添加到该文件夹的用户列表里,并赋予全部权限。

cwRsync-windows下的rsync工具_第2张图片

 

在服务管理器中,名称为 RsyncServer ,将其启动类型设置为 自动。

cwRsync-windows下的rsync工具_第3张图片

最后启动服务。

服务端的默认监听端口为 873,若是想修改端口,只需要在服务端配置文件里的模块下指定--port=PORT即可。

cwRsync-windows下的rsync工具_第4张图片

所以还需要在防火墙里开启873端口。

 

客户端安装

运行 cwRsync_4.1.0_Installer.exe ,一步步默认安装完毕后,

安装目录在 C:\Program Files (x86)\cwRsync

cwRsync-windows下的rsync工具_第5张图片

客户端的同步脚本cwrsync.cmd默认内容如下

@ECHO OFF
REM *****************************************************************
REM
REM CWRSYNC.CMD - Batch file template to start your rsync command (s).
REM
REM By Tevfik K. (http://itefix.no)
REM *****************************************************************

REM Make environment variable changes local to this batch file
SETLOCAL

REM ** CUSTOMIZE ** Specify where to find rsync and related files (C:\CWRSYNC)
SET CWRSYNCHOME=%PROGRAMFILES%\CWRSYNC

REM Set HOME variable to your windows home directory. That makes sure 
REM that ssh command creates known_hosts in a directory you have access.
SET HOME=%HOMEDRIVE%%HOMEPATH%

REM Make cwRsync home as a part of system PATH to find required DLLs
SET CWOLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%

REM Windows paths may contain a colon (:) as a part of drive designation and 
REM backslashes (example c:\, g:\). However, in rsync syntax, a colon in a 
REM path means searching for a remote host. Solution: use absolute path 'a la unix', 
REM replace backslashes (\) with slashes (/) and put -/cygdrive/- in front of the 
REM drive letter:
REM 
REM Example : C:\WORK\* --> /cygdrive/c/work/*
REM 
REM Example 1 - rsync recursively to a unix server with an openssh server :
REM
REM       rsync -r /cygdrive/c/work/ remotehost:/home/user/work/
REM
REM Example 2 - Local rsync recursively 
REM
REM       rsync -r /cygdrive/c/work/ /cygdrive/d/work/doc/
REM
REM Example 3 - rsync to an rsync server recursively :
REM    (Double colons?? YES!!)
REM
REM       rsync -r /cygdrive/c/doc/ remotehost::module/doc
REM
REM Rsync is a very powerful tool. Please look at documentation for other options. 
REM

REM ** CUSTOMIZE ** Enter your rsync command(s) here

 针对前面的服务端配置,这里修改之后的内容如下

@echo off

: 设置传入脚本的参数PARA1
SET PARA1=%1%
: 设置客户端程序的家目录
: set RSYNC_CLIENT_HOME=C:\Program Files (x86)\cwRsync

SET RSYNC_HOME=C:\cwRsync
: 设置客户端程序传输文件时使用的密码,对应于后面SERVER_FILE中的user。
SET PASS_FILE=/cygdrive/c/cwRsync/rsync20190126.pass

: 设置日志文件路径
SET LOG_FILE=/cygdrive/c/cwRsync/rsync.log

: 指定远端服务端所使用的端口
SET PORT=873

: 指定远端服务中设定的用户,服务器IP,模块。格式必须是 username@IP::modulename
SET [email protected]::Repositories

: 指定客户端本地对应的文件存放路径,注意这里的末尾需要带上正向斜线"/",代表这是个目录,:
: REM 当将本地文件CLIENT_FILE推送到远端时,没有斜线则代表推送该文件夹,有斜线则代表推送该文件夹下面的文件。:
: 例外,若是从远端拉取文件到本地时,这个斜线有无都没有关系。所以,我们最好都加上斜线,代表我们同步该文件夹下的所有文件。:
SET CLIENT_FILE_PATH=/cygdrive/d/backup/

: 调用rsync程序执行文件同步,以下命令将%FILE1%同步到%FILE2%。
: %RSYNC_HOME%\bin\rsync -avzP   --delete-after --progress --password-file=%PASS_FILE% --log-file=%LOG_FILE% --port=%PORT%  %SERVER_FILE_PATH%  %CLIENT_FILE_PATH%

if "%PARA1%"=="get"  ( 
    echo  ====== starting %PARA1% files from %SERVER_FILE_PATH% .. ======
    %RSYNC_HOME%\bin\rsync -avzP   --delete-after --progress --password-file=%PASS_FILE% --log-file=%LOG_FILE% --port=%PORT%  %SERVER_FILE_PATH%  %CLIENT_FILE_PATH%
    echo  ====== successfully %PARA1% files !
	)
if "%PARA1%"=="pull" (
    echo  ====== starting %PARA1% files to %SERVER_FILE_PATH% .. ======
    %RSYNC_HOME%\bin\rsync -avzP   --delete-after --progress --password-file=%PASS_FILE% --log-file=%LOG_FILE% --port=%PORT%  %CLIENT_FILE_PATH%  %SERVER_FILE_PATH% 
	echo  ====== successfully %PARA1% files !
	)

pause

在cmd命令行中执行 cwrsync.cmd get 或者 cwrsync.cmd pull 

 

设置windows计划任务

打开“任务计划程序”,选中“任务计划程序库”,点击右侧“创建任务”。

cwRsync-windows下的rsync工具_第6张图片

 在“常规”标签下,输入名称,勾选“使用最高权限”,选择“Windows 10”。

cwRsync-windows下的rsync工具_第7张图片

 

cwRsync-windows下的rsync工具_第8张图片

 

cwRsync-windows下的rsync工具_第9张图片

 

cwRsync-windows下的rsync工具_第10张图片

cwRsync-windows下的rsync工具_第11张图片

保存成功后,可以在面板看到刚才创建的Rsync计划任务。

 

附录

rsync参数列表

rsync is a file transfer program capable of efficient remote update
via a fast differencing algorithm.

Usage: rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
  
The ':' usages connect via remote shell, 
while '::' & 'rsync://' usages connect to an rsync daemon, and require SRC or DEST to start with a module name.

frequently used:
 
 -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
 -r, --recursive             recurse into directories
 -l, --links                 copy symlinks as symlinks
 -p, --perms                 preserve permissions
 -t, --times                 preserve modification times
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
 -D                          same as --devices --specials
     --devices               preserve device files (super-user only)
     --specials              preserve special files
	 
 -e, --rsh=COMMAND           specify the remote shell to use	 
 -z, --compress              compress file data during the transfer
     --compress-level=NUM    explicitly set compression level
     --partial               keep partially transferred files	 
     --bwlimit=KBPS          limit I/O bandwidth; KBytes per second
 -v, --verbose               increase verbosity
 
 

less used:
	 

 -q, --quiet                 suppress non-error messages
     --no-motd               suppress daemon-mode MOTD (see manpage caveat)
 -c, --checksum              skip based on checksum, not mod-time & size
 
     --no-OPTION             turn off an implied OPTION (e.g. --no-D)
 -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         set backup suffix (default ~ w/o --backup-dir)
 -u, --update                skip files that are newer on the receiver
     --inplace               update destination files in-place (SEE MAN PAGE)
     --append                append data onto shorter files
     --append-verify         like --append, but with old data in file checksum
 -d, --dirs                  transfer directories without recursing
 -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 source tree
 -k, --copy-dirlinks         transform symlink to a dir into referent dir
 -K, --keep-dirlinks         treat symlinked dir on receiver as dir
 -H, --hard-links            preserve hard links
 -E, --executability         preserve the file's executability
     --chmod=CHMOD           affect file and/or directory permissions
 -A, --acls                  preserve ACLs (implies --perms)


 -O, --omit-dir-times        omit directories from --times
     --super                 receiver attempts super-user activities
 -S, --sparse                handle sparse files efficiently
 -n, --dry-run               perform a trial run with no changes made
 -W, --whole-file            copy files whole (without delta-xfer algorithm)
 -x, --one-file-system       don't cross filesystem boundaries
 -B, --block-size=SIZE       force a fixed checksum block-size

     --rsync-path=PROGRAM    specify the rsync to run on the remote machine
     --existing              skip creating new files on receiver
     --ignore-existing       skip updating files that already exist on receiver
     --remove-source-files   sender removes synchronized files (non-dirs)
     --del                   an alias for --delete-during
     --delete                delete extraneous files from destination dirs
     --delete-before         receiver deletes before transfer, not during
     --delete-during         receiver deletes during transfer (default)
     --delete-delay          find deletions during, delete after
     --delete-after          receiver deletes after transfer, not during
     --delete-excluded       also delete excluded files from destination dirs
     --ignore-errors         delete even if there are I/O errors
     --force                 force deletion of directories 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-dir=DIR       put a partially transferred file into DIR
     --delay-updates         put all updated files into place at transfer's end
 -m, --prune-empty-dirs      prune empty directory chains from the file-list
     --numeric-ids           don't map uid/gid values by user/group name
     --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 in size and mod-time
     --size-only             skip files that match in size
     --modify-window=NUM     compare mod-times with reduced accuracy
 -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 destination files relative to DIR
     --copy-dest=DIR         ... and include copies of unchanged files
     --link-dest=DIR         hardlink to files in DIR when unchanged

     --skip-compress=LIST    skip compressing files with a suffix in LIST
 -C, --cvs-exclude           auto-ignore files 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; only wildcard special-chars
     --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
     --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
     --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

     --stop-at=y-m-dTh:m     Stop rsync at year-month-dayThour:minute
     --time-limit=MINS       Stop rsync after MINS minutes have elapsed
     --write-batch=FILE      write a batched update to FILE
     --only-write-batch=FILE like --write-batch but w/o updating destination
     --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
     --tr=BAD/GOOD           transliterate filenames
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6
     --version               print version number
(-h) --help                  show this help (-h works with no other options)

 

你可能感兴趣的:(windows)