-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCRIPT
189 lines (156 loc) · 6.99 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
#PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
arrayStarted=true
# INCREMENTAL OR SYNC BACKUP TO UNRAID UNASSIGNED DEVICES -----------
# LINK: https://github.com/bdrtec/Unraid-Bkp-Folder-bdrtec
# AUTHOR: BRAULIO RIBEIRO
# -------------------------------------------------------------------
# CHANGE THIS VARIABLES ---------------------------------------------
St_On_Off=1
# TURN THE SCRIPT ON OR OFF. 0 disable backup, 1 enable backup. If set "0" nothing will be modified (copied or deleted)!
Script_Name="bkp_folder"
# Script name. Created in "CA user scripts" (Unraid -> Settings -> User Scripts -> "Script_Name"). A copy will be made in the folder "Script_Dir" (Script_Dir/Script_Name). Type between "" without / begin and end
Script_Dir="/mnt/user/Scripts"
# Path to backup scripts. Type between "" without / begin and end
Backup_Dir="/mnt/user/Backup"
# Path to backup all files. Type between "" without / begin and end
Day_Sync=90
# Incremental backup: the frequency is scheduled in "CA User Scripts -> Schedule Disabled"
# Sync backup: 0 disable, x enable. "Day_Sync" is how often the script will sync (mirroring) backup
Send_Email1="[email protected]"
#Send_Email2="[email protected]"
#Send_Email_TI="[email protected]"
# END CHANGE THIS VARIABLES -----------------------------------------
# TRANSLATE THIS VARIABLES TO YOUR LANGUAGE -------------------------
# Translate to your preferred language (English, Portuguese, German, Russian, Italian, Spanish, etc)
Msg_Mail_Subject="Backup: ${Backup_Dir}"
Msg_Device="Destiny"
Msg_Date="Date"
Msg_Bkp_Type_Incr="Type = Incremental"
Msg_Bkp_Type_Sync="Type = Synchronized"
Msg_Bkp_Start="Status: Backup initialized. You'll receive an email when finish"
Msg_Bkp_Finish="Status: Backup performed successfully"
Msg_Error="Status: Backup configured incorrectly"
# END TRANSLATE THIS VARIABLES TO YOUR LANGUAGE ---------------------
# INCREMENTAL AND SYNC BACKUP ---------------------------------------
Func-Incr-Sync() {
echo "" >> "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
echo "${Msg_Bkp_Start}" >> "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
Func-Send-Email
Func-Log
# Read the file with the date of the last backup in the YYYY-MM-DD format
Log_Last_Sync_Bkp=$(sed '$q' "${Src_Flash}/${Script_Name}/LOG-LAST-SYNC-BKP.txt")
# Test if the file exists or is empty
if [ ! -f "${Src_Flash}/${Script_Name}/LOG-LAST-SYNC-BKP.txt" ] || [ -z "${Log_Last_Sync_Bkp}" ]; then
# Save the date of the last backup (today) in the YYYY-MM-DD format
echo "${Today_Date}" > "${Src_Flash}/${Script_Name}/LOG-LAST-SYNC-BKP.txt"
fi
if [ -n "${Day_Sync}" ] && [ "${Day_Sync}" -ne 0 ]; then
# Difference in days. The calculation is in seconds (Today_Date - Log_Last_Sync_Bkp)
let Dif_Date=($(date +%s -d ${Today_Date})-$(date +%s -d ${Log_Last_Sync_Bkp}))/86400
# Test if the current date is the backup scheduled date
if [ "${Dif_Date}" -ge "${Day_Sync}" ]; then
St_Sync_Bkp=1
fi
fi
}
# END INCREMENTAL AND SYNC BACKUP -----------------------------------
# FUNCTION RSYNC ----------------------------------------------------
Func-Rsync() {
Rsync_Config=("-avih" "--stats")
if [ -n "${St_Sync_Bkp}" ] && [ "${St_Sync_Bkp}" -eq "1" ]; then
echo "${Today_Date}" > "${Src_Flash}/${Script_Name}/LOG-LAST-SYNC-BKP.txt"
echo "${Msg_Bkp_Type_Sync}" >> "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
Rsync_Config+=("--delete")
else
echo "${Msg_Bkp_Type_Incr}" >> "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
fi
echo "" >> "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
echo "${Msg_Bkp_Finish_On}" >> "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
Rsync_Config+=("--include-from=${Src_Flash}/${Script_Name}/PATHS.txt" "--exclude=*" "/mnt/user/" "${Backup_Dir}")
rsync "${Rsync_Config[@]}"
}
# END FUNCTION RSYNC ------------------------------------------------
# FUNCTION SEND EMAIL ----------------------------------------------
Func-Send-Email() {
if [ -n "${Send_Email1}" ]; then
sendmail "${Send_Email1}" < "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
fi
if [ -n "${Send_Email2}" ]; then
sendmail "${Send_Email2}" < "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
fi
if [ -n "${Send_Email_TI}" ]; then
sendmail "${Send_Email_TI}" < "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
fi
}
# END FUNCTION SEND EMAIL ------------------------------------------
# FUNCTION LOG ------------------------------------------------------
Func-Log() {
Today_Date=$(date +%Y-%m-%d)
cat << EOF > "${Src_Flash}/${Script_Name}/LOG-BKP.txt"
From: "${Send_Email1}"
To: "${Send_Email1}"
Cc: "${Send_Email2}"
Cco: "${Send_Email_TI}"
Subject: ${Msg_Mail_Subject}
${Msg_Device} = ${Backup_Dir}
${Msg_Date} = ${Today_Date}
EOF
}
# END FUNCTION LOG --------------------------------------------------
# FUNCTION CREATE SCRIPT --------------------------------------------
Func-Create-Script() {
Src_Flash="/boot/config/plugins/user.scripts/scripts"
if [ ! -d "${Script_Dir}/${Script_Name}" ]; then
mkdir "${Script_Dir}/${Script_Name}"
fi
if [ ! -f "${Src_Flash}/${Script_Name}/PATHS.txt" ]; then
touch "${Src_Flash}/${Script_Name}/PATHS.txt"
fi
if [ ! -d "${Backup_Dir}" ]; then
mkdir "${Backup_Dir}"
fi
}
# END FUNCTION CREATE SCRIPT ----------------------------------------
# FUNCTION SYNC SCRIPT ----------------------------------------------
Func-Sync-Script() {
if [ -d "${Src_Flash}/${Script_Name}" ]; then
# Copy "LOG-BKP.txt"
if [ -f "${Src_Flash}/${Script_Name}/LOG-BKP.txt" ]; then
cp -R "${Src_Flash}/${Script_Name}/LOG-BKP.txt" "${Script_Dir}/${Script_Name}/"
fi
# Copy "LOG-LAST-SYNC-BKP.txt"
if [ -f "${Src_Flash}/${Script_Name}/LOG-LAST-SYNC-BKP.txt" ]; then
cp -R "${Src_Flash}/${Script_Name}/LOG-LAST-SYNC-BKP.txt" "${Script_Dir}/${Script_Name}/"
fi
# Copy "PATHS.txt"
if [ -f "${Script_Dir}/${Script_Name}/PATHS.txt" ]; then
cp -R "${Script_Dir}/${Script_Name}/PATHS.txt" "${Src_Flash}/${Script_Name}/"
elif [ -f "${Src_Flash}/${Script_Name}/PATHS.txt" ]; then
cp -R "${Src_Flash}/${Script_Name}/PATHS.txt" "${Script_Dir}/${Script_Name}/"
fi
# Change folder permission
chown -R root:users "${Script_Dir}/${Script_Name}/"
chmod -R 660 "${Script_Dir}/${Script_Name}/"
fi
}
# END FUNCTION SYNC SCRIPT ------------------------------------------
# MAIN --------------------------------------------------------------
# Check if all variables are filled correctly
if ([ -d "/boot/config/plugins/user.scripts/scripts" ] && [ -d "${Script_Dir}" ] && [ -n "${Script_Name}" ] && [ -n "${Backup_Dir}" ] &&
([ -n "${Send_Email1}" ] || [ -n "${Send_Email2}" ] || [ -n "${Send_Email_TI}" ]) &&
([ -n "${Day_Sync}" ] && ([ "${Day_Sync}" -ge "0" ] && [ "${Day_Sync}" -le "360" ])) &&
([ -n "${St_On_Off}" ] && ([ "${St_On_Off}" -eq "0" ] || [ "${St_On_Off}" -eq "1" ])) ); then
if [ "${St_On_Off}" -eq "1" ]; then
Func-Create-Script
Func-Sync-Script
Func-Log
Func-Incr-Sync
Func-Rsync
Func-Sync-Script
Func-Send-Email
fi
else
/usr/local/emhttp/webGui/scripts/notify -s "${Msg_Device}" -m "${Msg_Error}" -t
fi
# END MAIN ----------------------------------------------------------