mkay
3/8/2014 - 10:57 PM

MODX CLI updater

MODX CLI updater

#!/bin/bash
########################################################
#
# MODX (modx.com) CLI updater
# Kreuder 20140308 mk@s1.gl
#
########################################################
# 
# Description: perform a MODX Basic Upgrade via CLI.
#
########################################################
# 
# This script will run a MODX Basic Upgrade via CLI. (see http://bit.ly/1fcyisI for more)
# Either from a static local source directory or -if not provided- it will download the latest release from modx.com. 
# 
# Usage: 
# update-modx.sh /path/to/your/modx/install/
# 
# This is an EXPERIMENTAL script. Modify it according to your needs. 
# MAKE A BACKUP of your installation -both web and database- before running. 
# DO NOT RUN without knowing what you are doing. It does 'things'. 
# 
# 
# MODX is a registered trademark of MODX, LLC
# 
########################################################

# do we have a static source? (path to an unzipped MODX download). If empty we will d/l the latest version from modx.com.
SOURCE='' 

# set the default manager language. Use IANA codes. (see http://bit.ly/1fcyisI for more)
CONFIG_LANG='en'

# script arguments
SCRIPTSELF=`basename $0`
TARGET=${1%/}

# do we have a writable target directory?
if [ -z "$TARGET" -a "${TARGET+nada}" = "nada" ]; then echo "We need a target directory. (eg: '${SCRIPTSELF} /var/www/htdocs/') Aborting."; exit 1; fi

if [ -d "$TARGET" -a -w "$TARGET" ]; 
	then

		# no source directory set - we will d/l a copy of MODX
		if [ -z "$SOURCE" ];
			then
				# we need wget for grabbing the latest version of MODX. Make sure we have it.
				command -v wget >/dev/null 2>&1 || { echo >&2 "We need 'wget' for this but it's not installed. Aborting."; exit 1; }
				# we need unzip for extracting the downloaded .zip. Make sure we have it.
				command -v wget >/dev/null 2>&1 || { echo >&2 "We need 'unzip' for this but it's not installed. Aborting."; exit 1; }

				# ensure a correct /path/to/our/prerequisites.
				WGET=$(command -v wget) 
				UNZIP=$(command -v unzip)

				# d/l MODX' latest
				cd $TARGET
				$WGET -O latest.zip http://modx.com/download/latest

				# unzip it to a temporary directory
				TMPDIR=TMP_${RANDOM}
				mkdir $TARGET/${TMPDIR}
				$UNZIP latest.zip -d ./${TMPDIR}

				# cd to the d/l source dir
				cd ./${TMPDIR}/modx*

			else
				# cd to the static source dir
				cd $SOURCE
		fi

		# apply update (means: OVERWRITE the old installation)
		cp -rf ./* $TARGET

		# adjust permissions, in case we ran this as root
		USER=`stat -c '%U' $TARGET`		# get target's user
		GROUP=`stat -c '%G' $TARGET`	# get target's group
		chown -R $USER:$GROUP $TARGET 	# chown recursively

		# prepare config.xml for MODX command line update
		sed -i "s#<language>en</language>#<language>${CONFIG_LANG}</language>#g" ${TARGET}/setup/config.dist.upgrade.xml
		sed -i "s#/www/modx/core/#${TARGET}/core/#g" ${TARGET}/setup/config.dist.upgrade.xml

		# run MODX command line update
		PHP=$(command -v php)
		cd ${TARGET}/setup
		$PHP ./index.php --installmode=upgrade  --config=${TARGET}/setup/config.dist.upgrade.xml

		# clean up
		if [ -z "$SOURCE" ];
			then
				cd $TARGET
				rm -f ./latest.zip	# remove the .zip
				rm -rf $TMPDIR		# remove the tmp dir
		fi

	else
		echo $TARGET "either doesn't exist or is not writable. Aborting.";
fi