How To Keep Data Sync’d Between Two Load Balanced Servers

by lifeLinux on November 25, 2010

Load balancing helps provide redundancy to your website. However, one often asked question is how to keep your content synchronized on each server. If you put a new web page on one server, how does it get copied over to the second server? In this article, i’ll explain how to use rsync command to synchronize data between a specific directory on each server.

Install rsync

On Centos Linux type the following command:

yum install rsync

Using rsync command with password authentication

For example, i have two servers using for load balanced and called server1( and server2( I wanted to keep data in /var/www/html sync between to servers. Type the following command on server1:

rsync -avr --progress --links --rsh='/usr/bin/ssh' /var/www/html

-avr : archive mode (a), verbose (v), recurse into directories (r).
–progress: Show progress.
–links: copy symlinks as symlinks.
–rsh: use to specify the remote shell ssh to use. IP address of server2 and path to synchronize to server1
/var/www/html: Server1 path

Using rsync command with SSH certificate

Replace above command with following command:

rsync -avr --progress --links --rsh='/usr/bin/ssh -i /path_to_private_key' /var/www/html

A sample shell script

rsync -avr --progress --links --rsh="'$SSH -i $PRIVATEKEY'" $SOURCEHOST:$SOURCEPATH $DESTPATH 2>&1 >> $LOGFILE

Related Posts:

Leave a Comment

Previous post:

Next post: