Skip to content

Instantly share code, notes, and snippets.

@christianhanvey
Last active November 1, 2017 16:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christianhanvey/5592012 to your computer and use it in GitHub Desktop.
Save christianhanvey/5592012 to your computer and use it in GitHub Desktop.
A shell script to help automate the upgrade of a MODX Revolution installation
#!/bin/bash
# modx-upgrade.sh
# ---------------
# A shell script to help automate the upgrade of a MODX Revolution installation
# This script is for traditional installations only - not advanced
#
# Instructions:
# 1. Update the config section with your own site specific details
# 2. Upload this file to your server, preferably outside of web root
# 3. give this file execute permissions, and run from the command line
# 4. run /setup from your browser to finish upgrade
# 5. there is no step 5!
# 6. no step 6 either, huh?
#
# If you want to know what is happening:
# - a copy of the site files is made
# - the new modx is downloaded and then moved into this folder after
# preserving components, packages and configs from your existing install
# - those components, packages, and configs are then synced from the old site
# - finally, we swap old for new
# - also make a date-stamped archive of the old site, including a database backup
#
# REMEMBER: always verify your backups
# don't leave checking they actually work until the point where you need to use them
# that is the worst time to find out your backups don't work
# a backup works if you can fully restore your site from it
# --------------
# config section
# --------------
# MUST SET:
#
# absolute path to MODX
PATH_TO_MODX='/absolute/path/to/modx'
#
# db config
DB_NAME='your_db_name'
DB_USER='your_db_user'
DB_PASS='your_db_pass'
DB_HOST='your_db_host'
# OPTIONALLY SET:
#
# writable folder permissions
WRITE_DIR_MODE=0755
#
# writable file permissions
WRITE_FILE_MODE=0644
#
# name of temporary folder for modx download
# will be created alongside modx directory (should not already exist)
MODX_DL='modx_download'
#
# date format - used in back-up archive filename
D=$(date +%Y-%m-%d)
# ---------------
# start of script
# ---------------
# set to exit on error
set -e
# define some colours
red='\e[0;31m'
green='\e[0;32m'
litegreen='\e[1;32m'
purple='\e[0;35m'
NC='\e[0m' # No Color
echo ''
echo -e "${green}MODX Upgrade script${NC}"
echo -e "${litegreen}* * * * * * * * * * ${NC}"
echo ''
echo -e "${green}[INFO]${NC} Preparing to upgrade MODX installation at: $PATH_TO_MODX "
echo ''
# lets go
cd $PATH_TO_MODX
# get the name of directory where the install is
MODX_DIR_NAME=${PWD##*/}
cd ..
# remove any old upgrade folders
rm -rf _$MODX_DIR_NAME
echo -e "${green}[INFO]${NC} Copying directory ${MODX_DIR_NAME} to _${MODX_DIR_NAME}"
echo ''
# copy current site to temporary duplicate directory
cp -Rp $MODX_DIR_NAME _$MODX_DIR_NAME
# prepare new directory - make it easier to merge new modx
cd _$MODX_DIR_NAME
mv connectors _connectors
mv core _core
mv manager _manager
mv index.php _index.php
if [ -d setup ]
then
mv setup _setup
fi
cd ..
# grab the latest and greatest
rm -rf $MODX_DL
mkdir $MODX_DL
cd $MODX_DL
wget -O latest.zip http://modx.com/download/latest
unzip -q latest.zip
rm latest.zip
cd ..
# we don't know the name of the containing directory we just unzipped
# but we know its the only item in our temporary download folder
cd $MODX_DL/*
mv connectors ../../_$MODX_DIR_NAME/
mv core ../../_$MODX_DIR_NAME/
mv manager ../../_$MODX_DIR_NAME/
mv setup ../../_$MODX_DIR_NAME/
mv index.php ../../_$MODX_DIR_NAME
# copy components over
cd ../../_$MODX_DIR_NAME
cp -Rfp _core/components core
# move packages over make sure not to bring old core files
rm -rf _core/packages/core
rm -rf _core/packages/core.transport.zip
cp -Rfp _core/packages/* core/packages/
# bring our config files over
cp -f $PATH_TO_MODX/config.core.php .
cp -f $PATH_TO_MODX/connectors/config.core.php connectors
cp -f $PATH_TO_MODX/manager/config.core.php manager
cp -f $PATH_TO_MODX/core/config/config.inc.php core/config
# make sure permissions of required folders are correct
chmod $WRITE_DIR_MODE core/cache core/export core/components core/packages assets assets/components
chmod $WRITE_FILE_MODE core/config/config.inc.php
# backup setup to make it easy to re-run
rm -rf setup.tar
tar -czf setup.tar.gz setup
# swap old site with new site
echo ''
echo -e "${green}[INFO]${NC} Ok, let's hot-swap to upgraded version..."
echo -e ''
cd $PATH_TO_MODX
cd ..
#remove any existing backup folder first
rm -rf $MODX_DIR_NAME-backup
mv $MODX_DIR_NAME $MODX_DIR_NAME-backup && mv _$MODX_DIR_NAME $MODX_DIR_NAME
# clean up
rm -rf $MODX_DIR_NAME/_core && rm -rf $MODX_DIR_NAME/_connectors && rm -rf $MODX_DIR_NAME/_manager && rm -f _index.php
# remove download folder
rm -rf $MODX_DL
# backup the database
mysqldump -u $DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME | gzip -9 > $MODX_DIR_NAME"-backup/"$DB_NAME"-"$D".sql.gz"
# create archive of old version
tar -czf $MODX_DIR_NAME"_"$D".tar.gz" $MODX_DIR_NAME-backup
rm -rf $MODX_DIR_NAME-backup
echo -e "${green}[INFO]${NC} All done${NC}"
echo ''
echo -e "${red}[WARNING]${NC} You must now finish the upgrade by running /setup from your browser"
echo ''
echo -e "Have a nice day! ${green}:)${NC}"
echo ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment