A significantly modernized Python script for installing and updating Plex Media Server on Debian-based systems. This script automates the process of checking for the latest Plex versions, downloading them, verifying their integrity, and installing them, with support for different architectures and operational modes like dry-run and download-only.
- Automated Update Check: Fetches the latest version information from the Plex API.
- Architecture Support: Handles updates for different architectures (e.g.,
amd64,armhf). - Checksum Verification: Verifies the SHA1 checksum of downloaded files before installation to ensure integrity.
- Secure Installation: Uses
subprocessfor command execution and prompts forsudoconfirmation if needed. - Flexible Execution Modes:
--dry-run: See what would happen without making any changes.--download-only: Download and verify the package but skip installation.
- Configuration Management: Stores details of the last update in an
info.inifile to avoid reprocessing the same version. - Logging: Provides informative output with different log levels (INFO, DEBUG) and a
--verboseoption. - Error Handling: Robust error handling for network issues, file operations, and unexpected conditions.
The script is run from the command line as follows:
python3 plex-update.py <architecture> [options]<architecture>: (Required) The target architecture for the Plex update.- Choices:
amd64,armhf
- Choices:
-h, --help: Show the help message and exit.--version: Show the script's version number and exit.--dry-run: Check for updates and report what actions would be taken, but do not download or install anything. The configuration file will not be modified.--download-only: Download the update package if available and verify its checksum, but do not install it. The configuration file will be updated if the download is successful.-v, --verbose: Enable verbose logging, providing DEBUG level output.
- Python Version: This script requires Python 3.7 or higher. (Python 3.6 might work, but 3.7+ is recommended due to features like
Path.unlink(missing_ok=True)being available, although the script currently uses explicit checks for wider compatibility). - External Libraries: The primary external dependency is the
requestslibrary, used for making HTTP requests. - Installation: Dependencies are listed in
requirements.txtand can be installed using pip:Thepip install -r requirements.txt
requirements.txtfile contains:requests
This script is designed for Debian-based Linux systems (e.g., Debian, Ubuntu, Raspberry Pi OS).
- gdebi: The
gdebi-corepackage is required for installing the Plex.debfile and automatically handling its dependencies. You can install it using:sudo apt update sudo apt install gdebi-core
- Sudo Privileges: The script uses
gdebifor installation, which typically requires superuser privileges. If the script is not run as root, it will prompt you to confirm whether it should proceed by prependingsudoto thegdebicommand.
The script uses a configuration file named info.ini located in the same directory as plex-update.py. This file is used to store the checksum, download URL, and version of the last successfully processed Plex Media Server package for each architecture.
The script will create or update this file automatically.
Structure:
The info.ini file follows a simple INI format:
[amd64]
checksum = <sha1_checksum_of_last_downloaded_deb_for_amd64>
url = <url_of_last_downloaded_deb_for_amd64>
version = <version_string_of_last_downloaded_deb_for_amd64>
[armhf]
checksum = <sha1_checksum_of_last_downloaded_deb_for_armhf>
url = <url_of_last_downloaded_deb_for_armhf>
version = <version_string_of_last_downloaded_deb_for_armhf>- Each section (e.g.,
[amd64]) corresponds to an architecture. checksum: The SHA1 checksum of the last downloaded/verified.debfile.url: The URL from which the last.debfile was downloaded.version: The version string of that Plex Media Server release.
The [DEFAULT] section might have existed in older versions for the Plex JSON URL, but this URL is now a constant within the script. The script primarily manages the architecture-specific sections.
Ensure you have installed Python 3.7+ and the required dependencies (see "Python Dependencies" section). The script must be run from its directory or by providing the correct path to plex-update.py.
To check for an update and install it for a specific architecture:
python3 plex-update.py amd64or
python3 plex-update.py armhf-
Dry Run (see what would happen):
python3 plex-update.py amd64 --dry-run
-
Download Only (download but don't install):
python3 plex-update.py armhf --download-only
-
Verbose Output (for debugging):
python3 plex-update.py amd64 -v
or
python3 plex-update.py amd64 --verbose
You can automate the update check by scheduling the script to run periodically using cron.
-
Open your crontab for editing:
crontab -e
-
Add a line to schedule the script. For example, to run it daily at 3:00 AM for the
amd64architecture:0 3 * * * /usr/bin/python3 /path/to/your/plex-update.py amd64 > /tmp/plex-update.log 2>&1
Important:
- Replace
/usr/bin/python3with the actual path to your Python 3 interpreter if it's different (usewhich python3). - Replace
/path/to/your/plex-update.pywith the absolute path to theplex-update.pyscript. - It's recommended to redirect output to a log file (
> /tmp/plex-update.log 2>&1) to capture any messages or errors. - Consider using the
--verboseflag if you want detailed logs, or ensure your script's default logging level is appropriate for cron execution.
- Replace