23 Feb 2009

BASH Script to Update WordPress via Subversion (SVN)

Yet another BASH script that will automatically update your WordPress installation via Subversion (SVN). Run this script manually or using crontab (preferred). Really suitable if you run multiple WordPress installation in your hosting account.

Modify this script according to your WordPress installation and server preferences.

#!/bin/bash

WP_VERSION="2.7"
SVN_REPO="http://svn.automattic.com/wordpress/branches/$WP_VERSION"
ROOT_DIR=/home/user/public_html

REPO_REV=`svn info $SVN_REPO | grep -r "Last Changed Rev:" | cut -f 4 -d " "`
LOCAL_REV=`svn info $ROOT_DIR/example.com | grep -r "Last Changed Rev:" | cut -f 4 -d " "`

REPO_DB_VER=`wget -q -O - $SVN_REPO/wp-includes/version.php | grep "wp_db_version =" | cut -f 3 -d " " | sed "s/;//"`
LOCAL_DB_VER=`grep "wp_db_version =" $ROOT_DIR/example.com/wp-includes/version.php | cut -f 3 -d " " | sed "s/;//"`

BLOG_DIR=(
	"$ROOT_DIR/example.com"
	"$ROOT_DIR/example1.com"
	"$ROOT_DIR/example2.com"
)

BLOG_URL=(
	"http://example.com"
	"http://example1.com"
	"http://example2.com"
)

if [ $REPO_REV -gt $LOCAL_REV ]; then
	for ((i=0;i<${#BLOG_DIR[@]};i++)); do
		svn update ${BLOG_DIR[${i}]}
		if [ $REPO_DB_VER -gt $LOCAL_DB_VER ]; then
			wget -q -O - ${BLOG_URL[${i}]}/wp-admin/upgrade.php?step=1 > /dev/null
		fi
	done
	exit 0
fi