RSync or Remote Sync is the Linux command usually used for backup of files/directories and synchronizing them locally or remotely in an efficient way. One of the reasons of why RSync is preferred over all other alternatives is the speed of operation, RSync copies the chunk of data to other location at a significantly faster rate. This is because, whenever Rsync is executed for the very first occasion, it transfers all the data from source to the destination. On the next turn, it would just copy the files/directories whose contents are changed.
Another plus point of using this utility is, as it makes use of SSH protocol to encrypt the data to be replicated, so it is much more secure and trustworthy. One more advantage of using Rsync is, as it performs compression of the data at source end and decompresses it at the destination, the bandwidth used during the sync operation will be considerably less. Also, the file permissions, their user/group information and the timestamps is/can be preserved.
rsync [OPTIONS] SOURCE DESTINATION
-v : indicates Verbose mode which provides detailed information.
-r : indicates Recursive operation, timestamps and file permissions are not preserved
-a : indicates Archive mode,timestamps and file permissions are preserved
-z : indicates Compression, it compresses the data before it is transferred to destination.
-h : indicates Human Readable output format.
1. Sync Files and Directories on Local MachineIn the following command, we will synchronize the file
/var/log/syslogfrom the local machine to the
/home/mandar/logfilesdirectory on the same machine. Here, as a file is single element of the directory, there is no need of using
In the next command, we will sync all the elements in the
$ rsync -zvh /var/log/syslog /home/mandar/logfiles/
/home/mandardirectory inside the
/root/mandar-bkpdirectory on the same machine.
$ rsync -avzr /home/mandar/ /root/mandar-bkp/
2. Sync Files and Directories to a Remote MachineIn order to mention a remote host as a
DESTINATION, we will follow a syntax :
USER@REMOTE-HOST-IP-ADDR/HOSTNAME:/path/to/file-or-directory. It's always better to use IP Address of the remote host.
For a file synchronization,
To sync a directory,
$ rsync -zvh /home/mandar/osimages/ubuntu-trusty.iso email@example.com:/home/iso/
$ rsync -avz /home/mandar/osimages/ firstname.lastname@example.org:/home/iso/
3. Sync Files and Directories from a Remote MachineThis will be similar to the previous command, Remote host will be the
SOURCEand our local machine will be the
$ rsync -avz email@example.com:/home/mandar/docs/ /home/mandar/linux/docs/
4. Sync Files and Directories using SSH ProtocolWhen synchronizing critical files, you can ensure the transfer to be safe and secure by encrypting the data using SSH protocol. You can mention the protocol you wish to use using the option
$ rsync -avze ssh firstname.lastname@example.org:/home/mandar/projectX/ /home/mandar/imp/
5. Display Progress of the OperationThe
--progressoption will display the files that are being transferred or those which are already synchronized along with the timing details.
$ rsync -avz --progress email@example.com:/home/mandar/docs/ /home/mandar/linux/docs/
6. Include and Exclude Files and DirectoriesThe
--excludeoptions will let you sync selected files/directories from a chunk of data. Both of these options expect a pattern,
'*'here. The following command will sync all the files whose names start with
ubuntuand leave alone all other files and directories.
$ rsync -avz --include 'ubuntu*' --exclude '*' /home/mandar/osimages/ firstname.lastname@example.org:/home/iso
7. Delete Already Existing Files and Directories at DestinationSometimes, it might happen that, a file which is not present at the source, but is present at the destination. So, you might want the file to be deleted from the target host. In this case, you can use the option
$ rsync -avz --delete email@example.com:/home/mandar/projectX/ /home/mandar/imp/
8. Limit the Maximum Size of the FilesIn order to avoid transfer of larger files, you can limit the maximum size of the files that are to be transferred using the option
$ rsync -avzr --max-size='200K' /var/logs/ firstname.lastname@example.org:/home/mandar/logfiles/
9. Remove Source Files after ReplicationThe
--remove-source-filesoption will delete the data that is replicated to the destination host, after successful synchronization.
$ rsync -avz --remove-source-files email@example.com:/home/mandar/projectX /home/mandar/imp/
10. Limit the Bandwidth UsageOne can also allocate the maximum limit of bandwidth that should be used while synchronizing the data, using option
--bwlimit. The bandwidth limit will be in terms of Kilo Bytes Per Seconds (KBPS).
$ rsync -avz --bwlimit=100 /home/mandar/osimages/ firstname.lastname@example.org:/home/iso
11. Not Overwriting Changed Files at DestinationIf the modified version of some file is already available at the destination, you might not want to overwrite the already existing file at the destination. In this case, you can use the option
$ rsync -avzu email@example.com:/home/mandar/projectX/ /home/mandar/imp/
12. Replicate Only DirectoriesIn order to synchronize only the directories recursively; but not the files included in those directories, you can use option
$ rsync -avzd firstname.lastname@example.org:/home/mandar/projectX/ /home/mandar/imp/
That's all for this article. This is just the tip of the iceberg. There are plenty of options those are not included in this article, as this article was intended to highlight basic use of the RSync command. For more details, you can always refer to the man pages.
Thanks for reading and stay tuned for more articles on Linux Commands.