Skip to content

Commit 6b82f02

Browse files
authored
Merge pull request #23 from 007revad/develop
Develop
2 parents ec486b4 + 377c6c4 commit 6b82f02

5 files changed

Lines changed: 245 additions & 15 deletions

File tree

CHANGES.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
v1.3.9
2+
- Added support for Plex Media Server installed in snap.
3+
- Set snap=yes in backup_linux_plex.config
4+
5+
v1.2.8
6+
- Added optional support to send an email of the log after a backup.
7+
- Set the to and from email addresses in backup_linux_plex.config
8+
- Requires msmtp (most distros include msmtp).
9+
- Bug fix for sending email twice.
10+
111
v1.1.6
212
- Added KeepQty setting to delete old backups and only keep the latest N backups.
313
- KeepQty=5 would keep just the latest 5 backups.

Linux_Plex_Backup.sh

Lines changed: 106 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2317,SC2181
2+
# shellcheck disable=SC2317,SC2181,SC2009,SC2129,SC2163
33
#--------------------------------------------------------------------------
44
# 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
66
#
77
# MUST be run by a user in sudo, sudoers or wheel group, or as root
88
#
@@ -16,17 +16,26 @@
1616
#
1717
# Github: https://github.com/007revad/Linux_Plex_Backup
1818
# 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/
1925
#--------------------------------------------------------------------------
2026

21-
scriptver="v1.1.6"
27+
scriptver="v1.3.9"
2228
script=Linux_Plex_Backup
2329

2430

2531
# Read variables from backup_linux_plex.config
2632
Backup_Directory=""
2733
Name=""
34+
snap=""
2835
LogAll=""
2936
KeepQty=""
37+
to_email_address=""
38+
from_email_address=""
3039
if [[ -f $(dirname -- "$0";)/backup_linux_plex.config ]];then
3140
# shellcheck disable=SC1090,SC1091
3241
while read -r var; do
@@ -149,12 +158,83 @@ cleanup(){
149158
# Log and notify of backup success
150159
echo -e "\nPlex backup completed successfully" |& tee -a "${Log_File}"
151160
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+
152170
exit "${arg1}"
153171
}
154172

155173
trap cleanup EXIT
156174

157175

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+
158238
#--------------------------------------------------------------------------
159239
# Check that script is running as root
160240

@@ -179,9 +259,14 @@ fi
179259
# DSM6 /volume1/Plex/Library/Application Support/Plex Media Server
180260
# DSM7 /volume1/PlexMediaServer/AppData/Plex Media Server
181261
# Linux /var/lib/plexmediaserver/Library/Application Support/Plex Media Server
262+
# snap /var/snap/plexmediaserver/common/Library/Application Support
182263

183264
# 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
185270

186271

187272
#--------------------------------------------------------------------------
@@ -254,7 +339,11 @@ fi
254339

255340
echo "Stopping Plex..." |& tee -a "${Log_File}"
256341

257-
Result=$(systemctl stop plexmediaserver)
342+
if [[ ${snap,,} == "yes" ]]; then
343+
Result=$(snap stop plexmediaserver)
344+
else
345+
Result=$(systemctl stop plexmediaserver)
346+
fi
258347
code="$?"
259348
# Give sockets a moment to close
260349
sleep 5
@@ -304,7 +393,12 @@ if [[ -n $Response ]]; then
304393
|& tee -a "${Log_File}" "${Tmp_Err_Log_File}"
305394
echo "${Response}" |& tee -a "${Log_File}" "${Tmp_Err_Log_File}"
306395
# 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
308402
# Abort script because Plex didn't shut down fully
309403
exit 255
310404
else
@@ -387,8 +481,12 @@ echo "=================================================" |& tee -a "${Log_File}"
387481
# Start Plex Media Server
388482

389483
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
392490

393491

394492
#--------------------------------------------------------------------------

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This is a bash script to backup Linux Plex Media Server settings and database, a
2020
- Stops Plex Media Server, then checks Plex actually stopped.
2121
- Backs up Plex Media Server to a tgz file (**excluding the folders listed in plex_backup_exclude.txt**).
2222
- Starts Plex Media Server.
23+
- Emails the log to your email address (optional). Use when you have scheduled the script.
2324

2425
#### It also saves a log in the same location as the backup file, including:
2526

@@ -63,6 +64,13 @@ Set Name= to distro, hostname or you can set a 'nickname'. If Name= is blank the
6364

6465
The LogAll setting enables, or disables, logging every file that gets backed up. Set LogAll= to yes or no. Blank is the same as no.
6566

67+
If to_email_address and from_email_address are set an email containing the log will be sent after the script finishes.
68+
69+
```YAML
70+
to_email_address=email@email.com
71+
from_email_address=email@email.com
72+
```
73+
6674
The KeepQty setting tells the script to keep only keep the latest N backups (and delete older backups).
6775
- If KeepQty is blank or set to 0 all backups are kept.
6876

@@ -78,6 +86,94 @@ Make sure that backup_linux_plex.config and plex_backup_exclude.txt are in the s
7886

7987
**Note:** Due to some of the commands used **this script needs to be** run by a user in sudo, sudoers or wheel group, or as root
8088

89+
If you want the script to send an email of the log after the script finishes you need to have msmtp installed (most Linux distros include msmtp).
90+
91+
### Configuring msmtp so the script can send emails
92+
93+
Depending on your Linux distro the msmtprc or config file can be either:
94+
```
95+
/etc/msmtprc
96+
~/.msmtprc
97+
$XDG_CONFIG_HOME/msmtp/config
98+
```
99+
100+
The default msmtprc or config file usually contains:
101+
```
102+
# Set default values for all following accounts.
103+
defaults
104+
timeout 15
105+
tls on
106+
tls_trust_file /usr/builtin/etc/msmtp/ca-certificates.crt
107+
#logfile ~/.msmtplog
108+
109+
# The SMTP server of the provider.
110+
#account user@gmail.com
111+
#host smtp.gmail.com
112+
#port 587
113+
#from user@gmail.com
114+
#auth on
115+
#user user@gmail.com
116+
#password passwd
117+
118+
# Set a default account
119+
#account default: user@gmail.com
120+
```
121+
122+
**Example email account settings for using a smtp server**
123+
```
124+
defaults
125+
timeout 15
126+
tls on
127+
tls_trust_file /usr/builtin/etc/msmtp/ca-certificates.crt
128+
#logfile ~/.msmtplog
129+
130+
# The SMTP server of the provider.
131+
account dave@myisp.com
132+
host mail.myisp.com
133+
port 587
134+
from dave@myisp.com
135+
auth on
136+
user dave@myisp.com
137+
password mypassword
138+
139+
# Set a default account
140+
account default: dave@myisp.com
141+
```
142+
143+
If you don't want to include your email account's password in plain text in the msmtprc or config file see https://marlam.de/msmtp/msmtprc.txt
144+
145+
**Example email account settings for using gmail**
146+
```
147+
# Set default values for all following accounts.
148+
defaults
149+
timeout 15
150+
tls on
151+
tls_trust_file /usr/builtin/etc/msmtp/ca-certificates.crt
152+
#logfile ~/.msmtplog
153+
154+
# The SMTP server of the provider.
155+
account dave@gmail.com
156+
host smtp.gmail.com
157+
port 587
158+
from dave@gmail.com
159+
auth on
160+
user dave@gmail.com
161+
password gmailapppassword
162+
163+
# Set a default account
164+
account default: dave@gmail.com
165+
```
166+
167+
For gmail you will need to generate an "app password" and use that instead of your gmail password.
168+
169+
1. Go to https://myaccount.google.com/apppasswords and sign into google.
170+
2. Enter a name in the form of `appname@computer-name` like `plexbackup@ubuntu` and click Create.
171+
3. In the "Generated app password" popup copy the 16 character app password which will be like `abcd efgh ijkl mnop`
172+
4. In your msmtprc or config file replace `password passwd` with the 16 character app password (without spaces) like:
173+
```
174+
password abcdefghijklmnop
175+
```
176+
81177
### Running the script
82178

83179
Run the script by a user in sudo, sudoers or wheel group.

Restore_Linux_Plex_Backup.sh

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# shellcheck disable=SC2317,SC2181
33
#--------------------------------------------------------------------------
44
# Companion script for Linux Plex Backup script.
5-
# v1.0.5 09-Nov-2024 007revad
5+
# v1.1.6 31-Aug-2025 007revad
66
#
77
# MUST be run by a user in sudo, sudoers or wheel group, or as root
88
#
@@ -14,13 +14,14 @@
1414
# Script verified at https://www.shellcheck.net/
1515
#--------------------------------------------------------------------------
1616

17-
scriptver="v1.0.5"
17+
scriptver="v1.1.6"
1818
script=Restore_Linux_Plex_Backup
1919

2020

2121
# Read variables from backup_linux_plex.config
2222
Backup_Directory=""
2323
Name=""
24+
snap=""
2425
LogAll=""
2526
if [[ -f $(dirname -- "$0";)/backup_linux_plex.config ]];then
2627
# shellcheck disable=SC1090,SC1091
@@ -208,7 +209,11 @@ fi
208209
# Find Plex Media Server location
209210

210211
# Set the Plex Media Server data location
211-
Plex_Data_Path="/var/lib/plexmediaserver/Library/Application Support"
212+
if [[ ${snap,,} == "yes" ]]; then
213+
Plex_Data_Path="/var/snap/plexmediaserver/common/Library/Application Support"
214+
else
215+
Plex_Data_Path="/var/lib/plexmediaserver/Library/Application Support"
216+
fi
212217

213218

214219
#--------------------------------------------------------------------------
@@ -252,7 +257,11 @@ echo Plex version: "${Version}" |& tee -a "${Log_File}"
252257

253258
echo "Stopping Plex..." |& tee -a "${Log_File}"
254259

255-
Result=$(systemctl stop plexmediaserver)
260+
if [[ ${snap,,} == "yes" ]]; then
261+
Result=$(snap stop plexmediaserver)
262+
else
263+
Result=$(systemctl stop plexmediaserver)
264+
fi
256265
code="$?"
257266
# Give sockets a moment to close
258267
sleep 5
@@ -302,7 +311,12 @@ if [[ -n $Response ]]; then
302311
|& tee -a "${Log_File}" "${Tmp_Err_Log_File}"
303312
echo "${Response}" |& tee -a "${Log_File}" "${Tmp_Err_Log_File}"
304313
# Start Plex to make sure it's not left partially running
305-
/usr/lib/plexmediaserver/Resources/start.sh
314+
if [[ ${snap,,} == "yes" ]]; then
315+
snap start plexmediaserver
316+
else
317+
#/usr/lib/plexmediaserver/Resources/start.sh
318+
systemctl start plexmediaserver
319+
fi
306320
# Abort script because Plex didn't shut down fully
307321
exit 255
308322
else
@@ -348,8 +362,12 @@ echo "=================================================" |& tee -a "${Log_File}"
348362
# Start Plex Media Server
349363

350364
echo "Starting Plex..." |& tee -a "${Log_File}"
351-
#/usr/lib/plexmediaserver/Resources/start.sh
352-
systemctl start plexmediaserver
365+
if [[ ${snap,,} == "yes" ]]; then
366+
snap start plexmediaserver
367+
else
368+
#/usr/lib/plexmediaserver/Resources/start.sh
369+
systemctl start plexmediaserver
370+
fi
353371

354372

355373
#--------------------------------------------------------------------------

0 commit comments

Comments
 (0)