How to use the Rsync command in Linux.

Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behaviour and permit very flexible specifications of the set of files to be copied. Rsync supports a delta transfer algorithm which reduces the amount of data sent over the network by sending only the difference between the source and existing files in the destination.

The basic syntax of rsync command is

#rsync options source destination

let’s look at the common flags used in Rsync commands.
-a The archive mode behaves like the recursive mode but keeps all file permissions, symbolic links, file ownership, etc.

-r Allows to sync data recursively inside a directory.
-z Used to compress data during transfers to save space.
b Performs a backup during data synchronization.
-h Shows the numbers in the output in a human-readable format.
-e Instructs the rsync to use the SSH protocol for remote transfers.
v Verbose output. Displays the details of the transfer.

please note that if you are trying to transfer a file or folder to a specified location using rsync it will check if the desired location is there or else rsync will create a file/folder at your destination in order to copy the files.

In this blog, I will show you to use rsync in 3 different ways.

1: Rsync command to copy files/folders within our system

2: Rsync command used to copy files/folders from one server to another

3: Rsync command used to copy files/folders from one server to another server using SSH.

1: Rsync command to copy files/folders within our system.

1(a): Rsync command used to copy files from one location to another within the local machine.

rsync -v test.txt /home/rsync/

This command will copy the test.txt file in the CWD to the directory /home/rsync.

1(b): Rsync command used to copy a directory from one location to another.

rsync -avzh /home/linuxlearninghub/Download /home/linuxlearninghub/Documents

Here we are providing the A flag in order to archive the folder during transfer Z flag is used to compress the directory during transfer and V is used for verbose display of output and the H flag is used to display it in a human-readable format.

We can directly use our cp command in order to do this task but the benefit of rsync is that we can speed up the transfer and it also provides the output in human-readable format

2: Rsync command used to copy files/folders from one server to another.

Syntax

rsync -avzhe ssh –progress /path/to/dir root@:/path/to/paste/dir

2(a): Rsync command used to copy files and directories from our local server to a remote server.

rsync -avzh /home/test.txt [email protected]:/home

This command will copy test.txt file to a remote server’s /home location having ip address 107.23.196.25. This command will prompt to enter the password of the remote server.

2(b): Rsync command used to copy files/folders from the remote server to our local server.

syntax

rsync -avzh [email protected]:/home/backupfolder /home/todaysbackup

This command will copy /home/backupfolder from the remote server having ip address to the location /home/todaysbackup in our system.

3: Rsync command used to copy files/folders from one server to another server using SSH.

We can use rsync over ssh protocol in order to securely transfer our files/folders to the remote server using the -e flag

3(a): Rsync over ssh to transfer files from our server to the remote server.

rsync -avzhe ssh /home/test.txt [email protected]:/backup

This command will copy the file test.txt present in /home directory of local server to the remote server’s /backup location having IP address 107.23.196.25

3(b): Rsync over ssh to transfer files from a remote server to our local server.

rsync -avzhe ssh [email protected]:/home/sample.txt /home/files

This command will copy the test.txt file from the remote server having IP 107.23.196.25 address to our local system’s /home/files directory.

We can also track the progress of each transfer. Ie it will let you know the amount to data transfer and data left to transfer you can use the –progress flag with rsync command in order to track the progress

For example, if you want to track the progress of this transfer.

rsync -avzhe ssh –progress /home/test.txt [email protected]:/backup

use the –progress flag along with the command.

4, Rsync command used to transfer files from one server to remote server using the custom ssh port and ssh key’s

Rsync command used to transfer using the ssh key.
Here the key.pem is the private key of the server with IP address 172.168.10 so it will match along with the respected public key in the server.The backup.tar.gz file present in the /home directory of the local server is transferred to the /home/backup folder of the remote server.

rsync -avzhe “ssh -i key.pem” –progress /home/backup.tar.gz [email protected]:/home/backup

Rsync command used to transfer files using custom ssh port

rsync -avzhe ‘ssh -p 5522’ –progress /home/folder.tar.gz [email protected]:/home/

Here 5522 is the custom ssh port of the server with IP address 94.150.78.214 .
Here we are transferring folder.tar.gz from the local server to remote server’s home directory

Summary:
In this blog, we have learned all the basic use cases of the Rsync command in Linux
Similar blogs

How to use scp command in Linux?

Leave a Reply

Your email address will not be published. Required fields are marked *