Skip to content

Instantly share code, notes, and snippets.

@Bas-Man
Forked from shnhrrsn/plexDatabaseBackupScript.sh
Last active March 26, 2021 23:38
Show Gist options
  • Save Bas-Man/1e86a002ef5c09644567f496553737e9 to your computer and use it in GitHub Desktop.
Save Bas-Man/1e86a002ef5c09644567f496553737e9 to your computer and use it in GitHub Desktop.
Plex Media Server database backup script of Mac OS
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment