Syncing Two Drupal Sites

The drush module for Drupal has a new “sync” option to synchronize two Drupal sites, but it is undocumented and looking at the source code all it does is sync the files, not the mysql database. I haven’t seen any other public solutions that do completely sync 2 drupal sites including the databases.

Here’s a little shell script I’m using to synchronize the files and database of 2 drupal sites on different servers. We have a staging area where we test out upgrades or new code and features first. When not testing out stuff, we also use that 2nd server as a fallback in case the first server goes down. So this script copies everything over from the main drupal site to the 2nd staging area server using rsync, and then imports the mysql database on the 2nd server as well.

This script does depend on using the backup and migrate module for drupal, too, to dump the mysql database at regularly scheduled intervals to drupal’s files folder. The script fetches the newest sql dump and imports it into mysql on the staging area.

Also this script prompts for the ssh password to remotely connect. To run without any prompting, you’d need to generate an ssh key file (see this tutorial).

Edit the sections that start with ##EDIT:

##syncdrupal.sh

##EDIT:
STAGESERVERNAME="name.of.staging.server"

##run this script on staging area server only:
if [ `uname -n` != $STAGESERVERNAME ];
then
  echo "only run this on staging area server"
  exit 0
fi

##EDIT:
##src and dest can be remote or local (see rsync tutorials)
SRC="username@mainsite.edu:/path/to/mainsite/drupal/folder/*"
DEST="/path/to/stagingarea/drupal/folder/"

echo "Going to sync files from $SRC"
echo "to $DEST"

##I exclude cache because of a permission error with print/tcpdf/cache
rsync -e ssh -azv --delete --exclude="cache" $SRC $DEST

##EDIT:
SQLPATH="/path/to/stagingarea/sites/default/files/backup_migrate/scheduled"

##find most recent scheduled backup .sql.gz file:
MOSTRECENTSQL=`ls -1t $SQLPATH | awk 'NR==1{print $1}'`

gunzip "$SQLPATH/$MOSTRECENTSQL"

##get sql filename minus the ".gz" part
MOSTRECENTSQL=`ls -1t $SQLPATH | awk 'NR==1{print $1}'`

##EDIT:
##db name, user, pass for staging area mysql server:
DBNAME="db"
DBUSER="user"
DBPASS="pass"

echo "Importing $SQLPATH/$MOSTRECENTSQL"

##import the sql file into mysql:
mysql -u $DBUSER -p$DBPASS $DBNAME < "$SQLPATH/$MOSTRECENTSQL"

We also add a little code like this to our theme’s page.tpl.php file to flash a red box at the top of the staging area website so we’ll remember which site we are working on: (change “stage” to servername of your staging area, and change 5 to the number of characters in that name)

<?php if (strncmp($_SERVER['SERVER_NAME'],"stage",5) == 0) { ?>
<div id="warn" style="background-color:red;text-align:center;">
<h1>You are viewing a backup version of the WHATEVER website (servername).
Please do not save any information here.</h1>
</div>
<?php } ?>

Faculty developer. Interests: developing educational technology; faculty & student development; learning sciences & psychology.

Posted in drupal
One comment on “Syncing Two Drupal Sites
  1. perandre says:

    Wow! Gonna try this later. Thanks!

Comments are closed.

Doug Holton

Doug Holton

Faculty developer. Interests: developing educational technology; faculty & student development; learning sciences & psychology.

View Full Profile →

Join 5,342 other subscribers
Follow EdTechDev – Doug Holton on WordPress.com