11#! /usr/bin/env bash
22# shellcheck disable=SC2317,SC2181
33# --------------------------------------------------------------------------
4- # Companion script for Backup Asustor Plex script.
5- # v1.0.0 04-Feb-2023 007revad
4+ # Companion script for Linux Plex Backup script.
5+ # v1.0.2 10-Apr-2024 007revad
66#
77# MUST be run by a user in sudo, sudoers or wheel group, or as root
88#
99# To run the script:
10- # sudo i /volume1 /scripts/restore_asustor_plex_backup .sh
11- # Change /volume1 /scripts/ to the path where this script is located
10+ # sudo i /share /scripts/Restore_Linux_Plex_Backup .sh
11+ # Change /share /scripts/ to the path where this script is located
1212#
13- # Github: https://github.com/007revad/Asustor_Plex_Backup
13+ # Github: https://github.com/007revad/Linux_Plex_Backup
1414# Script verified at https://www.shellcheck.net/
1515# --------------------------------------------------------------------------
16- # REQUIRED:
17- # Because the Asustor only has BusyBox this script needs bash installed.
18- #
19- # Install Entware from App Central, then run the following commands via SSH
20- # You can run the commands in "Shell In A Box" from App Central, or use PuTTY
21- # opkg update && opkg upgrade
22- # opkg install bash
23- # --------------------------------------------------------------------------
2416
25- # Process Expansion and redirecting stdout and stderr to separate Log and
26- # Error Log causes an error on Asustor NAS unless bash is installed.
27- # Check if script is running in GNU bash and not BusyBox ash
28-
29- Shell=$( /proc/self/exe --version 2> /dev/null | grep " GNU bash" | cut -d " ," -f1)
30- if [ " $Shell " != " GNU bash" ]; then
31- echo -e " \nYou need to install bash to be able to run this script.\n"
32- echo " 1. Install Entware from App Central"
33- echo " 2. Run the following commands in a shell:"
34- echo " opkg update && opkg upgrade"
35- echo -e " opkg install bash\n"
36- exit 1
37- fi
17+ scriptver=" v1.0.2"
18+ script=Restore_Linux_Plex_Backup
3819
3920
40- # Read variables from backup_asustor_plex.config
41- if [[ -f $( dirname -- " $0 " ; ) /backup_asustor_plex.config ]]; then
21+ # Read variables from backup_linux_plex.config
22+ Backup_Directory=" "
23+ Name=" "
24+ LogAll=" "
25+ if [[ -f $( dirname -- " $0 " ; ) /backup_linux_plex.config ]]; then
4226 # shellcheck disable=SC1091
43- source " $( dirname -- " $0 " ; ) " /backup_asustor_plex .config
27+ source " $( dirname -- " $0 " ; ) " /backup_linux_plex .config
4428else
45- echo " backup_asustor_plex .config file missing!"
29+ echo " backup_linux_plex .config file missing!"
4630 exit 1
4731fi
4832
5236if [[ ! -d $Backup_Directory ]]; then
5337 echo " Backup directory not found:"
5438 echo " $Backup_Directory "
55- echo " Check your setting in backup_asustor_plex .config"
39+ echo " Check your setting in backup_linux_plex .config"
5640 exit 1
5741fi
5842
@@ -72,17 +56,9 @@ Started=$( date )
7256
7357# shellcheck disable=SC2154
7458case " ${Name,,} " in
75- brand)
76- # Get NAS Brand
77- if [[ -f /etc/nas.conf ]]; then
78- Nas=" $( awk ' /^Vendor\s/{print $3}' /etc/nas.conf) "
79- fi
80- ;;
81- model)
82- # Get Asustor model
83- if [[ -f /etc/nas.conf ]]; then
84- Nas=" $( awk ' /^Model\s/{print $3}' /etc/nas.conf) "
85- fi
59+ distro)
60+ # Get Linux Distro
61+ Nas=" $( uname -a | awk ' {print $2}' ) "
8662 ;;
8763 hostname|" " )
8864 # Get Hostname
9874# --------------------------------------------------------------------------
9975# Get list of backups from backup path
10076
101- # BusyBox gives an error if process substitution is used by non-root user!
10277# filelist=()
10378# readarray -d '' filelist < <(find "${Backup_Directory}/" -size +100\
10479# \( -iname "${Nas}*.tgz" -o -iname "${Nas}*.tar.gz" \) -print0)
@@ -150,7 +125,6 @@ Backup_Name="Restore_Plex_Backup"
150125
151126# Set log filename
152127Log_File=" ${Backup_Directory} " /" ${Backup_Name} " .log
153- echo " Restores Asustor Plex Backup" | & tee " ${Log_File} "
154128
155129# Set error log filename
156130Err_Log_File=" ${Backup_Directory} " /" ${Backup_Name} " _ERROR.log
@@ -159,8 +133,6 @@ Err_Log_File="${Backup_Directory}"/"${Backup_Name}"_ERROR.log
159133# --------------------------------------------------------------------------
160134# Create temp error log
161135
162- # Asustor mktemp only accepts max 6 Xs
163-
164136# Create temp directory for temp error log
165137Tmp_Dir=$( mktemp -d -t plex_to_tar-XXXXXX)
166138
@@ -172,7 +144,8 @@ Tmp_Err_Log_File=$(mktemp "${Tmp_Dir}"/errorlog-XXXXXX)
172144# Create trap and clean up function
173145
174146# Tmp logs clean up function
175- cleanup () {
147+ # shellcheck disable=SC2329
148+ cleanup (){
176149 arg1=$?
177150 # Move tmp_error_log to error log if tmp_error_log is not empty
178151 if [[ -s $Tmp_Err_Log_File ]] && [[ -d $Backup_Directory ]]; then
@@ -229,31 +202,11 @@ if [[ $( whoami ) != "root" ]]; then
229202fi
230203
231204
232- # --------------------------------------------------------------------------
233- # Check script is running on an Asustor NAS
234-
235- if [[ -f /etc/nas.conf ]]; then Brand=" $( awk ' /^Vendor\s/{print $3}' /etc/nas.conf) " ; fi
236- # Returns: ASUSTOR
237-
238- if [[ ${Brand,,} != " asustor" ]]; then
239- if [[ -d $Backup_Directory ]]; then
240- echo " Checking script is running on a Asustor NAS" | & tee -a " ${Tmp_Err_Log_File} "
241- echo " ERROR: $( hostname) is not a Asustor! Aborting." | & tee -a " ${Tmp_Err_Log_File} "
242- else
243- # Can't log error to log file because $Backup_Directory does not exist
244- echo -e " \nChecking script is running on a Asustor NAS"
245- echo -e " ERROR: $( hostname ) is not a Asustor! Aborting.\n"
246- fi
247- # Abort script because it's being run on the wrong NAS brand
248- exit 255
249- fi
250-
251-
252205# --------------------------------------------------------------------------
253206# Find Plex Media Server location
254207
255208# Set the Plex Media Server data location
256- Plex_Data_Path=/share/Plex/ Library
209+ Plex_Data_Path=" /var/lib/plexmediaserver/ Library/Application Support "
257210
258211
259212# --------------------------------------------------------------------------
270223# --------------------------------------------------------------------------
271224# Get Plex Media Server version
272225
273- Version=" $( /usr/local/AppCentral /plexmediaserver/Plex\ Media\ Server --version) "
226+ Version=" $( /usr/lib /plexmediaserver/Plex\ Media\ Server --version) "
274227# Returns v1.29.2.6364-6d72b0cf6
275228# Plex version without v or hex string
276229Version=$( printf %s " ${Version: 1} " | cut -d " -" -f1)
@@ -280,10 +233,12 @@ Version=$(printf %s "${Version:1}"| cut -d "-" -f1)
280233# --------------------------------------------------------------------------
281234# Start logging
282235
283- # Log NAS brand, model, DSM version and hostname
284- Model=" $( awk ' /^Model\s/{print $3}' /etc/nas.conf) "
285- ADMversion=" $( awk ' /^Version\s/{print $3}' /etc/nas.conf) "
286- echo " ${Brand} " " ${Model} " ADM " ${ADMversion} " | & tee -a " ${Log_File} "
236+ echo -e " $script $scriptver \n" | & tee -a " ${Log_File} "
237+
238+ # Log Linux distro, version and hostname
239+ Distro=" $( uname -a | awk ' {print $2}' ) "
240+ DistroVersion=" $( uname -a | awk ' {print $3}' | cut -d" -" -f1) "
241+ echo " ${Distro} " " ${DistroVersion} " | & tee -a " ${Log_File} "
287242echo " Hostname: $( hostname ) " | & tee -a " ${Log_File} "
288243
289244# Log Plex version
@@ -295,18 +250,16 @@ echo Plex version: "${Version}" |& tee -a "${Log_File}"
295250
296251echo " Stopping Plex..." | & tee -a " ${Log_File} "
297252
298- Result=$( /usr/local/AppCentral/plexmediaserver/CONTROL/start-stop.sh stop)
253+ Result=$( systemctl stop plexmediaserver)
254+ code=" $? "
299255# Give sockets a moment to close
300256sleep 5
301257
302- if [[ -n $Result ]]; then
303- if [[ $Result == * " stopped process in pidfile" * ]]; then
304- echo " Plex Media Server has stopped." | & tee -a " $Log_File "
305- elif [[ $Result == * " none killed" * ]]; then
306- echo " Plex Media Server wasn't running." | & tee -a " $Log_File "
307- else
308- echo " $Result " | & tee -a " $Log_File "
309- fi
258+ if [[ $code == " 0" ]]; then
259+ echo " Plex Media Server has stopped." | & tee -a " $Log_File "
260+ else
261+ echo " $Result " | & tee -a " $Log_File "
262+ exit $code
310263fi
311264
312265
@@ -337,8 +290,8 @@ Response=$(pgrep -l plex)
337290# Check if plexmediaserver was found in $Response
338291if [[ -n $Response ]]; then
339292 # Forcefully kill any residual Plex processes (plug-ins, tuner service and EAE etc)
340- pgrep plex | xargs kill -9 & > /dev/null
341- sleep 2
293+ pgrep [Pp]lex | xargs kill -9 & > /dev/null
294+ sleep 5
342295
343296 # Check if plexmediaserver still found in $Response
344297 Response=$( pgrep -l plex)
@@ -347,7 +300,7 @@ if [[ -n $Response ]]; then
347300 | & tee -a " ${Log_File} " " ${Tmp_Err_Log_File} "
348301 echo " ${Response} " | & tee -a " ${Log_File} " " ${Tmp_Err_Log_File} "
349302 # Start Plex to make sure it's not left partially running
350- /usr/local/AppCentral/ plexmediaserver/CONTROL /start-stop .sh start
303+ /usr/lib/ plexmediaserver/Resources /start.sh
351304 # Abort script because Plex didn't shut down fully
352305 exit 255
353306 else
@@ -393,7 +346,7 @@ echo "=================================================" |& tee -a "${Log_File}"
393346# Start Plex Media Server
394347
395348echo " Starting Plex..." | & tee -a " ${Log_File} "
396- /usr/local/AppCentral/ plexmediaserver/CONTROL /start-stop .sh start
349+ /usr/lib/ plexmediaserver/Resources /start.sh
397350
398351
399352# --------------------------------------------------------------------------
0 commit comments