Linux Commands

How to rsync to multiple destinations

Rsync is a very popular command used in Linux for syncing files or directories either locally or remotely. The reason behind its popularity is that it only takes the changes and copies them to the destination. Mostly, this command is used in keeping the data backup and restoration. This post will learn a bit about the rsync command, how it works, its syntax, and how to sync to multiple destinations using rsync.

Syntax

The syntax of writing and executing the rsync command is really simple.

$ rsync -options SOURCE_PATH DESTINATION_PATH

In the above-given syntax, we first have to provide the options to the rsync command and later provide the source from where you want to transfer the data or files and destination path.

The most common options used in the rsync command are as follows:

-a option is for archive files and folders during synchronization,
-r is for synchronizing the files in recursive mode,
-v is for verbose output,
-b is for keeping the backup during the synchronization,
-z is for compressing and transfer the files,
-progress is for showing progress.

Installation of rsync command

Usually, the rsync command is already available in all Linux-based operating systems, but it does not come pre-installed in the minimal installation of an Operating system.

To install rsync in Ubuntu or any Debian based Operating system, use the command given below:

$ sudo apt install rsync -y

To install rsync in CentOS or RHEL based Operating system, use the command given below:

$ sudo dnf install rsync -y

Rsync Command syntax for remote transfer

The syntax of the rsync command for remote transfer is a bit different. We have to provide the username and the IP address of the remote host where we want to transfer files using the syntax provided below:

Local to Remote Transfer

For transferring files from the local system to a remote system, you need to use the syntax given below:

$ rsync -options SOURCE_PATH @:DESTINATION_PATH

Remote to Local

For transferring files from any remote host to the local system, you need to use the syntax given below:

$ rsync -options @:SOURCE_PATH DESTINATION_PATH

Now, you have all the basic and required knowledge of the rsync command; let’s understand how to rsync to multiple destinations.

How to rsync to multiple destinations

The answer to either rsync provides any method or options to transfer files to multiple destinations, or not is short NO. There is no method available to provide multiple destinations to the rsync command. Rsync works only from point to point.

However, using some manual techniques, we can use any loop and invoke the rsync command multiple times.

Rsync to multiple destinations using for loop

For loop is a great choice to provide multiple destination paths and run the rsync command for transferring the data to multiple destinations instead of executing the rsync command manually again and again.

The syntax for writing the for loop and providing multiple destinations is as follows

$ for d in DESTINATION_PATH_1 DESTINATION_PATH_2
> do rsync -options SOURCE_PATH $d
> done

Using this technique, you can provide multiple destinations and transfer the data to multiple destinations using the rsync command.

Conclusion

Rsync is a command used for transferring files or directories from one place to another efficiently by just transferring the changes instead of the whole data again and again while back up or mirroring operations. This post provides the basic syntax, its usage, and installation method in any Linux-based Operating system. This post provides an interesting and useful technique for transferring the data to multiple destinations using the loop and rsync command.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.