Script to auto update Plex media server version

Hi,

I was annoyed a while back that I had to update my plex media server manually. I don’t mind that it’s upgraded immediately when it’s out so why do it manually. So I wrote a script to do this for me. See bottom.

What the script does: it compares the current installed version with the latest version. If the latest version is newer, it will download and install it. It will also create a log for it in the ~/Downloads/PlexMediaServerUpdate/ folder. Add the script to crontab to let it execute once a day (or any other frequency)

So the script:
In my home (cd ~), I have the script plexmediaserverupdate.sh.
Replace YOURNAME so it corresponds with your home dir
and replace PLEXTOKENHERE with your plex token ( Finding an authentication token / X-Plex-Token | Plex Support):

#!/bin/bash

MissingDownloadsDir=false
PlexMediaServerUpdateDir=false

#Check environment
if [ ! -d "/home/YOURNAME/Downloads" ]
then
  mkdir "/home/YOURNAME/Downloads"
  MissingDownloadsDir=true
fi
if [ ! -d "/home/YOURNAME/Downloads/PlexMediaServerUpdate" ]
then
  mkdir "/home/YOURNAME/Downloads/PlexMediaServerUpdate"
  PlexMediaServerUpdateDir=true
fi

cd "/home/YOURNAME/Downloads/PlexMediaServerUpdate"

if $MissingDownloadsDir
then
  echo "$(date) - Downloads directory was missing so it was created." >> "$(pwd)/PlexMediaServerUpdate.log"
fi
if $PlexMediaServerUpdateDir
then
  echo "$(date) - PlexMediaServerUpdate directory in Downloads was missing so it was created." >> "$(pwd)/PlexMediaServerUpdate.log"
fi

#Get downloadlocation
DownloadLocation=$(curl -I "https://plex.tv/downloads/latest/5?channel=8&build=linux-x86_64&distro=debian&X-Plex-Token=PLEXTOKENHERE" --stderr - | grep '^Location:' | cut -c 11- | tr -d '\r')

#Get versions
CurrentVersion=$(dpkg -s plexmediaserver | grep '^Version:' | cut -c 10- | sed -r 's/([0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,})-.+?/\1/g')
DownloadVersion=$(echo ${DownloadLocation} | sed -r 's/http.{1,}([0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,})-.+?/\1/g')

#Compare versions
if $(dpkg --compare-versions ${CurrentVersion} lt ${DownloadVersion})
then
  echo "Current version (${CurrentVersion}) is older. New version ${DownloadVersion} will be installed."
  Upgrade=true
else
  echo "Current version (${CurrentVersion}) is up to date."
  Upgrade=false
fi

#Upgrade version if newer
if $Upgrade
then
  wget -O "${DownloadVersion}.deb" "${DownloadLocation}"
  sudo dpkg -i "${DownloadVersion}.deb"
  rm "${DownloadVersion}.deb"
  echo "$(date) - Plex Media Server upgraded to version ${DownloadVersion}. Previous version was ${CurrentVersion}." >> "/home/YOURNAME/Downloads/PlexMediaServerUpdate/PlexMediaServerUpdate.log"
else
  echo "Plex needed no update."
fi

Schedule as a recurring task
I then added my script to crontab sudo crontab -e by adding the line:

0 14 * * * /home/diederik/plexmediaserverupdate.sh

After a while your log will say cat ./Downloads/PlexMediaServerUpdate/PlexMediaServerUpdate.log:

Mi 09 Dez 2020 14:00:21 CET - Plex Media Server upgraded to version 1.21.1.3759. Previous version was 1.21.0.3711.
Fr 11 Dez 2020 14:00:22 CET - Plex Media Server upgraded to version 1.21.1.3766. Previous version was 1.21.1.3759.
Mi 16 Dez 2020 14:00:22 CET - Plex Media Server upgraded to version 1.21.1.3795. Previous version was 1.21.1.3766.
Fr 18 Dez 2020 14:00:23 CET - Plex Media Server upgraded to version 1.21.1.3830. Previous version was 1.21.1.3795.
Di 05 Jan 2021 14:00:20 CET - Plex Media Server upgraded to version 1.21.1.3842. Previous version was 1.21.1.3830.
Mi 06 Jan 2021 14:00:20 CET - Plex Media Server upgraded to version 1.21.1.3876. Previous version was 1.21.1.3842.

Let me know if you have improvements on how to do this better!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.