rsync同步時,刪除目標目錄比源目錄多余文件的方法(--delete)


在日常運維工作中,我們經常用到rsync這個同步神器。有時在同步兩個目錄時,會要求刪除目標目錄中比源目錄多出的文件,這種情況下,就可用到rsync的--delete參數來實現這個需求了。

實例說明:
在服務器A上同步/tmp/work目錄到遠程服務器B的/tmp/work目錄下(A和B已經提前做好ssh無密碼信任跳轉關系了),同時刪除B服務器/tmp/work目錄下相比於A服務器/tmp/work中多余的文件
最近在處理策划資源文件的時候需要將目錄A的文件全部同步到目錄B的文件,並且把目錄B內多余的文件全部刪除掉。所以,就想到了使用rsync的--delete參數來實現功能。

1)A服務器

[root@serverA ~]# cd /tmp/work
[root@serverA work]# ls
a b c d 11

2)B服務器
[root@serverB ~]# cd /tmp/work
[root@serverB work]# ls
c d 11 12 13 fg 5t

3)從A服務器同步到B服務器(假設B服務器ip是11.11.11.11)
[root@serverA work]# rsync -e "ssh -p22" -avpz --delete  ./ root@11.11.11.11:/tmp/work/    #注意,--delete參數要放在源目錄和目標目錄前,並且兩個目錄結構一定要一致!不能使用./*
sending incremental file list
./
deleting fg
deleting 5t
deleting 13
deleting 12
11
a
b
c
d

sent 248 bytes received 110 bytes 716.00 bytes/sec
total size is 0 speedup is 0.00

4)再次查看B服務器,發現已經跟A服務器的/tmp/work目錄同步了,並且刪除了多余的文件
[root@serverB ~]# cd /tmp/work
[root@serverB work]# ls
a b c d 11

********************************************************************************

擴展:

下面根據示例說明幾個用法:

$ mkdir {dirA,dirB} //創建兩個測試目錄

//分別在兩個目錄創建相應的文件

$ touch dirA/{fileA1.txt,fileA2.txt,fileA3.txt}

$ touch dirB/{fileA1.txt,fileA2.txt,fileA3.txt,fileB1.txt,fileB2.txt,fileB3.txt}

1)將dirA的所有文件同步到dirB內,並保留文件的屬主,屬組,文件權限等信息

$ rsync -avz dirA/ dirB/

sending incremental file list

./

fileA1.txt

fileA2.txt

fileA3.txt

sent 199 bytes received 72 bytes 542.00 bytes/sec

total size is 0 speedup is 0.00

2)將dirA的所有文件同步到dirB內,並刪除dirB內多余的文件

$ rsync -avz --delete dirA/ dirB/       #源目錄和目標目錄結構一定要一致!!不能是dirA/* dirB/  或者dirA/ dirB/*  或者 dirA/* dirB/*

sending incremental file list

./

deleting fileB3.txt

deleting fileB2.txt

deleting fileB1.txt

fileA1.txt

fileA2.txt

fileA3.txt

sent 203 bytes received 72 bytes 550.00 bytes/sec

total size is 0 speedup is 0.00

3)將dirA的所有文件同步到dirB,但是在dirB內除了fileB3.txt這個文件不刪之外,其他的都刪除。

$ rsync -avz --delete --exclude "fileB3.txt" dirA/ dirB/

sending incremental file list

./

deleting fileB2.txt

deleting fileB1.txt

fileA1.txt

fileA2.txt

fileA3.txt

sent 203 bytes received 72 bytes 550.00 bytes/sec

total size is 0 speedup is 0.00

4)將dirA目錄內的fileA1.txt和fileA2.txt不同步到dirB目錄內。

$ rsync -avz --exclude="fileA1.txt" --exclude="fileA2.txt" dirA/ dirB/

sending incremental file list

fileA3.txt

sent 106 bytes received 31 bytes 274.00 bytes/sec

total size is 0 speedup is 0.00

5) 將dirA目錄內的fileA1.txt和fileA2.txt不同步到dirB目錄內,並且在dirB目錄內刪除多余的文件。

$ rsync -avz --exclude="fileA1.txt" --exclude="fileA2.txt" --delete dirA/ dirB/

sending incremental file list

deleting fileB3.txt

deleting fileB2.txt

deleting fileB1.txt

fileA3.txt

sent 106 bytes received 31 bytes 274.00 bytes/sec

total size is 0 speedup is 0.00

6)將dirA目錄內的fileA1.txt和fileA2.txt不同步到dirB目錄內,並且在dirB目錄內刪除多余的文件,同時,如果dirB內有fileA2.txt和fileA1.txt這兩個被排除同步的文件,仍然將其刪除。

$ rsync -avz --exclude="fileA1.txt" --exclude="fileA2.txt" --delete-excluded dirA/ dirB/

sending incremental file list

./

deleting fileB3.txt

deleting fileB2.txt

deleting fileB1.txt

deleting fileA2.txt

deleting fileA1.txt

fileA3.txt

sent 109 bytes received 34 bytes 286.00 bytes/sec

total size is 0 speedup is 0.00

這里可以看到只有fileA3.txt被同步到dirB目錄內,同時dirB目錄內的fileA1.txt和fileA2.txt兩個被過濾的文件也被刪除掉了。

*********************************************************************************************************

要在Linux下刪除海量文件的情況,需要刪除數十萬個文件。這個是之前的程序寫的日志,增長很快,而且沒什么用。這個時候,我們常用的刪除命令rm -fr * 就不好用了,因為要等待的時間太長。所以必須要采取一些非常手段。我們可以使用rsync的--delete-before參數來實現快速刪除大量文件。

1)建立一個空的文件夾:
mkdir /tmp/test
2)用rsync刪除目標目錄:
rsync --delete-before -a -H -v --progress --stats /tmp/test/ log/
這樣我們要刪除的log目錄就會被清空了,刪除的速度會非常快。rsync實際上用的是替換原理,處理數十萬個文件也是秒刪。

選項說明:
--delete-before 接收者在傳輸之前進行刪除操作
--progress 在傳輸時顯示傳輸過程
--a 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性
--H 保持硬連接的文件
--v 詳細輸出模式
--stats 給出某些文件的傳輸狀態


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM