Skip to content

Instantly share code, notes, and snippets.

@ssmereka
Created February 2, 2014 20:41
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ssmereka/8774610 to your computer and use it in GitHub Desktop.
Save ssmereka/8774610 to your computer and use it in GitHub Desktop.
Plex Media Server database backup restore script.
#!/bin/bash
# Restore a Plex database.
# Author Scott Smereka
# Version 1.0
# Script Tested on:
# Ubuntu 12.04 on 2/2/2014 [ OK ]
# Plex Database Location. The trailing slash is
# needed and important for rsync.
plexDatabase="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/"
# Location to backup the directory to.
backupDirectory="/mnt/usb/plexmediaserver/database/"
# Log file for script's output named with
# the script's name, date, and time of execution.
scriptName=$(basename ${0})
log="/mnt/usb/logs/${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 "Starting Restore of Plex Database." > $log 2>&1
echo -e "------------------------------------------------------------\n" >> $log 2>&1
# Stop Plex
echo -e "\n\nStopping Plex Media Server." >> $log 2>&1
echo -e "------------------------------------------------------------\n" >> $log 2>&1
sudo service plexmediaserver stop >> $log 2>&1
# Restore database
echo -e "\n\nStarting Database Restore." >> $log 2>&1
echo -e "------------------------------------------------------------\n" >> $log 2>&1
sudo rsync -av --delete --exclude="Logs/" --exclude="Crash Reports/" "$backupDirectory" "$plexDatabase" >> $LOG 2>&1
# Update database permissions
echo -e "\n\nUpdating Database Permissions." >> $log 2>&1
echo -e "------------------------------------------------------------\n" >> $log 2>&1
sudo chown -R plex:plex "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/" >> $log 2>&1
# Restart Plex
echo -e "\n\nStarting Plex Media Server." >> $log 2>&1
echo -e "------------------------------------------------------------\n" >> $log 2>&1
sudo service plexmediaserver start >> $log 2>&1
# Done
echo -e "\n\nRestore Complete." >> $log 2>&1
@ssmereka
Copy link
Author

ssmereka commented Feb 2, 2014

Plex Media Server database backup script gist.

@ajthom90
Copy link

ajthom90 commented May 8, 2023

I've used these scripts multiple times without any issue when backing up/restoring on same/similar hardware. Newer versions of Plex seem to download drivers to the folder that we are backing up/restoring. When migrating from an 8th-gen Intel to a 13th-gen Intel machine, I installed Plex on the new machine, which downloaded the correct driver. And then when I restored the backup, all the data was imported correctly and all my data was showing up correctly! However, it overwrote the driver and was causing some issues where HDR Tone Mapping hardware transcoding wasn't working! However, hardware transcoding worked fine when HDR tone mapping was turned off. I uninstalled Plex, and I added --exclude="Drivers/" to line 41, ran the restore script again, and now everything works great!

Thanks again for this script (and the backup script as well). They are a lifesaver, especially for someone like me who likes to tinker and is constantly changing things!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment