What is the rsync command & how to use it

In this article, you will learn about what is the rsync command & how to use it.

The rsync is a command-line utility in Linux and Unix-like operating systems. And, it helps to synchronize the files and directories from a source location to the destination location. It is also used to copy files and directories locally and remotely.

rsync is the faster way to sync the files. Because it is based on the rsync algorithm. So, it only syncs the changes done in the files or directories. It means the incremental backup on the files. This is a good tool for data backups and mirroring between two Linux systems.

Features of the rsync command

  • It uses the md5 algorithm.
  • It easily copies and syncs files to or from a remote system.
  • Rsync uses compression and decompression technique. So, It consumes less bandwidth.
  • It can use the rsh, ssh, or direct sockets as transport.
  • It supports anonymous rsync. This is good for mirroring.
  • Rsync update directory trees and filesystems completely.
  • It can resume the failed transfer
  • Rsync compressed the data with -z argument. So, no need to use archive utility.
  • Creates incremental data backups
  • It is faster than scp command. Because rsync uses the remote-update protocol. This allows us to transfer only the updated files. Firstly, it copies the whole content of the file or directory from the source to the destination. But, next time it only copies the changed blocks and bytes to the destination.
  • Rsync can be run as a daemon.
  • does not require super-user privileges
  • support for copying links, devices, owners, groups, and permissions

Installation of the rsync command on CentOS/RHEL

You can use the below command to install rsync on CentOS/RHEL 8.

sudo dnf install rsync

It may ask for your confirmation to install it. So, press to accept it and hit the enter key.

If you are using the CentOS/RHEL 7 system then you can use the below command to install rsync.

sudo yum install rsync

It may ask for your confirmation to install it. So, press to accept it and hit the enter key.

Installation of the rsync command on Debian based 

If you are using a Debian-based system like Ubuntu, or Debian then you can use the below command.

sudo apt install rsync

It may ask for your confirmation to install it. So, press to accept it and hit the enter key.

How to get help for the rsync command

I always suggest reading the manual page and help. This will give you a deep introduction to the command.

To get the manual page type the below command,

man rsync

To get the only command options, you can type the below command.

rsync --help

Syntax of the rsync command

As we know now, how to get some information about the rsync command using the above command with arguments. Now let’s talk about rsync syntax. The syntax of the rsync command is mentioned below,

General: rsync options source destination

Local: rsync [OPTION…] SRC… [DEST]

Access via remote shell:

Pull: rsync [OPTION…] [USER@]HOST:SRC… [DEST]

Push: rsync [OPTION…] SRC… [USER@]HOST:DEST

The most frequently used option with rsync command

Some options are used frequently along with the rsync command. You can find it very useful. I have mentioned it below.

  • -v:  –verbose, It shows the verbose output on the terminal.
  • -a: –archive mode, this option allows copy recursively and preserve almost everything, like permissions, user, and group ownership and timestamp. (It is equivalent to -rlptgoD)
  • -h: –human-readable, output numbers in a more human-readable format.
  • -r: –recursive, will recursively synchronize the entire directory tree and preserve any permission or timestamp.
  • -u: –update, this option helps to copy only if the source files or directories have any updated version from the source file. This forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file.
  •  -p: –perms, This option causes the receiving rsync to set the destination permissions to be the same as the source permissions.
  • -z: –compress, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted — something that is useful over a slow connection.
  • -l synchronizes symbolic links
  • -n will do a dry run before actually synchronizing.
  • -A uses archive mode and also synchronizes ACLs.
  • -X will synchronize the SELinux context as well.

Copy or sync file on locally

Example 1: For instance, let’s copy a file with the name of demorsynfile1 from source location /root to destination location /tmp. So, the command will be as below, and also I have attached the demo picture below.

rsync -vh /root/demorsyncfile1 /tmp
rsync demo 1
File copy from source /root/demorsyncfile1 to destination /tmp using the rsync command.

Copy or sync directory locally

Example 2: Now you can copy or sync a directory from one location to another. Here, I am synchronizing the data from /root/demorsyncdir/ to /tmp/test and the test directory does not exist. So, it will create the test directory and then copy the data.

rsync -azvh /root/demorsyncdir/ /tmp/test
rsync copy data
Copy or sync directory locally

Copy or sync the directory from a local system to a remote system

In this example, we are going to copy a directory with the name of demorsyncdir to the remote server which ip is 192.168.43.10 and the user name is sahil. It will confirm the remote user password if you are not using an ssh auto-login.

Enter the password and it will copy the directory.

rsync -azvh /root/demorsyncdir/ sahil@192.168.43.10:/tmp/test
rsync copy from local to remote
Copying directory from the local system to the remote system.

Copy or sync the directory from a remote system to a local system

In this example, we are going to copy a directory from a remote host to a local system.

rsync -ahvz sahil@192.168.43.10:/home/sahil/demorsyncremotedir /tmp
remote to local copy using rsync
Copying directory from a remote server to a local server

rsync using ssh

It is a good idea to transfer, copy, or sync the file or directories using the ssh. you can complete this task using an ssh passwordless login or using a password.

If you are using rsync along with ssh then the information will be encrypted using ssh encryption and the data will be more secure while copying or synchronizing.

Copy a File from a local  server to a remote server using ssh

As we are going to use ssh. So, ssh is a protocol, and to specify a protocol along with rsync we have to use the option -e.

If you are using an ssh passwordless login then it will not ask for a password otherwise it will confirm the remote user login password.

rsync -azvhe ssh /root/demorsyncdir/ root@192.168.43.10:/tmp
rsync over ssh
Copy a File from a local  server to a remote server using ssh

In the above example, I’m using passwordless ssh so it did not ask for a password.

Copy a File from a remote server to a local server using ssh

Now, let’s copy a file from a remote server to a local computer. If you are not using passwordless then it will confirm the password while copying the file or directory. You can see in the below picture, it is confirming the password.

rsync -ahvze ssh sahil@192.168.43.10:/home/sahil/demorsyncremotedir/ /tmp
rsync-ssh-remote-local
Copy a File from a remote server to a local server using ssh

Getting progress while copying the data using rsync

There is an option to get progress when you are copying or synchronizing data. You have to use –progress argument along with the rsync command. See the example below.

rsync -azvhe ssh --progress /root/demorsyncdir/ root@192.168.43.10:/tmp
rsync progress bar
Getting progress while copying the data using rsync

Using delete option

In some cases, you do not have the file or directory at the source location. But, it exists to the destination location then you can use the –delete option to delete that if you do not want that file or directory at the destination location.

For example, I have deleted a file with the name demorsyncfile4 from the source location, and after that, I used the –delete argument along with the rsync command to synchronize the directory to the destination location, and as you can see in the picture, it has deleted from the destination directory as well.

rsync -azvhe ssh --progress --delete /root/demorsyncdir/ root@192.168.43.10:/tmp
rsync-delete
Using –delete option along with rsync command

Setting the bandwidth limit while transferring the file

If you want to limit the bandwidth while you are copying the files then you can use the –bwlimit to limit it.

rsync --bwlimit=200 -azvhe ssh --progress --delete /root/demorsyncdir/ root@192.168.43.1
0:/tmp
rsync bandwidth limit
Setting the bandwidth limit while transferring the file

Conclusion

In this tutorial, we learn about the resync command in Linux. It is an important command for data backup. So practice it. I hope, you understand but if you have any questions, you can ask in the comment section.

You can read about What is the use of /etc/shadow file?

You can also read more from the rsync home page

One comment

Leave a Reply

Your email address will not be published.

  1. It is the best command for backup. Thanks for sharing essential options.