User Script for important Plex data backup (v3)


Recommended Posts

Originally I used a simple 2 line script to only back up the Plex databases since I excluded Plex from CA Appdata Backup because I didn't want nightly backups with 20GB+ of Plex metadata.

 

That idea sparked @Cpt. Chaz to further modify that basic idea with some improvements and a great @SpaceInvaderOne-type video on YouTube showing the implementation and use of his script.

 

His version of the script gave me some ideas on improving my original version, which led to this new version.  

 

A lot to follow, right? :D

 

The main difference between Cpt. Chaz's script and mine is that I use a tar archive for backup and he copies the files, which can easily be changed to using tar if you wanted to.  I also back up the Plug-ins preferences directory which includes settings for scrapers, plugins, etc.

 

Then I took it a step further (for fun, and learning) to not only back up the important Plex data but to also have the ability to create periodic full Plex appdata backups (like you'd ultimately get with CA Appdata Backup).  I run CA Appdata Backup nightly but I do not need or want full Plex backups every single night, so this is my solution.

 

If you run the script manually you'll end up seeing something like this... I just created a fresh set of backups.

 

Untitled.png.af9dbe382a95352f22a711179d4a6ec0.png

 

Find the script here and you can contribute to any changes / fixes.

 

https://github.com/rcodehub/unraid-plex-script

 

Enjoy!

  • Like 4
Link to comment
  • 8 months later...

I like your script @Energen but I like to stop the docker before backup and start it back up after backup is done.

 

I use this script, maybe it possible to put it into yours, for like a V4?

 

The only thing that's not working in this script, is the time the script used. But it send a notify in unraid, and a email. And it tells if the backup did sucess or fail.

 

I can't take credit for this script, I found it on the forum a long time ago!

 

#!/bin/bash

# variables
start_m=`date +%M`
start_s=`date +%S`
echo "Script start: $start_m:$start_s"

now=$(date +"%m_%d_%Y-%H_%M")
plex_library_dir="/mnt/SSD/plex/Library"
backup_dir="/mnt/user/backup/backup_dockers/plex"
num_backups_to_keep=4

# Stop the container
docker stop Plex
echo "Stopping Plex"

# wait 30 seconds
sleep 30

# Get the state of the docker
plex_running=`docker inspect -f '{{.State.Running}}' Plex`
echo "Plex running: $plex_running"

# If the container is still running retry 5 times
fail_counter=0
while [ "$plex_running" = "true" ];
do
    fail_counter=$((fail_counter+1))
    docker stop Plex
    echo "Stopping Plex attempt #$fail_counter"
    sleep 30
    plex_running=`docker inspect -f '{{.State.Running}}' Plex`
    # Exit with an error code if the container won't stop
    # Restart plex and report a warning to the Unraid GUI
    if (($fail_counter == 5));
    then
        echo "Plex failed to stop. Restarting container and exiting"
        docker start Plex
        /usr/local/emhttp/webGui/scripts/notify -i warning -s "Plex Backup failed. Failed to stop container for backup."
        exit 1
    fi
done

# Once the container is stopped, backup the Application Support directory and restart the container
# The tar command shows progress
if [ "$plex_running" = "false" ]
then
    echo "Compressing and backing up Plex"
    cd $plex_library_dir
    tar -czf - Application\ Support/ -P | pv -s $(du -sb Application\ Support/ | awk '{print $1}') | gzip > $backup_dir/plex_backup_$now.tar.gz
    echo "Starting Plex"
    docker start Plex
fi

# Get the number of files in the backup directory
num_files=`ls $backup_dir/plex_backup_*.tar.gz | wc -l`
echo "Number of files in directory: $num_files"
# Get the full path of the oldest file in the directory
oldest_file=`ls -t $backup_dir/plex_backup_*.tar.gz | tail -1`
echo $oldest_file

# After the backup, if the number of files is larger than the number of backups we want to keep
# remove the oldest backup file
if (($num_files > $num_backups_to_keep));
then
    echo "Removing file: $oldest_file"
    rm $oldest_file
fi

end_m=`date +%M`
end_s=`date +%S`
echo "Script end: $end_m:$end_s"

runtime_m=$((end_m-start_m))
runtime_s=$((end_s-start_s))
echo "Script runtime: $runtime_m:$runtime_s"
# Push a notification to the Unraid GUI if the backup failed of passed
if [[ $? -eq 0 ]]; then
  /usr/local/emhttp/webGui/scripts/notify -i normal -s "Plex Backup completed in $runtime"
else
  /usr/local/emhttp/webGui/scripts/notify -i warning -s "Plex Backup failed. See log for more details."
fi

 

Edited by ChillZwix
Link to comment
  • 1 year later...
  • 4 months later...

Is this script still OK to use? I think my database had become corrupt as it wasn't playing anything on the web but was OK on a Nvidia Shield. Luckily there was some sort of backup (I think Plex may have automatically did it) but I would also like to have another backup.

 

Also, if there was a need to restore, as @randalotto  mentioned above, how would this be done.

 

Thanks

Edited by matt3
Link to comment
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.