Script to sync files between web severs having plesk
I got a little assignment where there are two webservers behind a load balancer and both needs to have identical files. Both server has plesk control panel. As we know, plesk store website files in /var/www/vhosts/domain/httpdocs directory, here domain is the name of website.
I wrote a wrapper script for rsync and deployed there. Here is the script:
#!/bin/bash
############### websync.sh by Jagbir Singh #################
#
# This script acts as wrapper for rsync. It checks every domain listed in /var/www/vshots
# for existence in other server, if found there, then sync domain’s ‘httpdocs’ directory.
#
# ver 1.0 Mar 8, 2009: Initial version.
#
#############################################################cd /var/www/vhosts
webserver2=”192.168.30.2″## get directory list
ls –file-type | grep “\/” | sed ‘s/\///’ | grep -Ev “^chroot|^default” > /tmp/dir.list
exec 10 let count=0## check domains in other server and update them.
while read LINE <&10; do
## update second webserver at planet
if `ssh $webserver2 "ls /var/www/vhosts/$LINE/httpdocs/ > /dev/null 2>&1″` ; then
`rsync -az –delete -e ssh /var/www/vhosts/$LINE/httpdocs/ $webserver2:/var/www/vhosts/$LINE/httpdocs/`
fi((count++))
done
exec 10>&-
This is a very simple script and may not suitable for your requirements. It fetch all directory names under /var/www/vhosts exluding ‘chroot’ and ‘default’ and save them in a temporary file. Then get directory names from that temporary file and execute rsync for every directory after confirmation that it exist in second server.
I’ve made a cron entry to run this script every minute:
* * * * * /bin/bash /root/websync/websync.sh
So, if you update a file in webserver 1, then the file get reflected within a minute to second web server. This is acceptable but if you need more speedy sync then you have to slightly modify the script to detect changes in directory and sync as and when necessary.
Hi,
I don’t think it’s a good idea to update the content so often unless the 2nd machine is a backup.
Slavi
Hi Slavi,
It’s helpful when you web servers are behind a load balancer routing traffic between them. you’ve to ensure that all web servers must have identical contents.
You can use NFS to mount a directory at one web-server on rest of the web-servers behind the load-balancer. This directory will now hold the identical content.
@Abhishek
this will create a single point of failure as if the nfs host develop issues, every other will suffer. So, rsync seems good.