|
1 | 1 | #!/usr/bin/env bash |
2 | | -# shellcheck disable=SC2317,SC2181 |
| 2 | +# shellcheck disable=SC2317,SC2181,SC2009,SC2129,SC2163 |
3 | 3 | #-------------------------------------------------------------------------- |
4 | 4 | # Backup Linux Plex Database to tgz file in Backup folder. |
5 | | -# v1.1.6 04-Nov-2024 007revad |
| 5 | +# v1.3.9 31-Aug-2025 007revad |
6 | 6 | # |
7 | 7 | # MUST be run by a user in sudo, sudoers or wheel group, or as root |
8 | 8 | # |
|
16 | 16 | # |
17 | 17 | # Github: https://github.com/007revad/Linux_Plex_Backup |
18 | 18 | # Script verified at https://www.shellcheck.net/ |
| 19 | +# |
| 20 | +# Scheduling the script: |
| 21 | +# https://www.freecodecamp.org/news/cron-jobs-in-linux/ |
| 22 | +# https://crontab.guru/ |
| 23 | +# |
| 24 | +# https://arnaudr.io/2020/08/24/send-emails-from-your-terminal-with-msmtp/ |
19 | 25 | #-------------------------------------------------------------------------- |
20 | 26 |
|
21 | | -scriptver="v1.1.6" |
| 27 | +scriptver="v1.3.9" |
22 | 28 | script=Linux_Plex_Backup |
23 | 29 |
|
24 | 30 |
|
25 | 31 | # Read variables from backup_linux_plex.config |
26 | 32 | Backup_Directory="" |
27 | 33 | Name="" |
| 34 | +snap="" |
28 | 35 | LogAll="" |
29 | 36 | KeepQty="" |
| 37 | +to_email_address="" |
| 38 | +from_email_address="" |
30 | 39 | if [[ -f $(dirname -- "$0";)/backup_linux_plex.config ]];then |
31 | 40 | # shellcheck disable=SC1090,SC1091 |
32 | 41 | while read -r var; do |
@@ -149,12 +158,83 @@ cleanup(){ |
149 | 158 | # Log and notify of backup success |
150 | 159 | echo -e "\nPlex backup completed successfully" |& tee -a "${Log_File}" |
151 | 160 | fi |
| 161 | + |
| 162 | + # Send log via email if both logging and emails are enabled |
| 163 | + if [[ $to_email_address && $from_email_address ]]; then |
| 164 | + echo -e "\nSending email..." |
| 165 | + email_contents="email_contents.txt" |
| 166 | + send_email "$to_email_address" "$from_email_address" "$Backup_Directory"\ |
| 167 | + "$email_contents" "$Nas - $script log" |
| 168 | + fi |
| 169 | + |
152 | 170 | exit "${arg1}" |
153 | 171 | } |
154 | 172 |
|
155 | 173 | trap cleanup EXIT |
156 | 174 |
|
157 | 175 |
|
| 176 | +# Send email function |
| 177 | +# shellcheck disable=SC2329 # Invoked indirectly |
| 178 | +send_email(){ |
| 179 | + # $1 is $to_email_address |
| 180 | + # $2 is $from_email_address |
| 181 | + # $3 is $Backup_Directory |
| 182 | + # $4 is $email_contents" |
| 183 | + # $5 is $subject |
| 184 | + # $6 is $mail_body |
| 185 | + |
| 186 | + if [[ ! -f "$Log_File" ]]; then |
| 187 | + echo -e "\nWARNING Cannot send email as directory $Log_File does not exist"\ |
| 188 | + |& tee -a "${Err_Log_File}" |
| 189 | + elif [[ "${3}" == "" || "${4}" == "" ]]; then |
| 190 | + echo -e "\nWARNING Send email failed. Incorrect data was passed to \"send_email\" function"\ |
| 191 | + |& tee -a "${Err_Log_File}" |
| 192 | + else |
| 193 | + if [[ -d "${3}" ]]; then # Make sure directory exists |
| 194 | + if [[ -w "${3}" ]]; then # Make sure directory is writable |
| 195 | + if [[ -r "${3}" ]]; then # Make sure directory is readable |
| 196 | + echo "To: ${1} " > "${3}/${4}" |
| 197 | + echo "From: ${2} " >> "${3}/${4}" |
| 198 | + echo "Subject: ${5}" >> "${3}/${4}" |
| 199 | + echo "" >> "${3}/${4}" |
| 200 | + cat "$Log_File" >> "${3}/${4}" |
| 201 | + |
| 202 | + #if [[ "${1}" == "" || "${2}" == "" || "${5}" == "" || "${6}" == "" ]]; then |
| 203 | + if [[ "${1}" == "" || "${2}" == "" || "${5}" == "" ]]; then |
| 204 | + echo -e "\nWARNING One or more email address parameters [to, from, subject,"\ |
| 205 | + "mail_body] was not supplied, Cannot send an email" |& tee -a "${Log_File}" |
| 206 | + else |
| 207 | + if ! command -v msmtp &> /dev/null # Verify the msmtp command is available |
| 208 | + then |
| 209 | + echo -e "\nWARNING Cannot Send Email as command \"msmtp\" was not found"\ |
| 210 | + |& tee -a "${Log_File}" |
| 211 | + else |
| 212 | + local email_response |
| 213 | + email_response=$(msmtp "${1}" < "${3}/${4}" 2>&1) |
| 214 | + if [[ "$email_response" == "" ]]; then |
| 215 | + domain=$(echo "$to_email_address" | awk -F '@' '{print $NF}') |
| 216 | + echo -e "Email sent successfully to $domain" |& tee -a "${Log_File}" |
| 217 | + else |
| 218 | + echo -e "\nWARNING An error occurred while sending email."\ |
| 219 | + "The error was: $email_response\n\n" |& tee -a "${Log_File}" |
| 220 | + fi |
| 221 | + fi |
| 222 | + fi |
| 223 | + else |
| 224 | + echo -e "Cannot send email as directory \"${3}\" does not have READ permissions"\ |
| 225 | + |& tee -a "${Log_File}" |
| 226 | + fi |
| 227 | + else |
| 228 | + echo -e "Cannot send email as directory \"${3}\" does not have WRITE permissions"\ |
| 229 | + |& tee -a "${Log_File}" |
| 230 | + fi |
| 231 | + else |
| 232 | + echo -e "Cannot send email as directory \"${3}\" does not exist" |& tee -a "${Log_File}" |
| 233 | + fi |
| 234 | + fi |
| 235 | +} |
| 236 | + |
| 237 | + |
158 | 238 | #-------------------------------------------------------------------------- |
159 | 239 | # Check that script is running as root |
160 | 240 |
|
|
179 | 259 | # DSM6 /volume1/Plex/Library/Application Support/Plex Media Server |
180 | 260 | # DSM7 /volume1/PlexMediaServer/AppData/Plex Media Server |
181 | 261 | # Linux /var/lib/plexmediaserver/Library/Application Support/Plex Media Server |
| 262 | +# snap /var/snap/plexmediaserver/common/Library/Application Support |
182 | 263 |
|
183 | 264 | # Set the Plex Media Server data location |
184 | | -Plex_Data_Path="/var/lib/plexmediaserver/Library/Application Support" |
| 265 | +if [[ ${snap,,} == "yes" ]]; then |
| 266 | + Plex_Data_Path="/var/snap/plexmediaserver/common/Library/Application Support" |
| 267 | +else |
| 268 | + Plex_Data_Path="/var/lib/plexmediaserver/Library/Application Support" |
| 269 | +fi |
185 | 270 |
|
186 | 271 |
|
187 | 272 | #-------------------------------------------------------------------------- |
|
254 | 339 |
|
255 | 340 | echo "Stopping Plex..." |& tee -a "${Log_File}" |
256 | 341 |
|
257 | | -Result=$(systemctl stop plexmediaserver) |
| 342 | +if [[ ${snap,,} == "yes" ]]; then |
| 343 | + Result=$(snap stop plexmediaserver) |
| 344 | +else |
| 345 | + Result=$(systemctl stop plexmediaserver) |
| 346 | +fi |
258 | 347 | code="$?" |
259 | 348 | # Give sockets a moment to close |
260 | 349 | sleep 5 |
@@ -304,7 +393,12 @@ if [[ -n $Response ]]; then |
304 | 393 | |& tee -a "${Log_File}" "${Tmp_Err_Log_File}" |
305 | 394 | echo "${Response}" |& tee -a "${Log_File}" "${Tmp_Err_Log_File}" |
306 | 395 | # Start Plex to make sure it's not left partially running |
307 | | - /usr/lib/plexmediaserver/Resources/start.sh |
| 396 | + if [[ ${snap,,} == "yes" ]]; then |
| 397 | + snap start plexmediaserver |
| 398 | + else |
| 399 | + #/usr/lib/plexmediaserver/Resources/start.sh |
| 400 | + systemctl start plexmediaserver |
| 401 | + fi |
308 | 402 | # Abort script because Plex didn't shut down fully |
309 | 403 | exit 255 |
310 | 404 | else |
@@ -387,8 +481,12 @@ echo "=================================================" |& tee -a "${Log_File}" |
387 | 481 | # Start Plex Media Server |
388 | 482 |
|
389 | 483 | echo "Starting Plex..." |& tee -a "${Log_File}" |
390 | | -#/usr/lib/plexmediaserver/Resources/start.sh |
391 | | -systemctl start plexmediaserver |
| 484 | +if [[ ${snap,,} == "yes" ]]; then |
| 485 | + snap start plexmediaserver |
| 486 | +else |
| 487 | + #/usr/lib/plexmediaserver/Resources/start.sh |
| 488 | + systemctl start plexmediaserver |
| 489 | +fi |
392 | 490 |
|
393 | 491 |
|
394 | 492 | #-------------------------------------------------------------------------- |
|
0 commit comments