windows 2003下 rsync排除指定目錄或檔案進行同步

來源:互聯網
上載者:User

很常見的情況:我想同步/myweb下的 一些php檔案 ,  但是不想複製/myweb/log/裡邊的一些記錄檔,因為這些檔案太大了,備份也是沒有意義的。 現在如果想避開某個路徑  直接添加—exclude 即可 比如—exclude “./log” –exclude ‘./log/file’
Note: the directory path is relative to the folder you are backing up.

rsync備份時排除指定目錄或檔案

要排除指定目錄,可以在用戶端命令列中使用–exclude-from命令


例如
rsync -vzrtopg –progress –delete –exclude-from=/home/pcfile user@11.22.33.44 /back/xxx

pcfile檔案中,每條記錄佔一行,支援萬用字元
/tmp         ## 排除名為 tmp 的根目錄
.[a-z]*     ## 不備份以點開頭的隱藏檔案

注意:這個路徑必須是一個相對路徑,不能是絕對路徑


例子:原始伺服器/home/yjwan/bashshell有一個checkout檔案夾

[root@CentOS5-4 bashshell]# ls -dl checkout

drwxr-xr-x 2 root root 4096 Aug 21 09:14 checkou

現在想要完全避開複製這個檔案夾內容怎麼辦?

目標伺服器執行

rsync -av –exclude “checkout” yjwan@172.16.251.241:/home/yjwan/bashshell /tmp

將不會複製這個檔案夾

[root@free /tmp/bashshell]# ls -d /tmp/bashshell/checkout

ls: /tmp/bashshell/checkout: No such file or directory


注意:


1事實上,系統會把檔案和檔案夾一視同仁,如果checkout是一個檔案,一樣不會複製


2 如果想避開複製checkout裡面的內容,可以這麼寫—exclude “checkout/123”


3 切記不可寫為 —exclude “/checkout”這樣絕對路徑

這樣寫 將不會避免checkout被複製

比如

[root@free /tmp/bashshell]# rsync -av –exclude “/checkout” yjwan@172.16.251.241:/home/yjwan/bashshell /tmp

receiving file list … done

bashshell/checkout/


4可以使用萬用字元 避開不想複製的內容

比如—exclude “fire*”

那麼fire打頭的檔案或者檔案夾全部不會被複製

5如果想要避開複製的檔案過多,可以這麼寫

–exclude-from=/exclude.list


exclude.list 是一個檔案,放置的位置是絕對路徑的/exclude.list ,為了避免出問題,最好設定為絕對路徑。


裡面的內容一定要寫為相對路徑


比如 我想避開checkout檔案夾和fire打頭的檔案


那麼/exclude.list 寫為

checkout

fire*

然後執行以下命令,注意寫為–exclude-from或者–exclude-from=都可以

但是不能為—exclude

rsync -av –exclude-from=”/exclude.list” yjwan@172.16.251.241:/home/yjwan/bashshell /tmp

檢查結果:確實避開了checkout檔案夾和fire打頭的檔案

問題:如何計算對比複製以後的檔案數量是否正確呢?
1 查看錯誤記錄檔,看是否複製時候出問題了

2在原始伺服器執行可知道具體檔案和檔案夾的總個數

ls –AlR|grep “^[-d]”|wc

然後目標伺服器在計算一遍個數

看看數字是不是能對的上就ok了

對不上再研究怎麼回事

3現在的問題是:如果我使用了—exclude參數就麻煩了

我怎麼知道要複製幾個檔案?

首先,前面命令時候提到過一種寫法,就是只有源地址,沒有目標地址的寫法,這種寫法可以用來列出所有應該被複製的檔案

那麼用這個命令,可以計算出這個/root/bashshell下面檔案和檔案夾數量

在伺服器端執行

[root@CentOS5-4 bashshell]# rsync -av /root/bashshell/ |grep “^[-d]” | wc

62     310    4249

和ls 得到的結果一致的

[root@CentOS5-4 bashshell]# ls -AlR |grep “^[-d]“|wc

62     558    3731

因此,比如說我不要fire 打頭的檔案,可以在伺服器端先這樣計算要複製的檔案

[root@CentOS5-4 bashshell]# rsync -av –exclude “fire*” /root/bashshell/ |grep “^[-d]” | wc

44     220    2695

然後複製過去

看目標機器的檔案和檔案夾數量為

[root@free /tmp]# ls -AlR /tmp/bashshell/  |grep “^[-d]“|wc

44     396    2554

可以知道2者是同步的

問題:Rsync的其他幾個常見參數
1

-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

壓縮傳輸,如果網路頻寬不夠,那麼應該壓縮以後傳輸,消耗的當然是機器資源,但是如果內網傳輸的話,檔案數量不是很多的話,這個參數不必要的。

2

-password-file=FILE

前面說過了,只有遠端機器是rsync伺服器,才能用這個參數

如果你以為個FILE寫的是ssh 登陸的密碼,那就大錯特錯了,不少人犯了這個錯誤。


3

–stats: Adds a little more output regarding the file transfer status.

4

–progress: shows the progress of each file transfer. Can be useful to know if you have large files being backup up.


當然也可以rsync同步指定目錄了,具體如下

rsync使用–include參數與–exclude參數來實現同步指定目錄,並且–exclude目錄放在後面。

例如:

linux上檔案目錄

ls /usr/local/apache/htdocs/site
aa
bb
cc
dd
ee
..
zz
只想同步aa,bb,ee這三個目錄,其他的不同步。

實現代碼:

/usr/local/rsync/bin/rsync -azv  --include "aa/" --include "bb/" --include "ee"  --exclude "/*"  --progress rsync://127.0.0.1/site /work/backup/htdocs_bak/site

說明:

–include “aa/” –include “bb/” –include “ee” 包含了你要同步的目錄 –exclude包含了不同步的目錄所有用“/*”指定。
–exclude “/*” 除了–include同步的目錄外,排除其他目錄
rsync://127.0.0.1/site 為你rsync服務端要同步的目錄
/work/backup/htdocs_bak/site 為你用戶端的目錄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.