Post

Automating Emby Server Updates with Bash

Automating Emby Server Updates with Bash

This script is designed to automate the process of checking for updates to the Emby Server software on a Linux system and updating it if a new version is available. The script compares the currently installed version of Emby with the latest version available on the official GitHub releases page, and if an update is available, it downloads and installs the new version.

Script Breakdown

Below is a detailed breakdown of the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash

# Variables
GITHUB_RELEASES_URL="https://github.com/MediaBrowser/Emby.Releases/releases/latest"
TEMP_DIR="/tmp/emby_update"
CURRENT_VERSION=$(dpkg -s emby-server | grep Version | awk '{print $2}')

# Create a temporary directory for the download
mkdir -p $TEMP_DIR

# Fetch the latest release URL
LATEST_RELEASE_URL=$(curl -sI $GITHUB_RELEASES_URL | grep -i location | awk '{print $2}' | tr -d '\r\n')

# Extract the version number from the URL
LATEST_VERSION=$(basename $LATEST_RELEASE_URL | sed 's/[^0-9.]*\([0-9.]*\).*/\1/')

# Compare versions
if [ "$LATEST_VERSION" != "$CURRENT_VERSION" ]; then
  echo "New version available: $LATEST_VERSION (current: $CURRENT_VERSION)"

  # Construct the download URL
  DOWNLOAD_URL="https://github.com/MediaBrowser/Emby.Releases/releases/download/${LATEST_VERSION}/emby-server-deb_${LATEST_VERSION}_amd64.deb"

  # Download the latest version
  wget -O $TEMP_DIR/emby-server.deb $DOWNLOAD_URL

  # Install the new version
  sudo dpkg -i $TEMP_DIR/emby-server.deb

  # Clean up
  rm -rf $TEMP_DIR

  echo "Emby has been updated to version $LATEST_VERSION."
else
  echo "Emby is already up to date (version: $CURRENT_VERSION)."
fi

How the Script Works

Variables:

  • GITHUB_RELEASES_URL stores the URL to the latest Emby Server release.
  • TEMP_DIR is the temporary directory where the script will download the update.
  • CURRENT_VERSION fetches the currently installed version of Emby Server using dpkg.

Create a Temporary Directory:

  • The script creates a temporary directory using mkdir -p to store the downloaded update.

Fetch Latest Release URL:

  • The script uses curl to get the latest release URL from GitHub. The grep and awk commands filter the header response to extract the location header, which contains the redirect URL to the latest release.

Extract Latest Version:

  • The script then extracts the version number from the release URL using basename and sed.

Compare Versions:

  • The script compares the latest version with the currently installed version. If they differ, it means an update is available.

Download and Install the Update:

  • If an update is available, the script constructs the download URL, uses wget to download the .deb package, and installs it using dpkg.

Clean Up:

  • After installation, the script removes the temporary directory and its contents to clean up.

Output Messages:

  • The script provides feedback, letting the user know whether Emby was updated or if it was already up to date.

How to Use the Script

Preparation:

  • Ensure you have curl, wget, and dpkg installed on your system.
  • You need to have sudo privileges to run the script since it involves installing software.

Running the Script:

  • Copy the script into a .sh file, for example, update_emby.sh.
  • Make the script executable
    1
    
    chmod +x update_emby.sh
    
  • Run the script:
    1
    
    ./update_emby.sh
    
  • The script will check for updates and, if available, automatically download and install the latest Emby Server version.

Automating the Script:

  • You can automate this script by adding it to a cron job:
    1
    
    crontab -e
    
  • Add the following line to run the script daily at 2 AM:
    1
    
    0 2 * * * /path/to/update_emby.sh
    

This script simplifies the process of keeping your Emby Server up to date, ensuring you always have the latest features and security patches with minimal effort.

This post is licensed under CC BY 4.0 by the author.