Windwos cwRsync 服務端及客戶端安裝配置全攻略

本文由微酷網原創,所有內容均親自測試並投入實際應用中,原文地址:https://weiku.co/article/107/ ,轉載請註明出處,部分內容來自網絡,已在參考資料中列出。

微酷友情提示

這個 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 [email protected]::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 [email protected]::test /cygdrive/d/Admin/Desktop/autosync2 < D:/d/admin/desktop/rsync.passwd

指定端口

rsync -avz --delete /cygdrive/D/Uploads/ rsync://[email protected]:873/weiku/Uploads/ < D:/rsync.passwd

其中密碼文件 rsync.passwd 和服務端的不一樣,客戶端不需要用戶名,直接填寫密碼,如下:

password

從服務器拷貝文件到本地

rsync -avz --progress --delete -e "ssh -p 22" [email protected]:/www/weiku.co/ /cygdrive/d/webbackup

排除單個文件,如 d.cmd

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

排除多個文件或目錄

rsync -avz --progress --delete --exclude-from=/cygdrive/d/rsync.exclude --password-file=/cygdrive/d/rsync.passwd [email protected]::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 [email protected]::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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章