What is Rsync and how to use it ? With Examples

   Rsync is an open source utility that provides fast incremental file transfer. Rsync is typically used to synchronize files and directories between two different systems.
Base Syntax:
rsync local-file user@remote-host:remote-file
or
rsync options source destination
When on localhost only. rsync

Why rsync is so good ?

  • Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
  • Security: rsync allows encryption of data using ssh protocol during transfer.
  • Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
  • Privileges: No special privileges are required to install and execute rsync.

Now that we know what is rsync and why should we use it let us go over some use examples.

 1 - How to Synchronize Files From Local to Remote rsync allows you to synchronize files/directories between the local and remote system but both hosts must have rsync installed on them
 rsync -az /tmp/files/ aodba@122.111.111.11:/tmp/file/
Password:
Note: the -a option stand for "archive mode" , -z "compress file data during the transfer" Note: while doing rsync with the remote server, you need to specify username and ip-address of the remote server. You should also specify the destination directory on the remote server. 2 - How to Synchronize Files From Local to Local
rsync -a /tmp/files /tmp/files2
- all file from folder files will be copied to files2. 3 - How to Synchronize Files From Remote to Local When you want to synchronize files from remote to local, specify remote path in source and local path in target as shown below.
rsync -az aodba@111.111.221.11:/tmp/files /tmp/fileslocal
Password:
4 - How to Synchronize Only One File
rsync -a /tmp/files/file1.txt /tmp/files2
Note: you must specify the file name at the source 5 - How to Synchronize only the Directory Tree Structure (not the files) You can sync just directories by excluding everything else.
#local
rsync -av -f"+ */" -f"- *" /tmp/source /tmp/dest/

#remote
rsync -av -f"+ */" -f"- *" /tmp/source/ aodba@123.111.123.12:/tmp/dest/
6 - How to View the rsync Progress during Transfer with Rsync For this you need to add the --progress option
rsync -avz --progress [email protected]:/tmp/files /tmp/fileslocal

Password:
7 - How to Delete the Files Created at the Target with Rsync If a file is not present at the source, but present at the target, you might want to delete the file at the target during rsync. In that case, use –delete option. Rsync delete option deletes files that are not there in source directory.
rsync -avz --delete [email protected]:/tmp/files /tmp/fileslocal

Password:
8 - How to restrict Large Files transfers with Rsync We can tell rsync not to transfer files that are greater than a specific size using rsync –max-size option.
rsync -avz --max-size='50K' [email protected]:/tmp/files /tmp/fileslocal

Password:
We just put a limit on the max file size to be allowed to be transferred. There more complex operations that rsync can be used for but i just covered some basic ones, fell free post any rsync examples that you find useful.