Skip to content

Commit c1cfe32

Browse files
authored
Update Linux_Plex_Backup.sh
Added KeepQty setting
1 parent 762c677 commit c1cfe32

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

Linux_Plex_Backup.sh

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# shellcheck disable=SC2317,SC2181
33
#--------------------------------------------------------------------------
44
# Backup Linux Plex Database to tgz file in Backup folder.
5-
# v1.0.5 09-Nov-2024 007revad
5+
# v1.1.6 04-Nov-2024 007revad
66
#
77
# MUST be run by a user in sudo, sudoers or wheel group, or as root
88
#
@@ -18,14 +18,15 @@
1818
# Script verified at https://www.shellcheck.net/
1919
#--------------------------------------------------------------------------
2020

21-
scriptver="v1.0.5"
21+
scriptver="v1.1.6"
2222
script=Linux_Plex_Backup
2323

2424

2525
# Read variables from backup_linux_plex.config
2626
Backup_Directory=""
2727
Name=""
2828
LogAll=""
29+
KeepQty=""
2930
if [[ -f $(dirname -- "$0";)/backup_linux_plex.config ]];then
3031
# shellcheck disable=SC1090,SC1091
3132
while read -r var; do
@@ -390,6 +391,33 @@ echo "Starting Plex..." |& tee -a "${Log_File}"
390391
systemctl start plexmediaserver
391392

392393

394+
#--------------------------------------------------------------------------
395+
# Delete old backups
396+
397+
if [[ $KeepQty -gt "0" ]]; then
398+
readarray -t array < <(ls "$Backup_Directory" |\
399+
grep -E "${Nas}"'_[0-9]{8,}(-[0-9]{4,})?_Plex_.*\.tgz' | head -n -"$KeepQty")
400+
401+
if [[ "${#array[@]}" -gt "0" ]]; then
402+
echo -e "\nDeleting old backups" |& tee -a "${Log_File}"
403+
for file in "${array[@]}"; do
404+
if [[ -f "$Backup_Directory/$file" ]]; then
405+
echo "Deleting $file" |& tee -a "${Log_File}"
406+
rm "$Backup_Directory/$file"
407+
fi
408+
if [[ -f "$Backup_Directory/${file%.tgz}.log" ]]; then
409+
echo "Deleting ${file%.tgz}.log" |& tee -a "${Log_File}"
410+
rm "$Backup_Directory/${file%.tgz}.log"
411+
fi
412+
if [[ -f "$Backup_Directory/${file%.tgz}_ERROR.log" ]]; then
413+
echo "Deleting ${file%.tgz}_ERROR.log" |& tee -a "${Log_File}"
414+
rm "$Backup_Directory/${file%.tgz}_ERROR.log"
415+
fi
416+
done
417+
fi
418+
fi
419+
420+
393421
#--------------------------------------------------------------------------
394422
# Append the time taken to stdout and log file
395423

0 commit comments

Comments
 (0)