Setup File Mirroring Using Rsync in Debian/Ubuntu

Setup File Mirroring Using Rsync in Debian/Ubuntu

If you run a critical website, it is a good practice to mirror your files to a secondary server. In the event that your main server has a network or hardware issue, having mirrored files provides you with the flexibility to immediately switch from one server to another.

To sync your files from server A (main) to server B (backup), follow these steps.

Install rsync

Install rsync on both server A and server B.

sudo apt-get install rsync

Generate an SSH key on server B

Run the following command on server B to generate an SSH key.

ssh-keygen

Press enter to skip all inputs.
Copy the content generated in /root/.ssh/id_rsa.pub

Insert the SSH public key to server A

Go to server A, insert the copied content into /root/.ssh/authorized_keys.
If this directory or file does not exist, create it. Now server B can log into server A using SSH.

Start sync

Go back to server B and start rsync for the first time. Review and run the command below. You will need to add the path to your website on both servers and the IP address of server A.

rsync -avrt --delete --rsh='ssh -p 22' [email protected]_A_IP_ADDRESS:/path/to/your/website/in/server/a/ /backup/path/in/server/b/

Type yes if you been asked to save the authentication information. If you have configured ssh to listen on a custom port, change the 22 to your own port. Wait until all files has been synced over.

Setup cronjob

Setup a cronjob to sync your files automatically. This example syncs them every 3 minutes. Edit /etc/crontab

Review the following line, and append it in the crontab file. You will need to update it as you did previously.

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

Subscribe

Stay up to date! Get all the latest & greatest posts delivered straight to your inbox

You've successfully subscribed to Salehin Khan
Great! Next, complete checkout for full access to Salehin Khan
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.