天天看點

Rsync+linux用戶端+windows用戶端配置

Rsync+linux用戶端+windows用戶端配置

rsync是一個功能非常強大的工具,

Rsync的指令格式可以為以下六種:

  SYNOPSIS

      Local:  rsync [OPTION...] SRC... [DEST]

      Access via remote shell:

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

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

       Access via rsync daemon:

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

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

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

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

對應于以上六種指令格式,rsync有六種不同的工作模式:

  1)拷貝本地檔案。當SRC和DES路徑資訊都不包含有單個冒号":"分隔符時就啟動這種工作模式。如:rsync -a /data /backup

  2)使用一個遠端shell程式(如rsh、ssh)來實作将本地機器的内容拷貝到遠端機器。當DST路徑位址包含單個冒号":"分隔符時啟動該模式。如:rsync -avz *.c foo:src

  3)使用一個遠端shell程式(如rsh、ssh)來實作将遠端機器的内容拷貝到本地機器。當SRC位址路徑包含單個冒号":"分隔符時啟動該模式。如:rsync -avz foo:src/bar /data

  4)從遠端rsync伺服器中拷貝檔案到本地機。當SRC路徑資訊包含"::"分隔符時啟動該模式。如:rsync -av [email protected]::www /databack

  5)從本地機器拷貝檔案到遠端rsync伺服器中。當DST路徑資訊包含"::"分隔符時啟動該模式。如:rsync -av /databack [email protected]::www

  6)列遠端機的檔案清單。這類似于rsync傳輸,不過隻要在指令中省略掉本地機資訊即可。如:rsync -v rsync://172.17.5.13/www

rsync參數的具體解釋如下:

-v, --verbose 詳細模式輸出

-q, --quiet 精簡輸出模式

-c, --checksum 打開校驗開關,強制對檔案傳輸進行校驗

-a, --archive 歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性,等于-rlptgoD

-r, --recursive 對子目錄以遞歸模式處理

-R, --relative 使用相對路徑資訊

-b, --backup 建立備份,也就是對于目的已經存在有同樣的檔案名時,将老的檔案重新命名為~filename。可以使用--suffix選項來指定不同的備份檔案字首。

--backup-dir 将備份檔案(如~filename)存放在在目錄下。

-suffix=SUFFIX 定義備份檔案字首

-u, --update 僅僅進行更新,也就是跳過所有已經存在于DST,并且檔案時間晚于要備份的檔案。(不覆寫更新的檔案)

-l, --links 保留軟鍊結

-L, --copy-links 想對待正常檔案一樣處理軟鍊結

--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鍊結

--safe-links 忽略指向SRC路徑目錄樹以外的鍊結

-H, --hard-links 保留硬鍊結

-p, --perms 保持檔案權限

-o, --owner 保持檔案屬主資訊

-g, --group 保持檔案屬組資訊

-D, --devices 保持裝置檔案資訊

-t, --times 保持檔案時間資訊

-S, --sparse 對稀疏檔案進行特殊處理以節省DST的空間

-n, --dry-run現實哪些檔案将被傳輸

-W, --whole-file 拷貝檔案,不進行增量檢測

-x, --one-file-system 不要跨越檔案系統邊界

-B, --block-size=SIZE 檢驗算法使用的塊尺寸,預設是700位元組

-e, --rsh=COMMAND 指定使用rsh、ssh方式進行資料同步

--rsync-path=PATH 指定遠端伺服器上的rsync指令所在路徑資訊

-C, --cvs-exclude 使用和CVS一樣的方法自動忽略檔案,用來排除那些不希望傳輸的檔案

--existing 僅僅更新那些已經存在于DST的檔案,而不備份那些新建立的檔案

--delete 删除那些DST中SRC沒有的檔案

--delete-excluded 同樣删除接收端那些被該選項指定排除的檔案

--delete-after 傳輸結束以後再删除

--ignore-errors 及時出現IO錯誤也進行删除

--max-delete=NUM 最多删除NUM個檔案

--partial 保留那些因故沒有完全傳輸的檔案,以是加快随後的再次傳輸

--force 強制删除目錄,即使不為空

--numeric-ids 不将數字的使用者群組ID比對為使用者名群組名

--timeout=TIME IP逾時時間,機關為秒

-I, --ignore-times 不跳過那些有同樣的時間和長度的檔案

--size-only 當決定是否要備份檔案時,僅僅察看檔案大小而不考慮檔案時間

--modify-window=NUM 決定檔案是否時間相同時使用的時間戳視窗,預設為0

-T --temp-dir=DIR 在DIR中建立臨時檔案

--compare-dest=DIR 同樣比較DIR中的檔案來決定是否需要備份

-P 等同于 --partial

--progress 顯示備份過程

-z, --compress 對備份的檔案在傳輸時進行壓縮處理

--exclude=PATTERN 指定排除不需要傳輸的檔案模式

--include=PATTERN 指定不排除而需要傳輸的檔案模式

--exclude-from=FILE 排除FILE中指定模式的檔案

--include-from=FILE 不排除FILE指定模式比對的檔案

--version 列印版本資訊

--address 綁定到特定的位址

--config=FILE 指定其他的配置檔案,不使用預設的rsyncd.conf檔案

--port=PORT 指定其他的rsync服務端口

--blocking-io 對遠端shell使用阻塞IO

-stats 給出某些檔案的傳輸狀态

--progress 在傳輸時現實傳輸過程

--log-format=formAT 指定日志檔案格式

--password-file=FILE 從FILE中得到密碼

--bwlimit=KBPS 限制I/O帶寬,KBytes per second

-h, --help 顯示幫助資訊

一般都使用auz選項

在linux 172.17.5.13上

rpm安裝方法

[root@ Server_rsync ~]# rpm -qa | grep rsync

rsync-3.0.6-4.el5_7.1 

RHEL預設有安裝rsync, 可以編輯/etc/xinetd.d/rsync 後用service xinetd restart 來啟動。

[root@ Server_rsync ~ ~]# vim /etc/xinetd.d/rsync

 # default: off

# description: The rsync server is a good addition to an ftp server, as it \

#       allows crc checksumming etc.

service rsync

{

         disable    = no                                  預設yes 改成 no  

          socket_type     = stream

         wait            = no

         user            = root

         server          = /usr/bin/rsync

         server_args     = --daemon

         log_on_failure  += USERID

}

源碼安裝rsync方法:

去官網下載下傳http://rsync.samba.org/download.html最新版

[root@Server_rsync ~]#tar -xvzf rsync-3.1.1.tar.gz –C /usr/local

[root@Server_rsync ~]#cd  /usr/local/rsync-3.1.1/

[root@Server_rsync ~]#configure;make;make install

源碼安裝成功,隻是服務端啟動方法不同,配置檔案一樣,如下方法啟動:

/usr/local/rsync-3.1.1/rsync --daemon

配置檔案建立  rsync伺服器預設沒有 /etc/rsync.conf

[root@Server_rsync ~]# vim /etc/rsyncd.conf

motd file = /etc/rsyncd.motd                 歡迎資訊,可以不寫

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

port = 873                     port可以不加,預設為873端口,可以改為其它端口

uid = root                                   可以為nobody

gid = root

hosts allow = 192.168.0.0./16,172.17.0.0/16

hosts deny = *

max connections = 10

## 上面這段是全局配置,下面的子產品可以有多個

[files]                    子產品名字,自己命名

path = /backup/ftptap2delcom/

comment = rsync ftp                           這個是描述要共享的東東

ignore errors

read only = yes

list = no

auth users = rsync_test  同步驗證時用什麼賬号這個使用者和/etc/password沒有關系。

secrets file = /etc/rsyncd_pwd             指定認證檔案然後建立認證檔案

配置檔案結束,下面是修改密碼檔案。

[root@ Server_rsync ~]# vim /etc/rsyncd_pwd

rsync_test:2014?    

/etc/rsyncd_pwd 格式為使用者名:密碼。注意這個不是系統使用者,隻是rsync_test使用者。

[root@ Server_rsync ~]# chmod 600 /etc/rsync.passwd   隻能所有者可讀,否則報錯

[root@ Server_rsync ~]# service xinetd start

Starting             xinetd:             [  OK  ]

檢查rsync程序

[root@ Server_rsync ~]# netstat -anp | grep 873

tcp        0      0 0.0.0.0:873    0.0.0.0:*   LISTEN      3169/xinetd

在對rsync伺服器配置結束以後,下一步就需要在用戶端發出rsync指令來實作将伺服器端的檔案備份到用戶端來。

在Linux用戶端測試:

用戶端隻需要建立密碼檔案,檔案名可以自己命名,我這建立一個檔案為pwd

[root@Client_rsync ~]#touch /etc/pwd ;vim /etc/pwd

[root@Client_rsync ~]#  2014?                         用戶端隻須要密碼,不須要使用者名:密碼

[root@Client_rsync ~]#chmod 600 /etc/pwd              隻能所有者可讀,否則會報錯

[root@Client_rsync ~]# rsync –auz  --delete –password-file=/etc/pwd [email protected]::files  /backup_client/ftptap2delcom 

receiving file list ... rsync: readlink "/install.log" (in jason) failed: Permission denied (13)

rsync: readlink "/install.log.syslog" (in jason) failed: Permission denied (13)

rsync: readlink "/anaconda-ks.cfg" (in jason) failed: Permission denied (13)

。。。。。。

檢查用戶端 /backup_client/ftptap2delcom 檔案是否和伺服器端 /backup/ftptap2delcom同步。同步則OK

定時計劃

可以吧rsync –auz  --delete –password-file=/etc/pwd [email protected]::files  /backup_client/ftptap2delcom  這句寫到cron中去,做定時同步。

Windows用戶端

去官網下載下傳Windows  版 cwrsync

下載下傳位址為:https://www.itefix.net/cwrsync

下載下傳後解壓到C槽改名為rsync,在相應目錄建立密碼檔案PWD:

Rsync+linux用戶端+windows用戶端配置

可以CMD下用如下命測式:

C:\rsync\rsync.exe --port=873 -avz  --password-file=/cygdrive/c/rsync/pwd  [email protected]::files /cygdrive/d/ftptap2delcom 

在這個指令中cygdrive/d/為指定windows D盤,/cygdrive/d/ftptap2delcom相當于D:/ ftptap2delcom

定時運作

可以用一個bat腳本放到windows自帶計劃任務運作:

腳本名為rsync.bat

内容為:

@echo off

@C:\rsync\rsync.exe --port=873 -avz  --password-file=/cygdrive/c/rsync/pwd  rsyn  [email protected]::files /cygdrive/d/ftptap2delcom

Rsync+linux用戶端+windows用戶端配置

常見錯誤

如果不指定--password-file,就要互動式輸入密碼才能同步。

我們可以在client建立一個密碼檔案。

[root@station204 ~]# vim /etc/pwd

123456                    ## 隻要密碼就OK,不要使用者名。

[root@station204 ~]# chmod 600 /etc/pwd     ## 隻能所有者可讀,否則會報錯

[root@station204 ~]# rsync –auz  --delete –password-file=/etc/pwd [email protected]::files /backup_client/ftptap2delcom 

本文是 巧妙絕情 一個字一個圖打出來,參考了好多資料,感謝他們的分享,基于open source分享精神,轉載請注明出出。

支援我,請點選 巧妙絕情 謝謝

繼續閱讀