Skip to content

Instantly share code, notes, and snippets.

@sudo-battlekafer
Last active October 16, 2021 01:45
Show Gist options
  • Save sudo-battlekafer/ec70e443b843b17b496aaabb2532638e to your computer and use it in GitHub Desktop.
Save sudo-battlekafer/ec70e443b843b17b496aaabb2532638e to your computer and use it in GitHub Desktop.
Plex backup script with variables for paths
#!/bin/bash
# Backup a Plex database.
# Plex Database Location. The trailing slash is
# needed and important for rsync.
read -p "Enter cull path of Plex Directory" plexDatabase
name=${plexDatabase:-"/data/config/plex/config/Library/Application Support/Plex Media Server/"}
# Location to backup the directory to.
read -p "Enter Backup Directory" backupDirectory
name=${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})
read -p "Enter Log Location full path" log
name=${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