Syncing Files Between Web Servers
Posted on 21st August 2008 by SameerAfter learning how to load balance, you still need to keep your web files consistent between your web servers. My tool of choice for doing so is rsync which includes smart features such as delta uploads (if it notices a file has changed it will only upload the difference, not the whole file from scratch).
I am assuming that you will have a particular “main” web server which you always update with new content first. The new content can either be “pushed” by the main web server to dependent web servers running rsync daemons, or it can be “pulled” by dependent web servers from the main web server. I suggest running a “pull” environment because your main web server will not need any knowledge of the existence of the dependent web servers.
On your main web server you will need to start the rsync daemon. Save the following file (taken from FredShack) in /etc/init.d/rsyncd
#!/bin/sh
# Rsyncd This shell script takes care of starting and stopping the rsync daemon
# description: Rsync is an awesome replication tool.
# Source function library.
. /etc/rc.d/init.d/functions
[ -f /usr/bin/rsync ] || exit 0
case "$1" in
start)
action "Starting rsyncd: " /usr/bin/rsync --daemon
;;
stop)
action "Stopping rsyncd: " killall rsync
;;
*)
echo "Usage: rsyncd {start|stop}"
exit 1
esac
exit 0
Set the daemon to run on boot by typing “chkconfig rsyncd on”. You will then need to set up a basic rsyncd configuration file in /etc/rsyncd.conf.
#/etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
[mysite]
path = /path/to/webroot
auth users = rsyncuser
secrets file = /etc/rsyncd.secrets
hosts allow = 192.168.*.*
Only users on your private network (192.168.*.*) will be allowed to connect and only if they provide the password for the user rsyncuser whose password is stored in plain text in /etc/rsyncd.secrets. Because the password is in plain text, rsyncd will not let any user connect to the server if rsyncd.secrets is world readable.
#/etc/rsyncd.secrets rsyncuser:mypassword
Start the rsyncd daemon:
service rsyncd start
Now you want to move over to the dependent web servers and make sure they can connect to the rsync daemon to get the latest files.
First, you will need to create a password file. Note: this file only contains the password, not the username, and again the password is stored in clear text so the file must not be world readable.
#/etc/rsync/passwordfile mypassword
And then run:
# --delete is used to get an exact mirror # rsyncuser is the user needed to login # mysite is the module name defined in /etc/rsyncd.conf # mainwebserver is the hostname of the main web server rsync -av --delete --password-file=/etc/rsync/passwordfile rsyncuser@mainwebserver::mysite /path/to/webroot
To have the command run every minute put the above into /etc/crontab.
[...] Now that you know how to load balance, you will need to learn how to sync your files between multiple web servers. [...]
[...] that I have covered how to load balance multiple web servers and how to keep their content synchronized there is one more major problem to solve: sessions. You need sessions to identify a particular user [...]
[...] Sameer Parwani » Syncing Files Between Web Servers [...]
You can use inotify tool. It notifies you when something was changed and executes your script. No need to run you script every minute. You can look for csync2 it is a nice tool combined with inotify tool you are unstoppable ![]()

