DEV Community

Adam.S
Adam.S

Posted on • Edited on • Originally published at bas-man.dev

1

Back up your Plex Media Server on Mac OS

The Challenge:

How to automate the backing up of my Plex Media Server

Solution:

A Bash Script

I wanted to automate this process. If I didn't, it probably wouldn't get done.

This script was adapted from another GitHub Gist that I found here.

#!/usr/bin/env bash
# Backup a Plex database.
# Author Scott Smereka
# Ubuntu
# Version 1.0
# Modified by Shaun Harrison
# Ubuntu
# Version 1.1
# Modified by Adam Spann
# Version 1.0 for Mac OS
# Script Tested on:
# Mac OS X El Capitan 10.11.6 on 17/1/2019 [ OK ]
# Note: If you server sleep timer is not set to zero, the server will potentially sleep
# at some point during backup.
# To avoid this run the shell script using caffeinate command
# caffeinate -i "path/to/script"
#This will ensure the script runs uninterupted and does not require using pmset command
# Plex Database Location. The trailing slash is
# needed and important for rsync.
plexDatabase="$HOME/Library/Application Support/Plex Media Server/"
# Plex plist file
plexPlistFile="$HOME/Library/Preferences/com.plexapp.plexmediaserver.plist"
# Base backup location
backupDirectory="/Volumes/Drobo/PlexBackups"
# Location to backup the directory to.
plexBackupDirectory="$backupDirectory/plexmediaserver/database/"
#Location to backup plist file
plexPlistBackupDirectory="$backupDirectory/plexmediaserver/plist/"
# Location to backup logs to.
logsDirectory="$backupDirectory/logs"
# Log file for script's output named with
# the script's name, date, and time of execution.
scriptName=$(basename ${0})
scriptName="${scriptName%.*}"
log="$logsDirectory/${scriptName}_`date +%Y-%m-%d-%H%M%S`.log"
# Create Log
echo -e "Staring Backup of Plex Database." 2>&1 | tee $log
echo -e "------------------------------------------------------------\n" 2>&1 | tee $log
# Stop Plex
echo -e "\n\nStopping Plex Media Server." 2>&1 | tee $log
echo -e "------------------------------------------------------------\n" 2>&1 | tee $log
/usr/bin/osascript -e 'tell app "Plex Media Server" to quit' 2>&1 | tee $log
# Ensure directories exist
mkdir -p $plexBackupDirectory $logsDirectory
# Backup database
echo -e "\n\nStarting Backup." 2>&1 | tee $log
echo -e "------------------------------------------------------------\n" 2>&1 | tee $log
rsync -av --delete "$plexDatabase" "$plexBackupDirectory" 2>&1 | tee $log
rsync -av --delete "$plexPlistFile" "$plexBackupDirectory" 2>&1 | tee $log
# Restart Plex
echo -e "\n\nStarting Plex Media Server." 2>&1 | tee $log
echo -e "------------------------------------------------------------\n" 2>&1 | tee $log
/usr/bin/osascript -e 'tell app "Plex Media Server" to launch' 2>&1 | tee $log
# Done
echo -e "\n\nBackup Complete." 2>&1 | tee $log

You will probably need to modify the following variables.

  • plexDatabase
  • plexPlistFile
  • backupDirectory

Pay attention to lines 17 ~ 21. These refer to the possibility that your script may not
complete if your machine can go to sleep. As it suggests use the 'caffeinate' command to prevent
your machine from sleeping during backup. It will be able to sleep after the script completes.

Don't forget to give the script execution permission using 'chmod +x scriptname'

Install a cronjob under your user account using:

  crontab -e
Enter fullscreen mode Exit fullscreen mode

The contents of my cronjob

SHELL=/bin/bash
06 02 * * Sun /usr/bin/caffeinate $HOME/bin/plexbackup.sh
Enter fullscreen mode Exit fullscreen mode

Notes:

  • You need a SHELL set for the cronjob to run the command.
  • The script runs weekly on Sunday from 6:02am
  • It uses caffeinate so that the machine does not sleep during backup. I do not have my sleep value set to 0 on this machine.
  • My backup script is located in my home directory under the folder bin

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up