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(192.168.1.101) and server2(192.168.1.102). 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' 192.168.1.102:/var/www/html /var/www/html

With:
-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.
192.168.1.102:/var/www/html: 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' 192.168.1.102:/var/www/html /var/www/html

A sample shell script

#!/bin/bash
SOURCEPATH='source'
DESTPATH='destination'
SOURCEHOST='ip_address'
PRIVATEKEY='private_key'
LOGFILE='log_file'
SSH='/usr/bin/ssh'
rsync -avr --progress --links --rsh="'$SSH -i $PRIVATEKEY'" $SOURCEHOST:$SOURCEPATH $DESTPATH 2>&1 >> $LOGFILE

Related Posts:

Leave a Comment

Previous post:

Next post: