-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebian-Updater.sh
479 lines (422 loc) · 11.7 KB
/
Debian-Updater.sh
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash
# Debian-Updater.sh by SubleXBle (https://github.com/SubleXBle/Debian-Updater) !Update with ease and how YOU like it!
# Change Directory to the Directory where the Script is placed
cd "$(dirname "$0")"
### User-Config (DEB_UPD_config.sh) ###
source DEB_UPD_config.sh
# Language File
source Files/Language/Lang.$UV_LNG.sh
# Check for Logfile : if it does not exist it will be created
source Files/LogFileCheck.sh
# get Start Time
start=$(date +%s)
# write Starttime and Date to Logfile
F_Startzeit() {
echo "$(date +"%d.%m.%Y %H:%M:%S")">>$LOGFILE
}
### SCRIPT VARS ###
V_ScriptVersion="1.0.1"
NORMAL='\033[0;39m'
GREEN='\033[1;32m'
RED='\033[1;31m'
YELLOW='\033[33m'
BLUE='\033[34m'
TXTBOLD="\e[1m"
FEHLER=0
SILENT=false
ONLYUPDATE=false
NOAUTOREMOVE=false
CheckNotifications=false
### User-Config (DEB_UPD_config.sh) ###
source DEB_UPD_config.sh
### load Notification-Config if needed ###
# Pushover
[ "$UV_PUSHOVER" = true ] && source NotificationConfiguration/PUSHOVER_config.sh
[ "$UV_TELEGRAM" = true ] && source NotificationConfiguration/TELEGRAM_config.sh
[ "$UV_GOTIFY" = true ] && source NotificationConfiguration/GOTIFY_config.sh
[ "$UV_DISCORD" = true ] && source NotificationConfiguration/DISCORD_config.sh
[ "$UV_EMAIL" = true ] && source NotificationConfiguration/EMAIL_config.sh
[ "$UV_TEAMS" = true ] && source NotificationConfiguration/TEAMS_config.sh
### SWITCHES ###
# Silent Switch
log_message() {
if [ "$SILENT" = false ]; then
echo -e "$@"
fi
}
for arg in "$@"; do
case $arg in
-s|--silent)
SILENT=true
shift
;;
*)
#
;;
esac
done
# Help Switch
if [[ $1 == "-h" || $1 == "--help" ]]; then
if [ "$UV_LNG" = DE ]; then
source Files/DEB_UPD_help.sh
fi
if [ "$UV_LNG" = EN ]; then
source Files/DEB_UPD_help_en.sh
fi
if [ "$UV_LNG" = SP ]; then
source Files/DEB_UPD_help_sp.sh
fi
F_HELP
exit 0
fi
# only Update Switch
if [[ $1 == "-o" || $1 == "--onlyupdate" ]]; then
ONLYUPDATE=true
fi
# No Autoremove Switch
if [[ $1 == "-n" || $1 == "--no-autoremove" ]]; then
NOAUTOREMOVE=true
fi
# License Switch
if [[ $1 == "-l" || $1 == "--license" ]]; then
cat LICENSE
exit 0
fi
# Dist-Upgrade Once Switch
if [[ $1 == "-d" || $1 == "--dist-upgrade" ]]; then
DistUpgradeOnce=true
fi
# Test Notification Switch
if [[ $1 == "-tn" || $1 == "--test-notifications" ]]; then
CheckNotifications=true
fi
### CHECKS ###
# Loglevel check
case "$UV_LOG" in
"quiet")
V_LOGGING="-qq"
;;
"medium")
V_LOGGING="-q"
;;
"full")
V_LOGGING=""
;;
*)
V_LOGGING="-qq" # set LoggingLevel to quiet when no other option is set (Standard)
echo "Ungültiger Loglevel: $UV_LOG" >&2
exit 1
;;
esac
### Sanity Checks ##
# Used Sanity Check Scripts
check1="Files/varcheckone.sh"
check2="Files/varchecktwo.sh"
# check the vars
check_scripts() {
# Warte auf aktive Prozesse (dynamisch prüfen)
while { [[ -n "$pid_varcheckone" ]] && kill -0 "$pid_varcheckone" 2>/dev/null; } || \
{ [[ -n "$pid_varchecktwo" ]] && kill -0 "$pid_varchecktwo" 2>/dev/null; }; do
log_message $NORMAL
log_message -n $NORMAL "Check Sanity [$YELLOW!$NORMAL]"
sleep 1
log_message -ne "\r"
done
# Check for Exit State
exit_status1=0
exit_status2=0
if [[ -n "$pid_varcheckone" ]]; then
wait "$pid_varcheckone"
exit_status1=$?
fi
if [[ -n "$pid_varchecktwo" ]]; then
wait "$pid_varchecktwo"
exit_status2=$?
fi
# output
if [[ $exit_status1 -ne 0 || $exit_status2 -ne 0 ]]; then
log_message $NORMAL "Sanity Checks failed $NORMAL[$RED✗$NORMAL]"
exit 1
else
log_message -n $NORMAL "Checks done $NORMAL[$GREEN✓$NORMAL]"
sleep 1
log_message -ne "\r"
log_message -n $NORMAL "Script starts $GREEN $NORMAL[$GREEN✓$NORMAL]"
log_message $NORMAL
fi
}
# Start Checks and save PITs if possible
[[ -f "$check1" ]] && source "$check1" & pid_varcheckone=$!
[[ -f "$check2" ]] && source "$check2" & pid_varchecktwo=$!
# Call Sanity Check Function
check_scripts
### FUNCTIONS ###
# Check Notifications
if [ "$CheckNotifications" = true ]; then
source Files/NOTIFICATION_FUNCTIONS.sh
UV_NotifyOnlyOnError=false
V_NOTIFY=true
F_PUSHOVER
F_TELEGRAM
send_gotify_message
F_DISCORD
F_EMAIL
F_TEAMS
exit 0;
fi
# Check for Root
F_ISROOT() {
log_message -n -e $NORMAL "$LV_RootCheck \t \t \t "
if [ "$EUID" -ne 0 ]; then
LF_ISROOT_Neg
else
# log_message -e "$GREEN Root erkannt! \t "
LF_ISROOT_Pos
fi
}
# Show OS-Version
F_VERSION_ANZEIGEN() {
VERSIONSGREP=$(cat /etc/*release | grep PRETTY_NAME | cut -c 13-)
log_message -n -e $YELLOW "DEBIAN VERSION \t \t "
log_message -n -e $YELLOW $VERSIONSGREP
}
# update Repositorys
F_UPDATE() {
log_message -n "$NORMAL $LV_Update_start" | tee -a $LOGFILE
if apt-get update $V_LOGGING >>$LOGFILE 2>&1; then
LF_Positive_Output_Check
echo "$LV_Update_done">>$LOGFILE
if [ "$ONLYUPDATE" = true ]; then
F_ZEITAUSGABE
exit;
fi
else
FEHLER=RepoUpdateError
LF_Negative_Output_Check
echo "$LV_Update_error">>$LOGFILE
fi
}
# Show Upgradeable
F_ANZEIGE() {
log_message "$NORMAL $LV_Show_upgradeable"
if [ "$SILENT" = false ]; then
# Wenn Silent deaktiviert ist, wird die Ausgabe sowohl in die Konsole als auch ins Logfile geschrieben
if apt list --upgradeable 2>/dev/null | tee -a $LOGFILE; then
echo "Pakete wurden angezeigt" >>$LOGFILE
else
FEHLER=ShowUpgradeableError
LF_Negative_Output_Check
echo "Fehler bei apt list --upgradeable" >>$LOGFILE
fi
else
# Wenn Silent aktiviert ist, wird nur ins Logfile geschrieben, ohne Konsolenausgabe
if apt list --upgradeable >>$LOGFILE 2>/dev/null; then
echo "Pakete wurden angezeigt" >>$LOGFILE
else
FEHLER=ShowUpgradeableError
LF_Negative_Output_Check
echo "Fehler bei apt list --upgradeable" >>$LOGFILE
fi
fi
}
# Upgrade
F_UPGRADE() {
# Check for Upgrade-Mode
if [ "$UV_UpgradeMode" = true ]; then
V_UpgrMod="dist-upgrade -y"
else
V_UpgrMod="upgrade -y"
fi
if [ "$DistUpgradeOnce" = true ]; then
V_UpgrMod="dist-upgrade -y"
fi
# Log Beginning of Upgrade
log_message -n "$LV_Install \t \t "
# Upgrade
if apt-get $V_UpgrMod $V_LOGGING >>$LOGFILE 2>&1; then
LF_Positive_Output_Check
echo "$LFA_Upgrade_Y" >>$LOGFILE
else
# Check for Error state
FEHLER="UpgradeError"
LF_Negative_Output_Check
echo "$LFA_Upgrade_N" >>$LOGFILE
# Write to Logfile
echo -e "$NORMAL Fehlercode: $RED $? $NORMAL" >>$LOGFILE
fi
}
# Autoremove
F_AUTOREMOVE() {
if [ "$UV_AutoremoveMode" == true ]; then
V_Autorem_Mod=""
else
V_Autorem_Mod="--purge"
fi
if [ "$NOAUTOREMOVE" = false ]; then
log_message -n "$NORMAL $LV_Autoremove \t \t "
if apt-get autoremove $V_Autorem_Mod -y $V_LOGGING >>$LOGFILE 2>&1; then
LF_Positive_Output_Check
echo "$LFA_Autoremove_Y">>$LOGFILE
else
FEHLER=AutoremoveError
LF_Negative_Output_Check
echo "LFA_Autoremove_N">>$LOGFILE
fi
fi
}
[ "$UV_RKHUNTER" = true ] && source Files/RK_Hunter.sh
# Write Logfile to VAR
# Needed to send Logfile via Notification.
F_LOG_TO_VAR() {
if [[ "$UV_PUSHOVER" == true || "$UV_TELEGRAM" == true || "$UV_GOTIFY" == true || "$UV_DISCORD" == true || "$UV_EMAIL" == true || "$UV_TEAMS" == true ]] && [ "$UV_LOG2MSG" = true ]; then
BENACHRICHTIGUNG=$(cat "$LOGFILE")
#if [ "$UV_NotifyOrLog" = true ];
# Still thinking about how to make it in a nice way
#fi
fi
}
# Delete Logfile(s)
F_LOGKILL() {
log_message -n "$NORMAL $LV_LogKiller \t \t \t \t "
if [ "$UV_KEEP_LOG" = false ]; then
rm -f $LOGFILE
if [ "$KILL_OLD_LOGS" = true ];then
rm -f $LOGFILE.*
rm -f $LOGFILE-*
fi
LF_Positive_Output_Check
else
LF_Negative_Output_Check
fi
}
# Output to show possible Errors or No-Error-Run
F_Fehlerbehandlung() {
echo -e $NORMAL
if [ "$FEHLER" -eq 0 ]; then
LF_Fehlerbehandlung_Y
if [ "$V_ROOTKIT" = true ]; then
LF_Fehlerbehandlung_RKH
fi
else
LF_Fehlerbehandlung_N
fi
}
# Logline - shows a new Script-Run in the Logfile
F_LOGLINE() {
echo -e $NORMAL" ">>$LOGFILE
echo "=== NEUER SCRIPTLAUF ===">>$LOGFILE
echo -e $NORMAL" ">>$LOGFILE
if [ "$SILENT" = true ]; then
echo "SILENT-Mode=$SILENT">>$LOGFILE
fi
}
### NOTIFICATIONS ###
### Only load Notification Functions when needed ###
if [[ "$UV_PUSHOVER" == false && "$UV_TELEGRAM" == false && "$UV_GOTIFY" == false && "$UV_EMAIL" == false && "$UV_DISCORD" == false && "$UV_TEAMS" == false ]]; then
echo "LFA_Notification_Option_notSet">>$LOGFILE
V_NOTIFY=false
else
source Files/NOTIFICATION_FUNCTIONS.sh
V_NOTIFY=true
fi
### TIME MANAGEMENT ###
# to show the Script-Runtime
F_ZEITAUSGABE() {
end=$(date +%s) # Check End Time
runtime=$((end - start)) # Calc Runtime
log_message -e $NORMAL
if [ "$UV_KEEP_LOG" = false ]; then
LF_Zeitausgabe | tee -a $LOGFILE
else
LF_Zeitausgabe | tee -a $LOGFILE
fi
}
# show script-runtime in Logfile
F_ZEIT_IN_LOG() {
end=$(date +%s)
runtime=$((end - start))
LF_Zeitausgabe_Logfile
}
### LOGROTATION ###
# Logrotation - controlled by Variables (DEB_UPD_config.sh)
F_LOG_ROTATE() {
log_message -n -e $NORMAL "Logfile-Rotation \t \t \t \t "
local max_log_age=$LOGFILE_MAX_AGE
# Check if Logfile exists
if [ -f "$LOGFILE" ]; then
mv "$LOGFILE" "$LOGFILE-$(date +"%Y%m%d-%H%M%S").log"
LF_Positive_Output_Check
else
log_message -e "$NORMAL[$YELLOW!$NORMAL] $LFA_LOGROTATION_Err_1"
fi
# Delete Logfile when X Days old (DEB_UPD_config.sh)
OldLogs=$(find "$(dirname "$LOGFILE")" -name "$(basename "$LOGFILE")-*" -type f -mtime +$max_log_age)
if [[ -n "$OldLogs" ]]; then
find "$(dirname "$LOGFILE")" -name "$(basename "$LOGFILE")-*" -type f -mtime +$max_log_age -exec rm {} \;
LF_LogRotationMaxAge
else
log_message -e "$LFA_NOOLDLOGS"
fi
}
### TXT Functions and Variables for a nicer Scriptrun ###
# Start-Message
T_StartMSG() {
T_LEERZEILE
log_message -e $BLUE $TXTBOLD "Debian-Updater Version: $V_ScriptVersion" $NORMAL
log_message -e $BLUE $TXTBOLD "Check Help (-h / --help) for Options" $NORMAL
T_LEERZEILE
}
# Blank Line
T_LEERZEILE() {
log_message -e $NORMAL
}
### SCRIPTRUN ###
F_LOGLINE
F_Startzeit
T_StartMSG
F_VERSION_ANZEIGEN
T_LEERZEILE
T_LEERZEILE
F_ISROOT
F_UPDATE
F_ANZEIGE
F_UPGRADE
F_AUTOREMOVE
if [ $UV_NC_APP_Update = true ]; then
source Files/Nextcloud.sh
NC_APP_UPDATE
fi
if [ $UV_MW_Update = true ]; then
source Files/MediaWiki.sh
MEDIAWIKI_UPDATE
fi
[ "$UV_RKHUNTER" = true ] && F_ROOTKIT
F_ZEIT_IN_LOG
if [[ "$V_NOTIFY" = true && "$UV_NotifyOnlyOnError" = false ]]; then
F_LOG_TO_VAR
F_PUSHOVER
F_TELEGRAM
send_gotify_message
F_DISCORD
F_EMAIL
F_TEAMS
else
if [[ "$FEHLER" > 0 && "$UV_NotifyOnlyOnError" = true ]]; then
F_LOG_TO_VAR
F_PUSHOVER
F_TELEGRAM
send_gotify_message
F_DISCORD
F_EMAIL
F_TEAMS
fi
#
fi
if [ "$UV_KEEP_LOG" = false ]; then
F_LOGKILL
else
F_LOG_ROTATE
fi
F_Fehlerbehandlung
F_ZEITAUSGABE
T_LEERZEILE