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 on both server A and server B.
apt-get install rsync
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
.
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.
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' root@SERVER_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 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' root@SERVER_A_IP_ADDRESS:/path/to/your/website/in/server/a/ /backup/path/in/server/b/ >/dev/null 2>&1