What is Rsync?
Rsync is a network-enabled utility that is highly flexible in terms of syncing files and directories. It is included in most of the Unix and Linux distributions as a popular utility for system scripts and its widespread use on these operating systems.
Now, let’s check out the basic syntax of the rsync command.
Rsync Syntax:
Here “Option” refers to rsync options, “Source” for the source directory, and “Destination” for the destination directory.
To demonstrate to you the method of syncing a directory in Linux, we created two test directories, “testdir1” and “testdir2,” in our system.
1. Rsync directory locally:
First of all, list out all the directory content to get to know the files or folders. We have selected “testdir1” for this purpose.
Rsync directory with the “-r” option:
For direct syncing, utilize the recursive “-r” option in the rsync command. The below-given syntax will sync the content from the source to its destination directory. The “/” points towards the content of the source directory. Without this slash, the rsync command will place the source directory within the destination directory.
Write out this command to start local syncing a directory to another.
$ ls -al ./testdir2/
List out the “testdir2” content to view the changes made by rsync.
Rsync directory with the “-a” option:
You can also utilize various options or flags that rsync supports. “-a” flag is one of them. This flag helps the rsync command sync a directory recursively and maintain the device files, modification times, symbolic links, owner, group, and file permissions.
$ ls -al ./testdir2/
Rsync directory with the “-v” option:
To show the rsync process on the terminal “-v” option is used with the rsync command.
To check the difference that comes by using a “/” at the end of the source directory, write out this command:
The output clearly states that now the source directory is itself transferred.
Rsync directory with the “-P” option:
The “-P” shows the progress of the syncing directory by displaying the progress bar on the terminal.
You can also utilize the same command for resuming any interrupted transfers.
To check if using the same rsync command only sync the modification we made in the directory, create some test files in your source directory.
After that, execute the same rsync command and see the intelligent behavior of the rsync command.
2. Rsync directory from local to the remote system:
The operation of syncing a local directory to any remote system is known as “push,” as it pushes the directory from your local system to the remote one.
Follow the syntax of push operation for syncing a directory:
In our case, we are going to sync the “testdir1” directory to the remote host “10.0.2.15”.
3. Rsync directory from remote to local system:
As we all know, the opposite of push is “pull.” In the context of syncing, pull operation sync a directory from the remote system to your local system.
$ sudo rsync -v linuxhint@10.0.2.15:/home/linuxhint/testdir1 /testdir2
Conclusion:
Rsync is a tool that can improve the reliability of local directory syncing and the file transfer process over remote systems. You can use the rsync command to create complex backups and smooth control over what and how a directory will sync. In this post, we have shown you the various forms of using rsync, which include: rsync directory within a local system, rsync directory from local to a remote system, and also from remote to the local one.