Understanding Remote sync(rsync) in RHEL

Introduction:

Rsync(Remote Synchronization) program is used to transfer and sync the files between the servers.rsync is very fast and secure way to transfer and sync the files.You can also use the Rsync command to copy and synchronize your data remotely and locally across the directories, disks, networks, to perform backup and mirroring between two Linux servers.
It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied.

Advantages of rsync:
1.It efficiently syncs and copies files to/from the servers.
2.It is much faster than the SCP(secure protocol) to transfer the files between the servers.
3.rsync it consumes very less bandwidth as it uses compression and decompression method to transfer the files from/to the servers.
Points to  remember:
1.When you are using the rsync command to copy/sync the data between the server for the first time ,it copies the whole content from the source file and pastes it in the destination file.
2.Next time when you run the rsync command it copies/sync only the updated bytes from the source file and paste it in the destination file.(coz of this rsync consumes less bandwidth while transferring data)
3.rsync uses delta-transfer algorithm, which maximum reduces the amount of data sent over the network by sending only the differences between source files and existing files in the destination.
Note1: This utility(rsync) is mostly used for backup and mirroring and as an improved copy command for everyday use.
Note 2: rsync finds the files that need to be transferred by using the quick-check algorithm that looks for files that have changed in size or in last modified time.Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file’s data doesn’t need to be updated.
Syntax to run the rsync command:
#rsync   <options>   <source >      <destination >
Options available with rsync command:
-v = Verbose output
-r = Copies the data recursively(it will not preserve the timestamp and permission while transferring data)
-a = Archive mode(In this mode it allows copying the data recursively and it also preserve symbolic links, file permission, timestamp, user and group ownership)
-z = Compress file data
-h = Human readable format(display the size in MB,GB,kb format)
Before you start using the rsync utility you need to check whether rsync package is already installed on your server as follows,
#rpm  -qa rsync

The above output says the rsync package is installed already, so no need to install the package again.(Do this check on both the servers)
EX:1 To Copy/Sync Files and Directory Locally
In this example, we will sync a file on a local machine from one path to another path, Here we have a file  /mywhatsapp.txt needs to be copied/synced  to /var/mybackup  file

Syntax:

#rsync   <option>  <source>  <destination>
#rsync    -zvh   /mywhatsapp.txt   /var/mybackup

 

In the above example, the destination file mybackup is not already existed,rsync will create the destination file if it does not exist already.
Let us check whether the contents of the /mywhatsapp.txt is properly synced to the /var/mybackup file

The above screenshot has confirmed Everything is successfully synced to the destination without any data loss.
Note: As I said already in the beginning when you are running rsync for the first time it copies the entire content from the source file and paste it in the destination by default,After appending some more bytes to the source file,,again run the same command this time it transfers only the  updated bytes from the source to destination.
Now let us add some more bytes to the source file (in our case /whatsapp.txt is the source file)
#cat >>/whatsapp.txt

This is my second line

Control + d to save

Now run the  rsync command again,
#rsync -zvh   /whatsapp.txt    /var/mybackup

As you can see from the above screenshot rsync this time it transfers only the updated bytes to the destination.
Ex:2 Copy/Sync files/Directory from Local server to Remote Server
In this example, we will see how to sync data from local server to remote server.I have a directory on my local server with the name “Softwares” which contains rpm packages and you want that local server directory contents to be sync to a remote server.

Syntax:

#rsync <option> <Local serverfile path>  user@ipaddrofremoteserver:<remote server destinationpath>
Note: Remember to the sync files within your local system we use different syntax and for remote sync, we use the above syntax.

Server Details:

On server1: /Softwares (Source directory)

IP address:10.250.1.16
On server2: /tmp/mydatabase (destination file)

IP address:10.250.1.15
From 10.250.1.16 run the following command to sync:
Before I sync the data’s from /Softwares directory Let me check the total size of the directory by using the following command
#du -sh /Softwares

So the total data to be sync to the remote path is 3.3GB
#rsync  -avzh  /Softwares root@10.250.1.15:/tmp/mydatabase

As you can see from the above screenshot after authentication the files from the Softwares directory started syncing to the /tmp/mydatabase file.
Ex:3 Copy/Sync a Remote Directory to a Local server
In this example, we will see how to copy/sync a remote directory to local directory.Here we have a directory under /var/log/student which is on a remote server is being copied to your Local server under /tmp/serverdata

Syntax:

#rsync  <option> user@remoteserverip:<Remote server path>   <Local server path>
Note: The syntax will change always the way we use the rsync command.
On server1(10.250.1.16) run the following command:
#rsync   -avzh   root@10.250.1.15:/var/log/student   /tmp/serverdata

Now go to the local server path /tmp/serverdata and check whether the remote server directory “student” synced without any error.
On server 10.250.1.16 run the below command to confirm:
#cd /tmp/serverdata

#ls

As you can see from the above output the directory successfully synced to the local server path /tmp/serverdata.
Tips: The rsync utility by default it uses the  SSH port number 22  to sync the files over the local server to remote server.
Never miss any article from this blog ,Kindly do like my FB page   My FB official Linux Page

If you found this article useful, Kindly Subscribe here 👉👉🏿Click this link to Subscribe

*********************************************************************************************************************************