Upgrading Ghost

Upgrading Ghost

If you have a self hosted installation of Ghost upgrading it can be a little bit daunting and possibly dangerous. The instructions here are pretty good. Although sometimes all you want is one command to do the lot for you so you don't have to remember where those instructions are each time.

So here's my dodgy bash script that will upgrade your Ghost installation automagically for you. Be warned there's little in the way of error checking, so always have a backup.

#!/bin/bash
# Make sure only root can run our script
if [[ ${EUID} -ne 0 ]]; then
  echo "This script must be run as root" 1>&2
  exit 1
fi

if [[ $# -eq 0 ]]; then
  echo "Usage:"
  echo "    $0 <systemd|sysv>"
  echo -e "\n systemd : use systemd/systemctl to stop/start services"
  echo " sysv    : use /etc/init.d syvs init system to stop/start services"
fi
 
GHOST_PARENT=/var/www
OLD_PWD=$(pwd)

case "$1" in
   systemd)
     INIT_START="sudo systemctl start ghost.service"
     INIT_STOP ="sudo systemctl stop ghost.service"
     ;;
   sysv)
     INIT_START="sudo service ghost start"
     INIT_STOP ="sudo service ghost stop"
     ;;
sac
     
cd ${GHOST_PARENT}

# Get current Ghost version
OLD_VERSION=$(node -pe "JSON.parse(require('fs').readFileSync('/dev/stdin').toString()).version" < ghost/package.json)

echo "Upgrading Ghost"
echo -n "Fetching ... "
curl -LOk https://ghost.org/zip/ghost-latest.zip > upgrade.log 2>&1 || exit
echo "Unzipping ..."
unzip ghost-latest.zip -d ghost-latest >> upgrade.log 2>&1 || exit

# Get new Ghost version
NEW_VERSION=$(node -pe "JSON.parse(require('fs').readFileSync('/dev/stdin').toString()).version" < ghost-latest/package.json)

echo "Upgrading from ${OLD_VERSION} to ${NEW_VERSION}.  Please wait ..."

# Stop Ghost
${INIT_STOP}

# Backup the current version core files
mv -v ghost/core ghost-${OLD_VERSION}-core || exit

# Update with new files
cp -v ghost-latest/{*.js,*.json,*.md,LICENSE} ghost/ || exit
cp -R -v ghost-latest/core ghost/ || exit
cp -R -v ghost-latest/content/themes/casper ghost/content/themes || exit
cd ghost

# Update nodejs modules as required
npm install --production

# Fix permissions
chown -R ghost:www-data ./*

# Restart Ghost
${INIT_START}

cd ${OLD_PWD}

And for those using a systemd based init system here's a .service file for systemd

/etc/systemd/system/ghost.service

[Unit]
Description=Ghost service
# Make sure we have functional network and logging available
After=syslog.target
After=network.target

[Service]
User=ghost
Group=www-data
ExecStart=/usr/bin/node /path/to/ghost/index.js
Environment=NODE_ENV=production
StandardOutput=null
TimeoutSec=10

[Install]
WantedBy=multi-user.target