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:
or
When on localhost only.
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
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
- 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.
4 - How to Synchronize Only One File
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.
6 - How to View the rsync Progress during Transfer with Rsync
For this you need to add the --progress option
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.
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.
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.