• How to Sync Files Between Multiple Web Servers - Ubuntu

Synchronizing multiple servers You have more than one web server in your host configuration. What’s the easiest way to synch files among the multiple web servers? Solution – rsync does the trick efficiently and easily. Here are the steps required to set up rsync for multiple servers synchronization: Step One First make sure rsync is installed on each server that you’ll be working with. By typing rsync from a command prompt, you should get the rsync response asking you for parameters. If rsync isn’t found, then install it.

sudo apt-get install rsync

Step Two

Test synchronizing files. From a command prompt on your 2nd server, type in an rsync command to retrieve files from the 1st server. Let’s assume you have a directory /var/www/ set up on both servers and you wish to synchronize the files from the first server to the second. The first server acts as the master. Any new files or changes to existing files on the master will overwrite existing files on the second server. Type in from the command prompt on server 2

sudo rsync -ra user@FirstServerIP:/var/www/ /var/www/

The first server in this example sends all the files in /var/www/ along with the (-r) recursive sub directories. You can also sync only specific files, etc… If this works, you are ready to move on to step 3. If not, check your connectivity with the first server. Generally, if you can ssh to another server, you should be able to rsync with it as well. If you are using servers which require a passkey, the rsync command will look like this:

rsync -rave “ssh -i /home/ubuntu/.ssh/passkey.pem” ubuntu@EC2ServerIP:/var/www/ /var/www/

Step Three

Next, let’s set this up to synchronize all the time automatically. We accomplish this with a cron job. Here again, we’re assuming you are familiar with how to set up and edit crons. From your root user (sudo su) edit your crontab

crontab -e

Then, add a line to the crontab that tells it to run the rsync command every minute (or any frequency that suits you).

* * * * * rsync -ar FirstServerIP:/var/www/* /var/www/

Test your configuration! Go to your first server and make a change or add a file to the synchronized directory. Then go back to the second server and see if the file is there. You can replicate this process to as many servers as you need, but keep in mind that frequency of sync and amount of data passing on your internal network can get costly and cause a hit on performance.


This article was last modified: Oct. 17, 2017, 10:37 a.m.

0 Comments

Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.

Share

Hacker News

Top