Skip to content

Instantly share code, notes, and snippets.

@jbfriedrich
Last active November 4, 2018 19:07
Show Gist options
  • Save jbfriedrich/e99773ca2a4236d0225665e5792a8dbc to your computer and use it in GitHub Desktop.
Save jbfriedrich/e99773ca2a4236d0225665e5792a8dbc to your computer and use it in GitHub Desktop.
Check if a new Plex version is available, install it and then move the deb package into a directory for archival purposes.
#!/bin/bash
# Some global variables
WGET_BIN='/usr/bin/wget'
JQ_BIN='/usr/bin/jq'
SED_BIN='/bin/sed'
TOOLS_MISSING=0
CHECK_UPDATE_URL='https://plex.tv/api/downloads/1.json'
UPDATE_URL='https://plex.tv/downloads/latest/1?channel=16&build=linux-ubuntu-x86_64&distro=ubuntu'
ARCHIVE_DIR="${HOME}/plex_archive"
# Check if all required tools are installed
for tool in ${WGET_BIN} ${JQ_BIN} ${SED_BIN}; do
if [[ ! -f "${tool}" ]]; then
echo "Please install ${tool} via 'apt-get install ${tool##*/}'"
TOOLS_MISSING=1
fi
done
# Exit if tools are missing
if [[ ${TOOLS_MISSING} -eq 1 ]]; then
exit 1
fi
# Check if archive dir exists - if not, create it
[[ ! -d ${ARCHIVE_DIR} ]] && (mkdir ${ARCHIVE_DIR} && echo "Created archive directory ${ARCHIVE_DIR}") || echo "Archive directory exists"
# Check if newer version is available
echo "Checking for update..."
LATEST="$(${WGET_BIN} -q -O - ${CHECK_UPDATE_URL} | ${JQ_BIN} .computer.Linux.version | ${SED_BIN} 's/"//g')"
CURRENT="$(dpkg -s plexmediaserver | grep Version | ${SED_BIN} 's/Version: //g')"
echo "Installed version: ${CURRENT}"
echo "Latest version: ${LATEST}"
# Install if local version is different
if [[ "${CURRENT}" != "${LATEST}" ]]; then
echo "Downloading and installing new version..."
${WGET_BIN} -q --show-progress --trust-server-names ${UPDATE_URL} &&
dpkg -i *.deb &&
mv -v *.deb ${ARCHIVE_DIR} &&
systemctl restart plexmediaserver
else
echo "Lastest version already installed"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment