Window 上的 rsync

最後更新: 2018-11-06

 

目錄

  • Window 上的 rsync
  • cwRsync_5.3.0_Free
  • ICW rsync Server
  • cygwin Client 使用
  • Backup script
  • --exclude-from
  • Other rsync Client

 


Window 上的 rsync

 

cygwin - rsync

建議用它. 因為它是 100% free 的, 而且安裝時可以安埋其他工具(wget, grep ...)

cwRsync

官網: https://www.itefix.no/i2/cwrsync

  • Server 端安裝程式: cwRsyncServer_4.0.2_Installer
  • Client 端安裝程式: cwRsync_4.0.2_Installer

 


cwRsync_5.3.0_Free

 

Capabilities:

64-bit files, 64-bit inums, 32-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks,

IPv6, batchfiles, inplace, append, ACLs, no xattrs, iconv, symtimes, prealloc

'::' & 'rsync://'

rsync.exe --version

rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST

 

 


ICW rsync Server

 

安裝後, 設定檔在

C:\Program Files\ICW\rsyncd.conf

並且會建立另一個帳戶來啟動 Service

  • SvcCWRSYNC

一般設定如下:

# 一定要加上它, 否則會有 @ERROR Invalid UID nobody.
uid = 0
gid = 0

# 如果用了, 會有以下 Error
# file has vanished: "/proc" (in backup)
# rsync: readdir("/cygdrive" (in backup)): Bad address (14)
use chroot = no

# secrets.txt 的權限為 other 時, 那會有以下 Error
# secrets file must not be other-accessible (see strict modes option)
strict modes = false
hosts allow = *
log file = rsyncd.log

# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
[backup]

    # IP 限制功能
    # rsync denied on module backup from unknown (192.168.88.150)
    hosts allow = 192.168.88.150

    # 此 Path 可以有空格
    path =  /cygdrive/c/working folder
    # 此目錄必須被 Windows 帳戶 .\SvcCWRSYNC 讀取    
    # 否則會 2013/01/08 09:27:32 [4480] rsync: chdir /cygdrive/c/work failed: Permission denied (13)
    read only = true
    auth users = backup
    # secrets file 的 Path 有空格會出事的, 比如 C:\Program Files\ICW
    secrets file = secrets.txt
    transfer logging = yes

LOG:

rsyncd.log

啟動:

2013/01/03 14:53:49 [4592] rsyncd version 3.0.9 starting, listening on port 873

rsync_service:

 

測試:

bin\rsync.exe --config=rsyncd.conf --daemon --no-detach

 


cygwin Client 使用

 

rsync.exe -arvz /cygdrive/[localDir] [user]@[host]:[remoteDir]

Test-1

rsync /cygdrive/c/tmp/a      /cygdrive/c/tmp/c

rsync /cygdrive/c/tmp/a/     /cygdrive/c/tmp/c

Output:

skipping directory a

結論:

rsync 指令本身只對檔案有效

 

Test-2a

rsync -r /cygdrive/c/tmp/a /cygdrive/c/tmp/c

Output:

C:\tmp>dir c /b

a

 

Test-2b:

rsync -r /cygdrive/c/tmp/a    /cygdrive/c/tmp/c

rsync -r /cygdrive/c/tmp/a/  /cygdrive/c/tmp/c

結論:

Src 最尾的一 "/" 很影響, 加了 "/" 相當是指那目錄下的內容

 

Example3: 透過 SSH 去 rsync (":")

 

rsync -r /cygdrive/c/work/ remotehost:/home/user/work/

 

Example4: rsync with password file (--password-file) & MODULE ("::")

 

rsync -a --password-file  "/cygdrive/c/cwRsync_5.3.0_Free/key.txt"  "/cygdrive/c/test"   USER@HOST::MODULE

 


Backup Script

 

backup.bat

@echo off
REM use rsync to backup data to NAS

set server=remote.server
set user=MyUser
set module=MyModule

REM # path to backup
set source=/cygdrive/C/vhosts

REM # backup.bat, pw.txt, excludefile.txt file location
set scriptpath=/cygdrive/c/scripts

REM # Log file path
set WinLogfile=c:\scripts\backup.log
set LinLogfile=%scriptpath%/backup.log

REM # ======== Code ========

REM # cleanup screen
CLS

set pwfile=%scriptpath%/pw.txt
set excludefile=%scriptpath%/excludefile.txt
set remote=rsync://%user%@%server%/%module%/
set PATH=%PATH%;C:\cygwin\bin

REM # cygwin path 來
touch %excludefile%

REM # Remove log file
rm -f %LinLogfile%
del %WinLogfile%

REM # Stop Services
sc stop MSSQLSERVER

REM # Sleep 5 sec
timeout 5 /NOBREAK

echo Start time: %time%

REM # backup data
rsync -azvqHS --delete ^
      --password-file=%pwfile% ^
      --log-file=%LinLogfile% ^
      --exclude-from=%excludefile% ^
      %source% %remote%
      
REM # keep log on remote site
rsync -zq --password-file=%pwfile% ^
    %LinLogfile% %remote%

REM # Start Service
sc start MSSQLSERVER

echo Finish time: %time%

excludefile.txt 內容(格式)

/pagefile.sys
/System Volume Information
/$RECYCLE.BIN
Thumbs.db

Remark

[1] 要 stop service, 否則有 Err

sc stop MSSQLSERVER

rsync.exe ...

sc start MSSQLSERVER

Err:

rsync: send_files failed to open "/cygdrive/c/HS/HS_Data/RBMGoldenHK_log.LDF": 
 Device or resource busy (16)

 


--exclude-from

 

 * 如果 Filename 有空格, 亦不用加 ""

 * Foldername 有 "$", 比如 $RECYCLE.BIN 亦不用加 "\"

 * 在 exclude 時, test 不代表 test.txt (如果要用 pattern, 那就要 test.*)

 * 如果只要 exclude 頂層的 file, 就要用 /test.txt

 * /test 代表整個 folder. 如果想 keep folder 而不 sync 內容, 就要用 /test/*

 


Other rsync Client

 

rsync.net Windows Backup Agent

# V6.65 的 Delete 功能唔 work, 唔建議用

https://www.rsync.net/resources/howto/windows_backup_agent.html