天天看點

rsync詳解之exclude排除檔案

問題:如何避開同步指定的檔案夾?  --excludersync  --exclude files and folders

​​http://articles.slicehost.com/2007/10/10/rsync-exclude-files-and-folders​​

很常見的情況:我想同步/下的 /usr   /boot/ ,  但是不想複制/proc  /tmp 這些檔案夾

如果想避開某個路徑  直接添加--exclude 即可

比如--exclude “proc”

--exclude ‘sources’

Note: the directory path is relative to the folder you are backing up.

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

例子:源伺服器/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” ​​[email protected]:/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” ​​[email protected]:/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” ​​[email protected]:/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

然後複制過去

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

繼續閱讀