In daily operations, we often use rsync as a synchronization artifact. Sometimes when synchronizing two directories, it is required to delete files in the target directory that are more than the source directory, in which case the--delete parameter of rsync can be used to implement this requirement.
Like what:
Synchronize the/tmp/work directory on Server A to the/tmp/work directory of Remote Server B (A and B have already done the SSH no password trust jump relationship in advance), and delete the redundant files in the B server/tmp/work directory compared to a server/tmp/work
Recently, when processing the planning resource file, it is necessary to synchronize all directory a files to directory B files, and delete the redundant files in directory B. So, I think of the use of rsync--delete parameters to implement the function.
(1)a server
[Email protected]A ~]# cd/tmp/work
[[email protected]A work]# ls
A b c d 11
(2) B Server
[Email protected]B ~]# cd/tmp/work
[[email protected]B work]# ls
C D, FG 5t
(3) sync from a server to B server (assuming B server IP is 11.11.11.11)
[Email protected] work]# rsync-e "ssh-p22"-avpz--delete ./ [email protected]:/tmp/wo rk/ #注意, the--delete parameter should be placed in front of the source directory and the target directory, and the two directory structure must be consistent! cannot use./*
Sending incremental file list
./
deleting FG
deleting 5t
deleting 13
deleting 12
11
A
B
C
D
Sent 248 bytes received bytes 716.00 bytes/sec
Total size was 0 Speedup is 0.00
(4) Look at the B server again and find that it has been synchronized with the/tmp/work directory of a server and deleted the redundant files
[Email protected] ~]# cd/tmp/work
[[email protected] work]# ls
A b c d 11
********************************************************************************
Extended:
Here are some examples of how to use this:
$ mkdir {DIRA,DIRB}//Create two test directories
Create the appropriate files in two directories, respectively
$ Touch Dira/{filea1.txt,filea2.txt,filea3.txt}
$ Touch Dirb/{filea1.txt,filea2.txt,filea3.txt,fileb1.txt,fileb2.txt,fileb3.txt}
1) Dira all the files to the DIRB, and keep the owner of the file, belong to the group, file permissions and other information .
$ Rsync-avz dira/dirb/
Sending incremental file list
./
FileA1.txt
FileA2.txt
FileA3.txt
Sent 199 bytes received, bytes 542.00 bytes/sec
Total size was 0 Speedup is 0.00
2) Sync all dira files to DIRB and delete the extra files in DIRB
$ rsync-avz--delete dira/dirb/ #源目录和目标目录结构一定要一致!! It can't be dira/* dirb/ or dira/dirb/* or 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, Bytes 550.00 Bytes/sec
Total size was 0 Speedup is 0.00
3) Sync all dira files to DIRB, but in DIRB except fileB3.txt This file is not deleted, the others are removed.
$ 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, Bytes 550.00 Bytes/sec
Total size was 0 Speedup is 0.00
4) FileA1.txt and fileA2.txt within the Dira directory are not synchronized to the DIRB directory.
$ rsync-avz--exclude= "FileA1.txt"--exclude= "FileA2.txt" dira/dirb/
Sending incremental file list
FileA3.txt
Sent 106 bytes received bytes 274.00 bytes/sec
Total size was 0 Speedup is 0.00
5) FileA1.txt and fileA2.txt within the Dira directory are not synchronized to the DIRB directory, and unnecessary files are deleted in the DIRB directory.
$ 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 bytes 274.00 bytes/sec
Total size was 0 Speedup is 0.00
6) The FileA1.txt and FileA2.txt in the Dira directory are not synchronized to the DIRB directory, and the redundant files are deleted in the DIRB directory, and if DIRB and FileA2.txt are two files that are excluded from synchronization, they will still be deleted.
$ 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 bytes 286.00 bytes/sec
Total size was 0 Speedup is 0.00
Here you can see that only fileA3.txt is synchronized to the DIRB directory, while the DIRB directory of FileA1.txt and fileA2.txt two filtered files have also been deleted.
*********************************************************************************************************
To delete a huge amount of files under Linux, you need to delete hundreds of thousands of files. This is a log of the previous program, growing fast and useless. This time, we often use the delete command RM-FR * is not useful, because the time to wait too long. So we have to take some very good measures. We can use Rsync's--delete-before parameter to quickly delete a large number of files.
(1), create an empty folder:
Mkdir/tmp/test
(2), use rsync to delete the target directory:
rsync--delete-before-a-h-v--progress--stats/tmp/test/log/
The log directory that we want to delete will be emptied and deleted very quickly. Rsync actually uses the replacement principle, processing hundreds of thousands of files is also the second delete.
Option Description:
–delete-before the recipient to delete before transferring
–progress display the transfer process during transmission
-a archive mode that represents recursively transferring files and keeping all file attributes
-H Keep hard-connected files
-V Verbose output mode
–stats gives the transfer status of some files
When Rsync synchronizes, delete the destination directory than the source directory redundant file method (--delete)