Skip to content

Instantly share code, notes, and snippets.

@ssmereka
Last active April 16, 2024 16:42
Show Gist options
  • 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
@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