How to Set Up File Mirroring Between Two Cloud Servers Using the rsync Tool?

16-01-2024 02:21:25

If certain files on a server are of paramount importance, setting up a mirror on another server is an excellent method for backing up data. In the event of hardware damage or network failure on the primary server, we can promptly use the mirrored files for data recovery, ensuring the applications are up and running again without delay.

Installing rsync

Execute the following command on a Linux cloud server to install rsync.

CentOS

yum install rsync

Ubuntu/Debian

apt-get install rsync

Using rsync

For instance, to synchronize data from Cloud Server A to Cloud Server B, remotely access Cloud Server B and execute the following command:

rsync -avrt --delete --rsh='ssh -p 22' root@<ipa>:<source> <destination>

Here, 'ipa' is the IP address of Cloud Server A, 'source' is the source directory on Cloud Server A, and 'destination' is the target directory on Cloud Server B. If the remote port is not the default 22, modify it to the corresponding port number.

Setting up a cronjob

By setting up a cron job, you can automate the execution of the above rsync command, thereby achieving file mirroring.

Edit the cronjob file:

vi /etc/crontab

Add the following command to sync every 3 minutes:

*/3 * * * * rsync -avrt --delete --rsh='ssh -p 22' root@<ipa>:<source> <destination>

This concludes the method for setting up file mirroring between two cloud servers using rsync.