Reference: http://www.thegeekstuff.com/2010/09/rsync-command-examples/
Note: In all the examples below, there is actually no space between-and-. please delete the space when using it.
Rsync is a remote data synchronization tool that allows you to quickly synchronize files between multiple hosts over a LAN or the Internet. Rsync is a tool used to replace rcp, which is currently maintained by rsync.samba.org. Rsync uses the so-called "Rsync algorithm" to synchronize files between local and remote hosts. This algorithm only transfers different parts of the two files, instead of transmitting the entire file at a time, therefore, the speed is quite fast.
Rsync features:
Basic Syntax:
rsync options source destination
Both the source and target can be local or remote. During remote transmission, you must specify the logon name, remote server, and file location.
Example:
1. Synchronize the two directories on the local machine
$ rsync -zvr /var/opt/installation/inventory/ /root/tempbuilding file list … donesva.xmlsvB.xml.sent 26385 bytes received 1098 bytes 54966.00 bytes/sectotal size is 44867 speedup is 1.63$
Parameters:
2. Use rsync-a to keep the time mark during Synchronization
Rsync option-a is called archive mode. perform the following operations:
$ rsync -azv /var/opt/installation/inventory/ /root/temp/building file list … done./sva.xmlsvB.xml.sent 26499 bytes received 1104 bytes 55206.00 bytes/sectotal size is 44867 speedup is 1.63$
3. Synchronize only one file
$ rsync -v /var/lib/rpm/Pubkeys /root/temp/Pubkeyssent 42 bytes received 12380 bytes 3549.14 bytes/sectotal size is 12288 speedup is 0.99
4. Synchronize files locally to a remote server
$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/Password:building file list … done./rpm/rpm/Basenamesrpm/Conflictnamesent 15810261 bytes received 412 bytes 2432411.23 bytes/sectotal size is 45305958 speedup is 2.87
As you can see, you need to add the ssh logon Method to the remote directory in the format of username @ machinename: path
5. Synchronize remote files to a local device
Similar to the above, do the opposite operation
$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/tempPassword:receiving file list … donerpm/rpm/Basenames.sent 406 bytes received 15810230 bytes 2432405.54 bytes/sectotal size is 45305958 speedup is 2.87
6. Specify Remote shell during Synchronization
You can use the-e parameter to specify remote ssh. For example, you can use rsync-e ssh to specify
$ rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/tempPassword:receiving file list … donerpm/rpm/Basenamessent 406 bytes received 15810230 bytes 2432405.54 bytes/sectotal size is 45305958 speedup is 2.87
7. Do not overwrite the modified target file
You can use the rsync-u option to exclude modified target files.
$ ls -l /root/temp/Basenamestotal 39088-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames$ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/tempPassword:receiving file list … donerpm/sent 122 bytes received 505 bytes 114.00 bytes/sectotal size is 45305958 speedup is 72258.31$ ls -lrttotal 39088-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames
8. Only Synchronize directory permissions and not synchronize files)
Use the-d Parameter
$ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ .Password:receiving file list … donelogrotate.statusCAM/YaST2/acpi/sent 240 bytes received 1830 bytes 318.46 bytes/sectotal size is 956 speedup is 0.46
9 view the transfer process of each file
Use-progress parameters
$ rsync -avz – -progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/Password:receiving file list …19 files to consider./Basenames5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19)Conflictname12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19)...sent 406 bytes received 15810211 bytes 2108082.27 bytes/sectotal size is 45305958 speedup is 2.87
10. delete the files created in the destination folder.
Use--delete Parameter
# Source and target are in sync. Now creating new file at the target.$ > new-file.txt$ rsync -avz – -delete thegeekstuff@192.168.200.10:/var/lib/rpm/ .Password:receiving file list … donedeleting new-file.txt./sent 26 bytes received 390 bytes 48.94 bytes/sectotal size is 45305958 speedup is 108908.55
11 do not create a new file in the target folder
You can use the-exiting parameter to only synchronize objects in the destination and exclude newly created files in the source file.
$ rsync -avz –existing root@192.168.1.2:/var/lib/rpm/ .root@192.168.1.2′s password:receiving file list … done./sent 26 bytes received 419 bytes 46.84 bytes/sectotal size is 88551424 speedup is 198991.96
12. View changes between source and target files
Use-I Parameters
$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/Password:receiving file list … done>f.st…. Basenames.f….og. Dirnamessent 48 bytes received 2182544 bytes 291012.27 bytes/sectotal size is 45305958 speedup is 20.76
In the output result, nine letters are displayed at the beginning of each file
> Transmitted
F indicates that this is a file.
D indicates that this is a directory.
S indicates the size is changed
T time mark changed
O User changed
G user group changed
13 enable include and exclude modes during transmission
$ rsync -avz – -include ‘P*’ – -exclude ‘*’ thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/Password:receiving file list … done./PackagesProvidenameProvideversionPubkeyssent 129 bytes received 10286798 bytes 2285983.78 bytes/sectotal size is 32768000 speedup is 3.19
14 do not transmit large files
Use the-max-size parameter
$ rsync -avz – -max-size=’100K’ thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/Password:receiving file list … done./ConflictnameGroupInstalltidNameSha1headerSigmd5Triggernamesent 252 bytes received 123081 bytes 18974.31 bytes/sectotal size is 45305958 speedup is 367.35
15 transfer all files
No matter whether it changes or not, transfer all the files again, with the-W Parameter
# rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/tempPassword:receiving file list … done./BasenamesConflictnameDirnamesFilemd5sGroupInstalltidNamesent 406 bytes received 15810211 bytes 2874657.64 bytes/sectotal size is 45305958 speedup is 2.87