Skip to content

Instantly share code, notes, and snippets.

@sudo-battlekafer
Last active October 16, 2021 01:45
Show Gist options
  • Save sudo-battlekafer/6517c6957e22e9b4ccd912de8ee63b23 to your computer and use it in GitHub Desktop.
Save sudo-battlekafer/6517c6957e22e9b4ccd912de8ee63b23 to your computer and use it in GitHub Desktop.
Plex Database backup shell script with hardcoded values (better for cron jobs)
#!/bin/bash
# Backup a Plex database.
# Plex Database Location. The trailing slash is
# needed and important for rsync.
# Change to your value
plexDatabase="/data/config/plex/config/Library/Application Support/Plex Media Server/"
# Location to backup the directory to.
# Change to your value
backupDirectory="/data/media/backup/Plex"
# Log file for script's output named with
# the script's name, date, and time of execution.
scriptName=$(basename ${0})
#Change Line below to your full log path
log="/data/media/backup/Plex/logs/buplex-$(date +"%Y_%m_%d_%I_%M_%p").log"
# Check for root permissions
if [[ $EUID -ne 0 ]]; then
echo -e "${scriptName} requires root privileges.\n"
echo -e "sudo $0 $*\n"
exit 1
fi
# Create Log
echo -e "***********" >> $log 2>&1
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Create Log File." | tee -a $log 2>&1
# Stop Plex
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Stopping Plex Media Server." | tee -a $log 2>&1
sudo docker stop plex | tee -a $log 2>&1
# Backup database
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Starting Backup." | tee -a $log 2>&1
# WORKING Line: sudo tar cfz "$backupDirectory/buplex-$(date '+%Y-%m(%b)-%d at %khr %Mmin').tar.gz" "$plexDatabase" >> $log 2>&1
# cd into directory so the magic --exclude below works per:
# https://stackoverflow.com/questions/984204/shell-command-to-tar-directory-excluding-certain-files-folders
cd "$plexDatabase"
sudo tar cvz --exclude='./Cache' --exclude='./Metadata' -f "$backupDirectory/buplex-$(date +"%Y_%m_%d_%I_%M_%p").tar.gz" . >> $log 2>&1
# Restart Plex
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Starting Plex Media Server." | tee -a $log 2>&1
sudo docker start plex | tee -a $log 2>&1
# Done
echo -e "$(date '+%Y-%b-%d at %k:%M:%S') :: Backup Complete. " | tee -a $log 2>&1
# echo -e "***********" >> $log 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment