-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript
134 lines (107 loc) · 5.13 KB
/
script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# -- USER CONFIGURATION -- #
source="/mnt/user/appdata/Plex-Media-Server" # path to your plex appdata location
destination="/mnt/user/backups/Plex" # path to your backup folder
notify=yes # (yes/no) Unraid notification that the backup was performed
delete_after=7 # number of days to keep backups
fullbackup=no # (yes/no) creation of entire Plex backup (yes) or essential data only (no)
# Yes will significantly increase the amount of time and size to create a backup
# as all metadata (potentially hundreds of thousands of files) is included in the backup.
force_full_backup=7 # create a full backup every (#) number of days, in addition to regular essential data (0 to disable)
# this will create an essential backup and then a full backup separately
# this setting is ignored if fullbackup = yes
keep_full=2 # number of full backups to keep - these can be very large
# -- END USER CONFIGURATION -- #
# DO NOT MODIFY BELOW THIS LINE
#-------------------------------------------------------------------------------------------------------
start=`date +%s` # start time of script for statistics
# Read timestamp of the last full backup, if any
if [ -f /boot/config/plugins/user.scripts/scripts/last_plex_backup ]; then
while IFS= read -r line; do
lastbackup=$line
done < /boot/config/plugins/user.scripts/scripts/last_plex_backup
else
lastbackup=0
fi
# $(realpath -s $source) takes care of the presence or absense of a trailing slash (/) on the source path - error handling
cd "$(realpath -s $source)/Library/Application Support/Plex Media Server"
# set the destination directory and a date
dest=$(realpath -s $destination)/
dt=$(date +"%m-%d-%Y")
debug=false #testing only
# create the backup directory if it doesn't exist - error handling - will not create backup file it path does not exist
mkdir -p "$dest"
# create tar file of essential databases and preferences -- The Plug-in Support preferences will keep settings of any plug-ins, even though they will need to be reinstalled.
if [ $fullbackup == no ]; then
echo -e "\n\nCreating essential backup... please wait"
mkdir -p "$dest/Essential/$dt"
if [ $debug != true ]; then
tar -cf "$dest/Essential/$dt/Essential_Plex_Data_Backup-$(date +"%I_%M_%p").tar" "Plug-in Support/Databases" "Plug-in Support/Preferences" Preferences.xml
else
tar -cf "$dest/Essential/$dt/Essential_Plex_Data_Backup-debug0-$(date +"%I_%M_%p").tar" Preferences.xml
fi
if [ $force_full_backup != 0 ]; then
days=$(( ($(date --date="$date" +%s) - $(date --date="$lastbackup" +%s) )/(60*60*24) ))
if [[ $days -gt $force_full_backup ]] || [[ $lastbackup == 0 ]]
then
cf=true # created full backup
echo -e "\nCreating full backup now... please wait\n"
mkdir -p "$dest/Full/$dt"
if [ $debug != true ]; then
tar -cf "$dest/Full/$dt/Full_Plex_Data_Backup-$(date +"%I_%M_%p").tar" "$source"
else
tar -cf "$dest/Full/$dt/Full_Plex_Data_Backup-debug1-$(date +"%I_%M_%p").tar" Preferences.xml
fi
# save the date of the full backup
date > /boot/config/plugins/user.scripts/scripts/last_plex_backup
else
cf=false
echo -e "\nLast full backup created " $days " ago... skipping\n"
fi
fi
else
echo -e "\nCreating full backup... please wait\n"
mkdir -p "$dest/Full/$dt"
if [ $debug != true ]
then
tar -cf "$dest/Full/$dt/Full_Plex_Data_Backup-$(date +"%I_%M_%p").tar" "$source"
else
tar -cf "$dest/Full/$dt/Full_Plex_Data_Backup-debug2-$(date +"%I_%M_%p").tar" Preferences.xml
fi
# save the date of the full backup
date > /boot/config/plugins/user.scripts/scripts/last_plex_backup
fi
sleep 2
chmod -R 777 "$dest"
echo -e "\n\nRemoving Essential backups older than " $delete_after "days... please wait\n\n"
find $destination/Essential* -mtime +$delete_after -exec rm -rfd {} \;
old=$(( $force_full_backup*$keep_full ))
if [ -d "$destination/Full" ]; then
echo -e "Removing Full backups older than " $old "days... please wait\n\n\n"
find $destination/Full* -mtime +$old -exec rm -rfd {} \;
fi
if [ $notify == yes ]; then
if [ $fullbackup == no ]; then
if [ $cf = false ]; then
/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Plex Backup" -d "Essential Plex data has been backed up." -i "normal"
echo -e Essential backup: "$(du -sh $dest/Essential/$dt/)\n"
else
/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Plex Backup" -d "Essential & Full Plex data has been backed up." -i "normal"
echo -e Essential backup: "$(du -sh $dest/Essential/$dt/)\n"
echo -e Full backup: "$(du -sh $dest/Full/$dt/)\n"
fi
else
/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Plex Backup" -d "Complete Plex data has been backed up." -i "normal"
echo -e Full backup: "$(du -sh $dest/Full/$dt/)\n"
fi
fi
end=`date +%s`
echo -e "\nTotal time for backup: " $((end-start)) "seconds\n"
echo -e '\nAll Done!\n'
if [ -d $dest/Essential/ ]; then
echo -e Total size of all Essential backups: "$(du -sh $dest/Essential/)"
fi
if [ -d $dest/Full/ ]; then
echo -e Total size of all Full backups: "$(du -sh $dest/Full/)"
fi
exit