rsync同步時,刪除目標目錄比源目錄多餘檔案的方法(--delete)

散盡浮華發表於2016-08-12

 

在日常運維工作中,我們經常用到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 給出某些檔案的傳輸狀態

 

相關文章