rsync is a software application for Unix systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. (Thanks, Wikipedia.) Since this is supposed to be a quick tutorial, we’ll skip the formal lecture and get on with some usage examples.
1) sync a whole directory from one location on the local filesystem to another, while keeping all metadata such as ownership and permissions in tact:
rsync -azvrP /media/disk/source /home/user/destination
Important note: if source does not have a trailing slash, it will create the directory source/ inside destination/ — if it does have a trailing slash, it will copy only the contents of source but not source/ itself. This is similar to `cp /source` vs. `cp /source/*`
2) sync from a local machine over ssh to a remote server
rsync -zvrP /home/user/source user@10.10.10.100:
rsync uses ssh for remote connections by default, so there is nothing special you have to do besides the remote destination syntax which is identical to scp. (Obviously, sshd must be running on the server.) In this example there is nothing after the colon, meaning it defaults to the user’s home directory.
3) sync from a local machine over ssh to a remote server on a non-standard ssh port to a non-home directory
rsync -e ‘ssh -p 12345′ -zvrP /home/user/source root@10.10.10.100:/usr/local/
The -e flag takes a string identical to what you would use if you were using ssh manually. In this example, ‘ssh -p 12345′ connects using port 12345 on the server. The other difference is the destination directory on the end of the remote IP. To copy from a server to yourself, simply reverse source and destination.
4) sync one directory to another, except for files ending in .iso and .img
rsync zvrP –exclude ‘*.iso’ –exclude ‘*.img’ /home/user/source /media/destination/
Nothing new here except for the –exclude option. (Those are TWO dashes, not one! WordPress likes to change things without my permission.) This is useful for when you’re backing up a directory but don’t care to transfer huge files that you can easily re-download. For more details about fine-grained exclusion, see the rsync man page.
Command line options
Personally, about 95% of my rsync usage is accomplished with -zvrP. Below is a quick table of all the options seen in the previous examples.
-a: archive (implies -rlptoD)
-r: recursive
-l: copy symlinks as symlinks
-p: preserve permissions
-t: preserve modification times
-g: preserve group
-o: preserve owner (root only)
-D: (implies --devices --specials)
--devices: recreates character/block devices
(receiving end must be root)
--specials: transfer special files like named sockets
-z: compress during transfer
-v: verbose
-r: recursive
-P:
--partial: keep partially transferred files
--progress: show progress during transfer
-e: specify the remote shell to use
--exclude: excludes files matching PATTERN