rsync詳解之exclude排除文件
問題:如何避開同步指定的文件夾? --exclude rsync --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
現(xiàn)在想要完全避開復制這個文件夾內(nèi)容怎么辦?
目標服務器執(zhí)行
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、事實上,系統(tǒng)會把文件和文件夾一視同仁,如果checkout是一個文件,一樣不會復制
2 、如果想避開復制checkout里面的內(nèi)容,可以這么寫--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、可以使用通配符 避開不想復制的內(nèi)容
比如--exclude “fire*”
那么fire打頭的文件或者文件夾全部不會被復制
5、如果想要避開復制的文件過多,可以這么寫
--exclude-from=/exclude.list
exclude.list 是一個文件,放置的位置是絕對路徑的/exclude.list ,為了避免出問題,最好設置為絕對路徑。
里面的內(nèi)容一定要寫為相對路徑
比如 我想避開checkout文件夾和fire打頭的文件
那么/exclude.list 寫為
checkout
fire*
然后執(zhí)行以下命令,注意寫為--exclude-from或者--exclude-from=都可以
但是不能為--exclude
rsync -av --exclude-from=”/exclude.list” yjwan@172.16.251.241:/home/yjwan/bashshell /tmp
檢查結(jié)果:確實避開了checkout文件夾和fire打頭的文件
問題:如何計算對比復制以后的文件數(shù)量是否正確呢?
1、查看錯誤日志,看是否復制時候出問題了
2、在源服務器執(zhí)行可知道具體文件和文件夾的總個數(shù)
ls –AlR|grep “^[-d]”|wc
然后目標服務器在計算一遍個數(shù)
看看數(shù)字是不是能對的上就ok了
對不上再研究怎么回事
3、現(xiàn)在的問題是:如果我使用了--exclude參數(shù)就麻煩了
我怎么知道要復制幾個文件?
首先,前面命令時候提到過一種寫法,就是只有源地址,沒有目標地址的寫法,這種寫法可以用來列出所有應該被復制的文件
那么用這個命令,可以計算出這個/root/bashshell下面文件和文件夾數(shù)量
在服務器端執(zhí)行
[root@CentOS5-4 bashshell]# rsync -av /root/bashshell/ |grep “^[-d]” | wc
62 310 4249
和ls 得到的結(jié)果一致的
[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
然后復制過去
看目標機器的文件和文件夾數(shù)量為
[root@free /tmp]# ls -AlR /tmp/bashshell/ |grep “^[-d]“|wc
44 396 2554
可以知道2者是同步的
問題:Rsync的其他幾個常見參數(shù)
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
壓縮傳輸,如果網(wǎng)絡帶寬不夠,那么應該壓縮以后傳輸,消耗的當然是機器資源,但是如果內(nèi)網(wǎng)傳輸?shù)脑挘募?shù)量不是很多的話,這個參數(shù)不必要的。
2、
--password-file=FILE
前面說過了,只有遠端機器是rsync服務器,才能用這個參數(shù)
如果你以為個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.
關于這個參數(shù):
I frequently find myself adding the -P option for large transfers. It preserves partial transfers in case of interuption, and gives a progress report on each file as it’s being uploaded.
I move large media files back and forth on my servers, so knowing how long the transfer has remaining is very useful.
•Previous Entry: nginx 每天定時切割Nginx日志的腳本
•Next Entry: 如何開啟MySQL的遠程帳號
相關文章
Visual Studio Code 使用Git進行版本控制(圖文教程)
這篇文章主要介紹了Visual Studio Code 使用Git進行版本控制(圖文教程),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
CentOS與Ubuntu哪個更適合做服務器系統(tǒng)
對于服務器而言,有兩個主流的 Linux 發(fā)行版,那就是 CentOS 和 Ubuntu。但如何從這兩個之中選擇,這是擺在管理員、初學者和專業(yè)人士面前的主要問題。在對這兩個(和更多)發(fā)行版有了一定的經(jīng)驗之后,我們決定對這兩個發(fā)行版用于服務器時做個比較2017-04-04
Apache Hudi結(jié)合Flink的億級數(shù)據(jù)入湖實踐解析
這篇文章主要為大家介紹了Apache Hudi結(jié)合Flink的億級數(shù)據(jù)入湖實踐解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-03-03
解決IntelliJ IDEA maven庫下載依賴包速度慢的問題
下面小編就為大家分享一篇解決IntelliJ IDEA maven庫下載依賴包速度慢的問題,具有很好的參考價值。希望對大家有所幫助2017-11-11

