-
-
Save ssmereka/8773626 to your computer and use it in GitHub Desktop.
| #!/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 |
Tested on Linux Mint 19 Mate, works great
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
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
Sadly I have a huge
/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Mediafolder 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
I'm unsure if you still have issues with this, but I went from 244G used on media to 24 after deleting video previews.
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"```
Thank you for the script! I've modified somewhat it so that:
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