Mattermost Server - Upgrade Script

Upgrading a Mattermost Server is not complicated, but requires some steps that can be mostly automated. I'm watching 'Releases only' on the GitHub repository to get a notification when a new version is available and start the script manually.

Below script assumes, that Mattermost is installed at the default location /opt/mattermost.
The script first greps the current download link from the official Mattermost Download page, displays the found link and asks if it should continue. Rest should be self-explanatory (see echo messages).

#!/bin/bash
# Mattermost documentation: https://docs.mattermost.com/administration/upgrade.html
# Mattermost download: https://mattermost.com/download/

# Variables
downloadLink=$(curl -v --silent https://mattermost.com/download/ 2>&1 | grep 'https://releases.mattermost.com/.*/mattermost-[[:digit:]].*-linux-amd64.tar.gz' | sed 's/.* value=\"\(.*\)\".*/\1/'  | sed 's/\?src\=dl//')
echo 'Download link is:' $downloadLink
fileName=${downloadLink##*/}
echo 'File name is:' $fileName

# Continue?
read -p 'Do you want to continue (y/n)?' decision

# Start
if [ "$decision" = "y" ]; then
    echo 'Moving to /tmp'
    cd /tmp

    echo 'Downloading latest version'
    wget $downloadLink

    echo 'Extracting files'
    tar -xf mattermost*.gz --transform='s,^[^/]\+,\0-upgrade,'

    echo 'Stopping mattermost service'
    sudo systemctl stop mattermost

    echo 'Backing up current files'
    cd /opt
    cp -ra mattermost/ mattermost-back-$(date +'%F-%H-%M')/

    echo 'Removing all files except special directories from within the current mattermost directory'
    find mattermost/ mattermost/client/ -mindepth 1 -maxdepth 1 \! \( -type d \( -path mattermost/client -o -path mattermost/client/plugins -o -path mattermost/config -o -path mattermost/logs -o -path mattermost/plugins -o -path mattermost/data \) -prune \) | sort | sudo xargs rm -r

    echo 'Removing the plugins directories so they do not interfere with the upgrade'
    mv mattermost/plugins/ mattermost/plugins~
    mv mattermost/client/plugins/ mattermost/client/plugins~

    echo 'Changing ownership of the new files before copying them'
    chown -hR mattermost:mattermost /tmp/mattermost-upgrade/

    echo 'Copying the new files to your install directory and remove the temporary files'
    cp -an /tmp/mattermost-upgrade/. mattermost/
    rm -r /tmp/mattermost-upgrade/

    echo 'Starting mattermost service'
    sudo systemctl start mattermost

    echo 'Upgrade your config.json schema and continue then! (Open the System Console and change a setting, then revert it. This should enable the Save button for that page. Click Save. Refresh the page.)'
    read -p 'Continue?'

    echo 'Reinstating the plugins directories, then restart the mattermost service'
    cd /opt/mattermost
    rsync -au plugins~/ plugins
    rm -rf plugins~
    rsync -au client/plugins~/ client/plugins
    rm -rf client/plugins~
    sudo systemctl restart mattermost

    echo 'Removing downloaded file'
    rm /tmp/$fileName

    echo 'Finished.'
else
     echo 'Exiting.'
fi
Show Comments