Skip to content

Instantly share code, notes, and snippets.

@shnhrrsn
Forked from ssmereka/plexDatabaseBackupScript.sh
Last active January 5, 2022 13:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shnhrrsn/7f22abe085a07e250c851d53db55bae2 to your computer and use it in GitHub Desktop.
Save shnhrrsn/7f22abe085a07e250c851d53db55bae2 to your computer and use it in GitHub Desktop.
Plex Media Server database backup script.
#!/usr/bin/env bash
# Backup a Plex database.
# Author Scott Smereka
# Version 1.0
# Modified by Shaun Harrison
# Version 1.1
# Script Tested on:
# Ubuntu 14.04 on 9/6/2017 [ OK ]
# Plex Database Location. The trailing slash is
# needed and important for rsync.
plexDatabase="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/"
# Base backup location
backupDirectory="/data/media0/backups"
# Location to backup the directory to.
plexBackupDirectory="$backupDirectory/plexmediaserver/database/"
# 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 +%m%d%y%H%M%S`.log"
# Check for root permissions
if [[ $EUID -ne 0 ]]; then
echo -e "${scriptName} requires root privledges.\n"
echo -e "sudo $0 $*\n"
exit 1
fi
# 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
sudo service plexmediaserver stop 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
sudo rsync -av --delete "$plexDatabase" "$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
sudo service plexmediaserver start 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