Updated April 15, 2023
Introduction to Linux Rsync
Rsync is a utility in the command line which enables users to transfer and synchronize files efficiently between a computer and an external hard drive in the entire connected network. This utility is achieved by comparison of modification times and also the file size. This utility of the synchronization is written in C language, and this application executes as a single-threaded application. Another term you would hear a lot when someone talks about rsync is that this utility is a delta encoding. Delta encoding essentially a way of storage or transmission of data as a difference of the files or, in other terms, sequential data. This utility needs to be set up between the user and the host, and once the sync is set, thus utility will itself determine parts of the local file that needs to be transferred between them to keep the sync. In this topic, we are going to learn about Linux Rsync.
Syntax
Before we even jump into understanding what scenarios rsync would help or the working principle of rsync, it is beneficial to understand the syntax behind the rsync. Then, while going through the working principle, we can keep the syntax in mind.
rsync [-OPTION1] [-OPTION 2] [-OPTION 3] … [-OPTION n]SOURCE [USERNAME@]HOST:DESTINATION
OR
rsync [-OPTION1] [-OPTION 2] [-OPTION 3] … [-OPTION n][USERNAME@]HOST:SOURCE [DESTINATION]
The usage of any of the above syntax solely lies in the developer’s level of comfortability and confidence. Therefore, there is no benefit difference in using any syntax over the other. If you notice carefully, the difference only lies in the way the source address is mentioned, and each individual has their level of confidence on which one to be used, and hence the utility in the corresponding ways.
Here, SOURCE is the place where the files or directory will be copied from, and DESTINATION is the place where the files and directories get copied into. Now, these 2 parameters or arguments are the mandatory ones, whereas the ones in square brackets “[]” are optional and can be used as per the requirement of the functionality being developed. Next, we will talk about the majority of the options which are used extensively in the industry nowadays.
How does rsync work in Linux?
In the introduction, we already learned that rsync is a command-line utility that helps in synchronizing Unix clients present in the network. This utility comes in very handy in scenarios like backing up the files in a system to another system which is housed only for backing up the files. Rsync utility also has the capability to run in daemon mode, a mode that allows the application to run in the background rather than run under the active control of a user. Daemon mode out of scope for this article, but as an interesting fact, the syntax is read as: rsync://
From here, we will look at different OPTIONS we talked about in the syntax one by one, and we will try to accompany each OPTION or a combination of OPTIONS with a code snippet and the output that is expected corresponding to it!
OPTION | Where will it be used? |
-v, –verbose | This option is used if the user wants to know what exactly the computer is doing while executing the command. |
-q, –quiet | If the user doesn’t need the output message, this option can be used for suppressing the outputs. |
-a, –archive | This option can be used to archive files when synchronization is taking place. |
-r, –recursive | For recursively syncing the files and directories, this option can be used. |
-b, –backup | While the synchronization is taking place, if the backup of the files or delta is needed, even if there is a loss in communication, data leakage can be minimized. |
-u, –update | If the user doesn’t want the files at the destination to not update if the files there are newer than the source, this option can be used to successfully implement the use case. |
-l, –links | This option is used in scenarios where symbolic links (symlinks) should be copied as symlinks only. |
-n, –dry-run | This option is used when we would need to do a trial run without actual synchronization. This is done so that we understand the use case we are trying to solve and if the solution implemented actually caters to the problem. |
-e, –rsh=COMMAND | When this option is used, an alternative remote shell program is used for communication between the remote and local copies. |
-z, –compress | To minimize the data consumption during synchronization, this option can be used to compress the file being sent. |
-h, –human-readable | To display the output in a human-readable format for interpretation, this option is used. |
–progress | When the sync’s progress needs to be shown as an output, this option comes in very handy! |
–remove-source-files | In some cases, one would like to delete the file at the source so that there is no duplication of the files and space can be kept optimized. This is the option to get that utility! |
Examples of Linux Rsync
Given below are the examples of Linux Rsync:
Example #1
LOCAL COPY OR SYNC OF FILES
Code:
mkdir backup
ls -l ./backup/
rsync -zvh fileSync.txt ./backup/
ls -l ./backup/
Output:
In this scenario, we have utilized the combination of options listed above to have a verbose (-v) output during the rsync execution. In the highlighted version, the output is changed to a more human-readable format (-h). Also, during transfer, the text file was zipped (-z) so that the data transfer can be optimized to the maximum.
Example #2
LOCAL COPY OR SYNC OF FILES& DELETE FILE IN SOURCE
Code:
ls -l
rsync -zvh--remove-source-files fileSync.txt ./backup/
ls -l
Output:
In the case of, we see that fileSync.txt was present in the working directory in the initial instance. After the synchronization is complete, when we do an ls command, we see that the file has been moved from there to the new location as specified in the command, i.e. Backup.
Conclusion
In this article, we have gone through various options that are possible through the use of rsync in general. In cases of performing rsync on different IPS, only the user and the IP address of systems need to be added extra to complete the utility.
Recommended Articles
We hope that this EDUCBA information on “Linux Rsync” was beneficial to you. You can view EDUCBA’s recommended articles for more information.