Making Plex Backups Easier

I know this isn't a Plex forum, but have you ever tried navigating theirs? Sheesh.

Anyway, I run Plex Media Server on my Ubuntu MATE 22.04.5 LTS. For reasons not known to me, the Plex sqlite3 databases sometimes go wonky, requiring me to revert to earlier versions. Plex makes backups every three days, but losing three days of updates requires time and patience to bring things back to the present, so I wrote a script that I run via cron every night to make a backup. It requires superuser privileges, so I run it in root's crontab.

#!/bin/bash
#
# Plex only backs up its databases every three days.  This is a manual method to do it 
# more often.  Please NOTE:  The plexmediaserver is taken offline in order to do this
# cleanly.  
#
# Must be run as root.  This is not secure, but...
#
# NOTE:  "******" replaces the actual root password in this script. 
#                (i.e. "******" = rootpass)
#               It might be possible to store the root password in a variable
#
# Step 1:  Stop Plex
#
echo "******" | sudo -S systemctl stop plexmediaserver
#
cd "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/"
#
# Step 2:  Copy the database files
#
echo "******" | sudo -S cp com.plexapp.plugins.library.db com.plexapp.plugins.library.db-$(date +%m-%d-%Y).bak
echo "******" | sudo -S cp com.plexapp.plugins.library.blobs.db com.plexapp.plugins.library.blobs.db-$(date +%m-%d-%Y).bak
#
# Step 3:  Apply proper ownership to file
#
echo "******" | sudo -S chown plex *db
echo "******" | sudo -S chown plex *bak
#
# Step 4:  Restart Plex & make sure it's running
#
echo "******" | sudo -S systemctl start plexmediaserver
#

If I need to restore from a backup, I simply reverse the process (no script yet, this is a manual process):
Everything is run under root.

  1. Stop Plex
  2. Rename the two "active" files (those with the simple .db extension). I give them the extension of .active
  3. Copy the two needed files, stripping off the date suffixes
  4. Change the ownership to the plex account (running as root changes the owner)
  5. Start Plex

This has saved me a lot of time and frustration. If I knew why Plex's databases were so unreliable, this wouldn't be necessary. Also, I periodically manually "prune" these backups as they have no expiration. They aren't large, but after time they can consume disk space.

2 Likes

Hi, @OldStrummer :slight_smile:

Interesting shell script. Thanks! :slight_smile: However, I'm confused: if the script is indeed being run in the "root" user's crontab, then I believe it should NOT need the root user's password in the body of the script and the shell script could then become the following:

#!/bin/bash
#
# Plex only backs up its databases every three days.  This is a manual method to do it 
# more often.  Please NOTE:  The plexmediaserver is taken offline in order to do this
# cleanly.  
#
# Must be run as root.
#
#
# Step 1:  Stop Plex
#
systemctl stop plexmediaserver
#
cd "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/"
#
# Step 2:  Copy the database files
#
cp com.plexapp.plugins.library.db com.plexapp.plugins.library.db-$(date +%m-%d-%Y).bak
cp com.plexapp.plugins.library.blobs.db com.plexapp.plugins.library.blobs.db-$(date +%m-%d-%Y).bak
#
# Step 3:  Apply proper ownership to file
#
chown plex *db
chown plex *bak
#
# Step 4:  Restart Plex & make sure it's running
#
systemctl start plexmediaserver
#
3 Likes

Point taken. I wrote the script as myself. When it worked, I then added it to root's crontab. Since it continues to work, I've left it as is.

2 Likes

Hi,
I know this doesn't answer your question but having numerous problems with Plex I decided to try Jellyfin, it's like a breath of fresh air in comparison. It may be worth giving it a try.

1 Like

I haven't heard of Jellyfin, but I'll take a look. Thanks!

1 Like