
This question has been answered by gissirob. See the first response.


#!/bin/bash
# MODX Update Script
# -------------------
# This shell script automate the upgrade of a MODX Revolution installation
# on command line (CLI) on a webserver.
# Is not bullet proof and has to be understood and adapted to each user.
# I wrote it because upgrading 60 Revolution website is long and boring.
# With it, you can update a Revolution Website in 8 seconds without mouse
#
# BE SURE TO HAVE FONCTIONNAL BACKUP BEFORE USING THIS SCRIPT (database dump AND files)
#
# Thanks to Christian Hanvey for the inspiration https://gist.github.com/christianhanvey/5592012
# --------------
# Config section
# --------------
SOURCE_DIR="/root/utils"
MODX_PACKAGE_MAX_AGE=86400
MODX_PACKAGE_LAST_FILENAME="modx-lastest.zip"
MODX_LAST_DIR="modx-lastest"
# ---------------
# Start of script
# ---------------
# Set to exit on error
set -e
# Define some colours
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[1;33m'
litegreen='\e[1;32m'
purple='\e[0;35m'
NC='\e[0m' # No Color
# -------------------
# FUNCTION : Cleaning
# -------------------
# 1/ Remove all packages, sources directory and symlink of MODX in the $SOURCE_DIR
function clean_modx {
echo -e "${litegreen}-------------------- ${NC}"
echo -e "${litegreen}Cleaning old package ${NC}"
echo -e "${litegreen}-------------------- ${NC}"
# Delete old package, directories and symlink
rm -fr $SOURCE_DIR/{modx-2*,$MODX_LAST_DIR}
rm -f $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME
echo -e "-> old MODX packages and directories removed from $SOURCE_DIR/ ${green}[OK]${NC}"
echo ""
}
# -------------------
# FUNCTION : Get MODX
# -------------------
# 1/ Check if package is older than a day before downloading it again
# 2/ Remove older source package and directories
# 3/ Download last MODX version and extract it to $SOURCE_DIR
# 4/ Make a symlink from $SOURCE_DIR/modx-2.3.1-pl to $SOURCE_DIR/modx-lastest
function get_modx {
echo -e "${litegreen}------------${NC}"
echo -e "${litegreen}Getting MODX${NC}"
echo -e "${litegreen}------------${NC}"
# Downloading only if not exist or it's to older
if [ -f $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME ]
then
MODX_PACKAGE_LAST_DOWNLOADED_DATE=`stat -c %X $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME`
CURRENT_DATE=`date +%s`
MODX_PACKAGE_CURRENT_AGE=$(( ($CURRENT_DATE - $MODX_PACKAGE_LAST_DOWNLOADED_DATE) ))
else
MODX_PACKAGE_CURRENT_AGE=86400000
fi
if [ $MODX_PACKAGE_CURRENT_AGE -gt $MODX_PACKAGE_MAX_AGE ]
then
# Function call to cleaning old package
clean_modx
# Getting last MODX package
wget -O $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME http://modx.com/download/latest
echo -e "-> download MODX package ${green}[OK]${NC}"
# Unzip the package on the Source Directory
unzip -o -q $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME -d $SOURCE_DIR
echo -e "-> extract MODX package inside $SOURCE_DIR/ ${green}[OK]${NC}"
# Finding the name of the lastest created directory during the extraction
LAST_CREATED_DIR=$(find $SOURCE_DIR/modx-2* -type d -prune -exec ls -d {} \; |tail -1)
# Symbolic link is created to set an unvariable path to the last version of MODX
ln -s $LAST_CREATED_DIR $SOURCE_DIR/$MODX_LAST_DIR
echo -e "-> symbolic link to $SOURCE_DIR/$MODX_LAST_DIR ${green}[OK]${NC}"
echo ""
else
echo -e "${green}[INFO]${NC} The MODX package is very fresh (less than 24h). Not necessary to download it again."
echo ""
fi
}
# -------------------
# FUNCTION : Put MODX
# -------------------
# 1/ Check if we are in the right place (the root of one Revo Website). The test is made for my server
# 2/ If yes, copy new MODX files to the current directory (the project which has to be upgraded)
# 3/ Setting correct owner to the files inside the directory project to upgrade
function put_modx {
echo -e "${litegreen}----------------------------------${NC}"
echo -e "${litegreen}Copy new MODX files on the project${NC}"
echo -e "${litegreen}----------------------------------${NC}"
# Directory verification : we're checking the owner of the directory where we are
# It's specidic to my server... so adapt to yours
# If we are not on a correct directory project, we stop
PROJECT_DIR_OWNER=$(ls -ld .|awk '{ print $3 }')
if [ "$(echo $PROJECT_DIR_OWNER | grep -E "vu([0-9]{4})")" ]
then
echo ""
echo "Copying the last files of MODX in ${PWD}"
cp -Rf /root/utils/modx-lastest/* ./
echo -e " -> done ${green}[OK]${NC}"
echo ""
echo "Setting correct owner to the files inside ${PWD}"
chown -R $PROJECT_DIR_OWNER.www-data ./
echo -e " -> done ${green}[OK]${NC}"
echo ""
else
echo "CANCELATION : you are not in a correct project directory !!"
exit
fi
}
# -----------------------
# FUNCTION : Upgrade MODX
# -----------------------
# 1/ The fabulous CLI to upgrade in 7 seconds. I'm using an optionnal config-modx.xml file just to set the language
function up_modx {
echo -e "${litegreen}------------------------ ${NC}"
echo -e "${litegreen}Running the MODX upgrade ${NC}"
echo -e "${litegreen}------------------------ ${NC}"
echo "Upgrading in progress..."
/usr/bin/php ./setup/index.php --installmode=upgrade --config=/root/bin/config-modx.xml
}
# ============
# Introduction
# ============
echo ''
echo -e "${litegreen}* * * * * * * * * * ${NC}"
echo -e "${litegreen}MODX UPGRADE SCRIPT ${NC}"
echo -e "${litegreen}* * * * * * * * * * ${NC}"
echo ''
echo -e "What do you want to do ?"
echo -e "------------------------"
echo -e "- get : getting the last MODX Revolution package and unpack it on $SOURCE_DIR/$MODX_LAST_DIR"
echo -e "- put : copy $SOURCE_DIR/$MODX_LAST_DIR to ${yellow}${PWD}${NC}"
echo -e "- up : run upgrade command in ${yellow}${PWD}${NC}"
echo -e "- put-up : copy AND run upgrade command in ${yellow}${PWD}${NC}"
echo -e "- clean : remove every MODX sources in $SOURCE_DIR/"
echo ''
# ===========
# The choices
# ===========
echo -e "You are in ${PWD}"
PS3='Please enter your choice : '
options=("get" "put" "up" "put-up" "clean" "Quit")
select opt in "${options[@]}"
do
case $opt in
"get")
get_modx;;
"put")
put_modx;;
"up")
up_modx
exit
;;
"put-up")
put_modx
up_modx
exit
;;
"clean")
clean_modx
exit
;;
"Quit")
break
;;
*) echo -e "${red}[ERROR]${NC} Invalid option";;
esac
done

<modx>
<!-- Set this to 1 if you are using MODX from Git or extracted it from the full MODX package to the server prior
to installation. -->
<inplace>0</inplace>
<!-- Set this to 1 if you have manually extracted the core package from the file core/packages/core.transport.zip.
This will reduce the time it takes for the installation process on systems that do not allow the PHP time_limit
and Apache script execution time settings to be altered. -->
<unpacked>0</unpacked>
<!-- The language to upgrade MODX for. This will set the default manager language to this. Use IANA codes. -->
<language>fr</language>
<!-- Paths for your MODX core directory -->
<!--<core_path>/www/modx/core/</core_path> -->
<!-- Whether or not to remove the setup/ directory after installation. -->
<remove_setup_directory>1</remove_setup_directory>
</modx>
- automatically back up the filesystem and the database before you startIn my case, I don't really need to include the backup automation in this script because I have another system on my server (automysqlbackup and rsnapshot)
)- backing up config.inc.phpIn my experience, I never had to do this.
- removing the setup directory once you've finishedIt's already active in my script, with using the idoine config.xml option
- making config.inc.php writable during the upgrade and then read-only again afterAs said before, I don't touch to the original config.inc.php during the upgrade.

- capturing all output (including errors) to a log in case something goes wrong.Yes, you're right, I will put it on my todo-list

