Skip to content

Instantly share code, notes, and snippets.

@ssmereka
Last active April 16, 2024 16:42
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save ssmereka/8773626 to your computer and use it in GitHub Desktop.
Save ssmereka/8773626 to your computer and use it in GitHub Desktop.
Plex Media Server database backup script.
#!/bin/bash
# Backup 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="/srv/raid10/backups/plexmediaserver/database/"
# Log file for script's output named with
# the script's name, date, and time of execution.
scriptName=$(basename ${0})
log="/srv/raid10/backups/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 "Staring Backup 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
# Backup database
echo -e "\n\nStarting Backup." >> $log 2>&1
echo -e "------------------------------------------------------------\n" >> $log 2>&1
sudo rsync -av --delete "$plexDatabase" "$backupDirectory" >> $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\nBackup Complete." >> $log 2>&1
@ssmereka
Copy link
Author

ssmereka commented Feb 2, 2014

Plex Media Server database restore script gist.

@picardflo
Copy link

Hello,

Thanks, for your script, work fine for me (on Ubuntu 16.04 LTS)

For Windows and Linux systems, you can exclude the Cache directory if you wish.
more informations : here

sudo rsync -av --delete --exclude="Cache/" "$plexDatabase" "$backupDirectory" >> $log 2>&1

Regards

@Viharrai
Copy link

Viharrai commented Dec 19, 2017

Thanks for the script. Works on Xenial (16.04).
I prefer seeing some status on the terminal, so I replaced the >> $log with | tee -a $log on all except the rsync line.

@Bas-Man
Copy link

Bas-Man commented Jan 18, 2019

Thanks for this. I forked it and made it work under Mac OS.

@basti2015
Copy link

Can you add the feature to delete Backup-Version there are older then like 14 versions?

@SPAC3MANSP1FF
Copy link

Working on 18.04. Thanks!

@theluke
Copy link

theluke commented May 27, 2020

It worked in focal fossa, good job, thank you!

@Griffone01
Copy link

Griffone01 commented Sep 7, 2020

Thank you for the script! I've modified somewhat it so that:

  1. Now EXCLUDES the /Cache directory (that was trickier than expected) which for me saves ~700 mb of space per backup.
  2. Also updated log to append so log is a continuous track of when and what.
  3. Also updated as I have both a READ ONLY mount and a READ/WRITE mount, so now the R/W share is ONLY mounted at run time of script (me and my “this user should ONLY ever have READ access to my media” paranoia… lol)… NOTE: this also then requires the /mnt/buplex share to have the “noauto” option in /etc/fstab file…
  4. Creates a different backup file via TAR each time, vs using RSYNC (which I found walking my tree rather slow and painful, and I want separate DB backups in case DB gets corrupted somehow... I don't to overwrite a good backup with a bad one etc).
  5. Possibly a few other tweaks I'm forgetting. Sorry.

NOTE: the LAST bit of the file (unmounting R/W share) happens AFTER logging and r/w access to the log file (in my setup) ends... so NO ERROR will be caught if unmount of r/w mount fails. Move your log file somewhere else if this concerns you more than me (ie, you set this up to be automated, not manually run) ;-)

Some of the above others probably don’t want or need, but just FYI might prove useful (and as a note to self for my own future sanity!)

SCRIPT CODE BELOW HERE

#!/bin/bash


# Backup a Plex database.
# Original Author Scott Smereka
# Edited by Neil C.
# Version 1.1


# Script Tested on:
# Ubuntu 20.04 on 2020-Aug-9 [ 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/buplex/Archive/Plex_Backups"


# Log file for script's output named with 
# the script's name, date, and time of execution.
scriptName=$(basename ${0})
log="/mnt/buplex/Archive/Plex_Backups/logs/buplex.log"


# Check for root permissions
if [[ $EUID -ne 0 ]]; then
echo -e "${scriptName} requires root privileges.\n"
echo -e "sudo $0 $*\n"
exit 1
fi


# Mount Plex Media Share in R/W mode
sudo mount /mnt/buplex 

# Create Log
echo -e "***********" >> $log 2>&1
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Mounted Share in R/W Mode." | tee -a $log 2>&1


# Stop Plex
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Stopping Plex Media Server." | tee -a $log 2>&1
sudo service plexmediaserver stop | tee -a $log 2>&1


# Backup database
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Starting Backup." | tee -a $log 2>&1
# WORKING Line: sudo tar cfz "$backupDirectory/buplex-$(date '+%Y-%m(%b)-%d at %khr %Mmin').tar.gz"  "$plexDatabase" >> $log 2>&1
# cd into  directory so the magic --exclude below works per:
# https://stackoverflow.com/questions/984204/shell-command-to-tar-directory-excluding-certain-files-folders
cd "$plexDatabase"
sudo tar cz --exclude='./Cache' -f "$backupDirectory/buplex-$(date '+%Y-%m(%b)-%d at %khr %Mmin').tar.gz" . >> $log 2>&1


# Restart Plex
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Starting Plex Media Server." | tee -a $log 2>&1
sudo service plexmediaserver start | tee -a $log 2>&1


# Done
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Backup Complete. Unmounting R/W Share..." | tee -a $log 2>&1
echo -e "***********" >> $log 2>&1
sudo umount /mnt/buplex

@Ukstormer
Copy link

Tested on Linux Mint 19 Mate, works great

@Ukstormer
Copy link

Can you add the feature to delete Backup-Version there are older then like 14 versions?

Create another bash file and add the following then run a cron weekly to keep things tidy, this is set to 10 days but depends how often you backup.

#!/bin/bash
#Name:
#Author:
#Date:
#Description: Cleanup files older than 10 days inside DIRECTOR_TO_CLEANUP

DIRECTORY_TO_CLEANUP="/mnt/buplex/Archive/Plex_Backups"
LOGFILE="/home/cleanup.txt"

/usr/bin/find ${DIRECTORY_TO_CLEANUP} -type f -mtime +10 -delete >${LOGFILE} 2>&1

@alneven
Copy link

alneven commented Apr 20, 2023

Sadly I have a huge /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Media folder that rsync and tar got killed within the process to backup that 'Media' folder.

Any other idea?

tar -zcvf is running for a while and it gets killed:

Media/localhost/1/571848b10aa99619bf4b3fab7e89847194a46fe.bundle/Contents/Subtitle Contributions/com.plexapp.agents.localmedia/
Media/localhost/1/571848b10aa99619bf4b3fab7e89847194a46fe.bundle/Contents/Subtitle Contributions/com.plexapp.system/
Media/localhost/1/571848b10aa99619bf4b3fab7e89847194a46fe.bundle/Contents/GoP-0.xml
Media/localhost/1/571848b10aa99619bf4b3fab7e89847194a46fe.bundle/Contents/Subtitles/
Media/localhost/1/ef636c36896a6c53e515dd3c07a4df2d8dbd532.bundle/
Media/localhost/1/ef636c36896a6c53e515dd3c07a4df2d8dbd532.bundle/Contents/
Media/localhost/1/ef636c36896a6c53e515dd3c07a4df2d8dbd532.bundle/Contents/Subtitles.xml
Media/localhost/1/ef636c36896a6c53e515dd3c07a4df2d8dbd532.bundle/Contents/Thumbnails/
Media/localhost/1/ef636c36896a6c53e515dd3c07a4df2d8dbd532.bundle/Contents/Thumbnails/thumb1.jpg
Killed

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