Setup File Mirroring Using Rysnc Debian/Ubuntu

If you have been running a critical website on Linux Dedicated Server, then the best practice that one can have is the mirroring of the files to a secondary server. In this case the main server will be having a network or the hardware issue, by having the mirrored files it will provide you the flexibility of switching from one server to another one.

For syncing the files from one server to another one needs to follow the steps as shown below:

Installation of rsync
Here the installation of rsync on both the server A as well as B.
apt-get install rsync

Generation of SSH key on server B

One needs to run the following command on the server B for the generation of an SSH key.
ssh-keygen


Now you need to Press enter for skipping all the inputs in Linux Dedicated Server.

Then just copy the content generated in /root/.ssh/id_rsa.pub.

Inserting the SSH public key to the server A

Here one needs to go to the server A, then you need to insert the copied content into /root/.ssh/authorized_keys.

If this type of directory is not existing you need to create it. Here the server B can be logged into server A by using the SSH.

By Staring the sync

You need to go back to the server B and then start the rsync for the first time. Here the review and then run the command which is below. You will have to add the path of the website on both of these server and the IP address of the server A.

rsync -avrt --delete --rsh='ssh -p 22' root@SERVER_A_IP_ADDRESS:/path/to/your/website/in/server/a/ /backup/path/in/server/b/

You have to type yes if you have been asked for saving the authentication information. If the ssh has been configured then one needs to listen to the custom port, you need to change the 22 to the own port. You need to change the 22 to an own port. You need to wait until all the files have been synced over.

 

Setting up of Cronjob

You need to setup the cronjob for syncing the files automatically. Here it will sync in every 3 minutes.

Edit /etc/crontab.

You need to review the following line and then append it in the crontab file. This has to be updated as it has been done previously.


*/3 * * * * root rsync -avrt --delete --rsh='ssh -p 22' root@SERVER_A_IP_ADDRESS:/path/to/your/website/in/server/a/ /backup/path/in/server/b/ >/dev/null 2>&1

Once following the above steps now you can easily steup File Mirroring in your Ubuntu server