A Poor Mans Staging/Live with WordPress and Rsync

Firstly, this isn't meant to be a serious contender for 'proper' version control, Eg. Subversion or Git, or anything of the kind.

But instead just a nice little way of managing a WordPress staging and live site, so that you can perform simple design tweaks/changes on a staging site and roll out those changes gracefully to the live site. We use it for tiny WordPress sites where version history is not required but we still want to separate CSS tweaks from the live site.

Start by creating a new file sync.sh and paste the following into it, changing the highlighted lines as necessary, then chmod +x sync.sh and execute it.

Support for

  • W3 Total Cache - providing an option of flushing all caches (if necessary).
  • WP Minify - automatically turning off CSS and JS minification on the staging site and flushing cache on live site

The code

!/bin/sh

LIVE_DBNAME="" LIVE_DBUSER="" LIVE_DBPASS="" LIVE_DOMAIN="www.mysite.com" LIVE_PATH="/home/mysite/public_html"

STAG_DBNAME="" STAG_DBUSER="" STAG_DBPASS="" STAG_DOMAIN="staging.mysite.com" STAG_PATH="/home/mysite/subdomains/staging"

THEME="twentyeleven" DBHOST="localhost"

##########################################################

No need to make changes after this line ...

##########################################################

echo "Which way do you want to sync ..." echo " 1. From LIVE to STAGING" echo " 2. From STAGING to LIVE" echo -n "[1/2]? "

From LIVE to STAGING

read OPTION if [[ "$OPTION" == "1" ]]; then echo -n "Sync themes [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then rsync --delete -par $LIVE_PATH/wp-content/themes/$THEME/ $STAG_PATH/wp-content/themes/$THEME/ fi

echo -n "Sync uploads [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then rsync --delete -par $LIVE_PATH/wp-content/uploads/ $STAG_PATH/wp-content/uploads/ fi

echo -n "Sync plugins [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then rsync --delete -par $LIVE_PATH/wp-content/plugins/ $STAG_PATH/wp-content/plugins/ fi

echo -n "Sync database [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then mysqldump -h$DBHOST -u$LIVE_DBUSER $LIVE_DBNAME -p$LIVE_DBPASS | sed "s/$LIVE_DOMAIN/$STAG_DOMAIN/g" | sed 's/"enable_css";b:1;s:9:"enable_js";b:1/"enable_css";b:0;s:9:"enable_js";b:0/g' | mysql -h$DBHOST -u$STAG_DBUSER $STAG_DBNAME -p$STAG_DBPASS fi fi

From STAGING to LIVE

if [[ "$OPTION" == "2" ]]; then echo -n "Sync themes [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then rsync --delete -par $STAG_PATH/wp-content/themes/$THEME/ $LIVE_PATH/wp-content/themes/$THEME/ fi

echo -n "Sync uploads [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then rsync --delete -par $STAG_PATH/wp-content/uploads/ $LIVE_PATH/wp-content/uploads/ fi

echo -n "Sync plugins [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then rsync --delete -par $STAG_PATH/wp-content/plugins/ $LIVE_PATH/wp-content/plugins/ fi

echo -n "Sync database [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then mysqldump -h$DBHOST -u$STAG_DBUSER $STAG_DBNAME -p$STAG_DBPASS | sed "s/$STAG_DOMAIN/$LIVE_DOMAIN/g" | sed 's/"enable_css";b:0;s:9:"enable_js";b:0/"enable_css";b:1;s:9:"enable_js";b:1/g' | mysql -h$DBHOST -u$LIVE_DBUSER $LIVE_DBNAME -p$LIVE_DBPASS fi

echo -n "Flush LIVE cache [y/N]? " read CONFIRM if [[ "$CONFIRM" == "y" ]]; then rm -f $LIVE_PATH/wp-content/plugins/wp-minify/cache/ rm -rf $LIVE_PATH/wp-content/w3tc/dbcache/ rm -rf $LIVE_PATH/wp-content/w3tc/pgcache/ rm -rf $LIVE_PATH/wp-content/w3tc/objectcache/ fi fi

echo "Completed sync"

Then chmod +x sync.sh and execute it.

[syntaxhighlighter]