Skip to content

Instantly share code, notes, and snippets.

@ssmereka
Last active July 12, 2026 23:28
Show Gist options
  • Select an option

  • Save ssmereka/8773626 to your computer and use it in GitHub Desktop.

Select an option

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
@TechJedi51

TechJedi51 commented Jul 12, 2026

Copy link
Copy Markdown

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

Do you really need to stop PMS? SQLite provides an online backup mechanism that produces a transactionally consistent database while the application remains active. The crucial detail is to use Plex’s bundled SQLite executable rather than macOS’s generic sqlite3, because Plex uses its own SQLite build and extensions.

DB_DIR="$HOME/Library/Application Support/Plex Media Server/Plug-in Support/Databases"
BACKUP_DIR="/Volumes/Backups/Plex/Database"

mkdir -p "$BACKUP_DIR"

STAMP="$(date '+%Y-%m-%d_%H-%M-%S')"

"$PLEX_SQLITE" \
  "$DB_DIR/com.plexapp.plugins.library.db" \
  ".backup '$BACKUP_DIR/com.plexapp.plugins.library.db-$STAMP'"

"$PLEX_SQLITE" \
  "$DB_DIR/com.plexapp.plugins.library.blobs.db" \
  ".backup '$BACKUP_DIR/com.plexapp.plugins.library.blobs.db-$STAMP'"```
  
So a complete script might be something like:
  ```#!/bin/zsh

set -euo pipefail

PLEX_SQLITE="/Applications/Plex Media Server.app/Contents/MacOS/Plex SQLite"
DB_DIR="$HOME/Library/Application Support/Plex Media Server/Plug-in Support/Databases"
BACKUP_DIR="/Volumes/Backups/Plex/Database"
STAMP="$(date '+%Y-%m-%d_%H-%M-%S')"

MAIN_DB="$DB_DIR/com.plexapp.plugins.library.db"
BLOBS_DB="$DB_DIR/com.plexapp.plugins.library.blobs.db"

MAIN_BACKUP="$BACKUP_DIR/com.plexapp.plugins.library.db-$STAMP"
BLOBS_BACKUP="$BACKUP_DIR/com.plexapp.plugins.library.blobs.db-$STAMP"

mkdir -p "$BACKUP_DIR"

if [[ ! -x "$PLEX_SQLITE" ]]; then
    echo "Plex SQLite executable not found: $PLEX_SQLITE" >&2
    exit 1
fi

for db in "$MAIN_DB" "$BLOBS_DB"; do
    if [[ ! -f "$db" ]]; then
        echo "Database not found: $db" >&2
        exit 1
    fi
done

echo "Backing up Plex library database..."
"$PLEX_SQLITE" "$MAIN_DB" ".timeout 60000" ".backup '$MAIN_BACKUP'"

echo "Backing up Plex blobs database..."
"$PLEX_SQLITE" "$BLOBS_DB" ".timeout 60000" ".backup '$BLOBS_BACKUP'"

echo "Checking library database..."
main_result="$("$PLEX_SQLITE" "$MAIN_BACKUP" "PRAGMA integrity_check;")"

echo "Checking blobs database..."
blobs_result="$("$PLEX_SQLITE" "$BLOBS_BACKUP" "PRAGMA integrity_check;")"

if [[ "$main_result" != "ok" ]]; then
    echo "Library database integrity check failed: $main_result" >&2
    rm -f "$MAIN_BACKUP"
    exit 1
fi

if [[ "$blobs_result" != "ok" ]]; then
    echo "Blobs database integrity check failed: $blobs_result" >&2
    rm -f "$BLOBS_BACKUP"
    exit 1
fi

# Retain 14 local staging generations.
find "$BACKUP_DIR" \
    -type f \
    \( -name 'com.plexapp.plugins.library.db-*' \
       -o -name 'com.plexapp.plugins.library.blobs.db-*' \) \
    -mtime +14 \
    -delete

echo "Plex database backup completed successfully:"
echo "$MAIN_BACKUP"
echo "$BLOBS_BACKUP"```

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