Script to sync files between web severs having plesk

by jagbir on March 10, 2009

I got a little assignment where there are two webservers are there 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. Though this script can be used in any other case, just you may need to update it little bit.

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' only 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</tmp/dir.list
let count=0
 
## check domains in other server and update them.
while read LINE <&10; do
    ## update second webserver if domain exist there (just in case)
    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, It fetch all directory names under /var/www/vhosts excluding ‘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 every minute update is more than sufficient for most of web servers but still if you need more speedy updates, you can detect changes in directories and initiate sync as and when required (Read more).

Other possible ways to do this thing is to use an NFS mount in second web server which also facilitate instant (real-time) sync but this will create a single point of failure where if server having NFS directory/mount have some issues, other server will not be able to access it. So for smaller setup, this rsync stuff works like a charm.

Other most read similar article on this site:
* How to ensure secure login in production Linux servers.
* Top 5 Linux Commands for Admins.
* Small script to backup you log files.
* Recover or reset password in MySQL and PostgreSQL.
* Quick sync or update time in server.

  • http://slavi.biz/ Slavi

    Hi,

    I don’t think it’s a good idea to update the content so often unless the 2nd machine is a backup.

    Slavi

    • http://www.jagbir.info jagbir

      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.

  • Abhishek

    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.

  • http://www.jagbir.info jagbir

    @Abhishek
    this will create a single point of failure as if the nfs host develop issues, every other will suffer. So, rsync seems good.

  • http://www.secretsfw.com/business/management/ Charolette Vories

    Good website.

  • http://articlemarketingtoolcase.com/ Avery Weissbrodt

    One thing countless article marketers disregard is to keep their articles concise and sweet. Even the most insightful articles or blog posts can cause alluring waves of sleepiness at times.

  • Pingback: lockscripts free premium website scripts

Previous post:

Next post: