-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.functions
823 lines (739 loc) · 33.3 KB
/
server.functions
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
#!/bin/bash
# minecraft server functions
# this file stores all the functions for the server.
# please note that those function are global and impact every script.
# please notice that editing these functions can be devastating
# if you know what you are doing, feel free to tinker with them ;^)
# prints all input to log at given log level
function Log {
if [[ $1 == "ok" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") ok: ${2}" >>"${3}"
fi
if [[ $1 == "info" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") info: ${2}" >>"${3}"
fi
if [[ $1 == "warn" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") warn: ${2}" >>"${3}"
fi
if [[ $1 == "error" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") error: ${2}" >>"${3}"
fi
if [[ $1 == "fatal" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") fatal: ${2}" >>"${3}"
fi
if [[ $1 == "action" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") action: ${2}" >>"${3}"
fi
}
# prints all input to terminal at given log level
function Print {
if [[ ${1} == "ok" ]] && [[ ${quiet} == false ]]; then
echo "$(date +"%H:%M:%S") ${green}ok${noColor}: ${2}"
fi
if [[ ${1} == "info" ]] && [[ ${quiet} == false ]]; then
echo "$(date +"%H:%M:%S") ${cyan}info${noColor}: ${2}"
fi
if [[ ${1} == "warn" ]]; then
echo "$(date +"%H:%M:%S") ${yellow}warn${noColor}: ${2}"
fi
if [[ ${1} == "error" ]]; then
echo "$(date +"%H:%M:%S") ${red}error${noColor}: ${2}"
fi
if [[ ${1} == "fatal" ]]; then
echo "$(date +"%H:%M:%S") ${red}fatal${noColor}: ${2}"
fi
if [[ ${1} == "action" ]] && [[ ${quiet} == false ]]; then
echo "$(date +"%H:%M:%S") ${blue}action${noColor}: ${2}"
fi
if [[ $1 == "debug" ]] && [[ ${quiet} == false ]]; then
echo "$(date +"%H:%M:%S") debug: ${2}"
fi
}
# checks if debug mode is on
function Debug {
if [[ ${enableDebug} == true ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") ${1}" >>"${debugLog}"
fi
if [[ ${verbose} == true ]]; then
Print "debug" "${1}"
fi
}
# prints all input to screen
function Screen {
if screen -list | grep -q "\.${serverName}"; then
screen -Rd "${serverName}" -X stuff "${1}$(printf '\r')"
fi
}
# function for parsing all arguments for a script
function ParseArgs {
force=false
help=false
now=false
quiet=false
verbose=false
while [[ $# -gt 0 ]]; do
case "${1}" in
-f)
force=true
;;
-h)
help=true
;;
-n)
now=true
;;
-q)
quiet=true
;;
-v)
verbose=true
;;
--force)
force=true
;;
--help)
help=true
;;
--now)
now=true
;;
--quiet)
quiet=true
;;
--verbose)
verbose=true
;;
*)
Print "warn" "bad argument: ${1}"
Print "info" "for help use --help"
;;
esac
shift
done
}
function ParseCategory {
# standart values
isHourly=false
isDaily=false
isWeekly=false
isMonthly=false
# test and set backup category
if [[ $1 == "--hourly" ]]; then
isHourly=true
elif [[ $1 == "--daily" ]]; then
isDaily=true
elif [[ $1 == "--weekly" ]]; then
isWeekly=true
elif [[ $1 == "--monthly" ]]; then
isMonthly=true
else
Print "error" "\"$1\" is not a backup category"
Print "info" "use --hourly, --daily, --weekly, --monthly"
exit 1
fi
}
# prints help and then exits
function ArgHelp {
if [[ ${help} == true ]]; then
Print "info" "available arguments:"
Print "info" "argument explanation"
Print "info" "-f --force (ignores script safety checks)"
Print "info" "-h --help (prints this help page)"
Print "info" "-n --now (executes an action without countdown)"
Print "info" "-q --quiet (silences all output except warnings and errors)"
Print "info" "-v --verbose (prints more verbose and extra debug information)"
exit 0
fi
}
# performs countdown
function Countdown {
if ! [[ ${now} == true ]]; then
counter=60
while [ ${counter} -gt 0 ]; do
if [[ "${counter}" =~ ^(60|40|20|10|5|4|3|2)$ ]]; then
Print "info" "server is ${1} in ${counter} seconds"
TellrawScript "server is ${1} in ${counter} seconds"
fi
if [[ "${counter}" == 1 ]]; then
Print "info" "server is ${1} in ${counter} second"
TellrawScript "server is ${1} in ${counter} second"
fi
counter=$((counter - 1))
sleep 1s
done
fi
}
# root safety check
function RootSafety {
if [ $(id -u) = 0 ]; then
Print "fatal" "please do not run me as root as this is dangerous :("
exit 1
fi
}
# checks if a script is already running
function ScriptSafety {
if [[ ${force} == false ]]; then
declare -a scriptsLock=("reset.sh" "restart.sh" "restore.sh" "start.sh" "stop.sh" "update.sh" "vent.sh")
scriptsLockLength=${#scriptsLock[@]}
for ((i = 0; i < ${scriptsLockLength}; i++)); do
for pid in $(pidof -x ${scriptsLock[i]}); do
if [ $pid != $$ ]; then
Print "warn" "script ${scriptsLock[i]} is already running"
Print "info" "use --force option to ignore this safety check"
exit 1
fi
done
done
fi
}
# checks and coordinates hourly daily weekly and monthly backup
# TODO: write function which prevents backups from happening simultaneous
function BackupSafety {
Print "info" "this function is still in development"
Print "info" "it does not execute anything at the moment"
}
# check for existence of screen terminal
function CheckScreen {
if ! screen -list | grep -q "\.${serverName}"; then
Log "warn" "server is not currently running" "${screenLog}"
Print "warn" "server is not currently running"
exit 1
fi
}
# check for existence of screen terminal
function LookForScreen {
if screen -list | grep -q "\.${serverName}"; then
Log "warn" "server is already running" "${screenLog}"
Print "warn" "server is already running - type screen -r ${serverName} to open server terminal"
exit 1
fi
}
# check for executable
function CheckExecutable {
if ! ls ${executableServerFile}* 1>/dev/null 2>&1; then
Log "fatal" "no executable found" "${screenLog}"
Print "fatal" "no executable found"
exit 1
fi
}
# prints input to screen with script format
function TellrawScript {
if [[ $# -eq 1 ]]; then
Screen "tellraw @a [\"\",{\"text\":\"[Script] \",\"color\":\"blue\"},{\"text\":\"${1}\"}]"
else
Screen "tellraw @a [\"\",{\"text\":\"[Script] \",\"color\":\"blue\"},{\"text\":\"${1}\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"${2}\"}]}}}]"
fi
}
# prints input to screen with backup format and color
function TellrawBackup {
if [[ $# -eq 2 ]]; then
Screen "tellraw @a [\"\",{\"text\":\"[Backup] \",\"color\":\"blue\"},{\"text\":\"${1}\",\"color\":\"${2}\"}]"
else
Screen "tellraw @a [\"\",{\"text\":\"[Backup] \",\"color\":\"blue\"},{\"text\":\"${1}\",\"color\":\"${2}\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"${3}\"}]}}}]"
fi
}
# prints input to screen at given player
function TellrawPlayer {
if [[ $# -eq 2 ]]; then
Screen "tellraw ${1} [\"\",{\"text\":\"[Script] \",\"color\":\"blue\"},{\"text\":\"${2}\"}]"
else
Screen "tellraw ${1} [\"\",{\"text\":\"[Script] \",\"color\":\"blue\"},{\"text\":\"${2} \"},{\"text\":\"${3}\",\"color\":\"${4}\"}]"
fi
}
# prints welcome to screen at every player
function TellrawWelcome {
if [[ $# -eq 2 ]]; then
Screen "tellraw @a [\"\",{\"text\":\"[Welcome] \",\"color\":\"blue\"},{\"text\":\"${1}\"}],{\"text\":\"${2}\",\"color\":\"green\"}]"
else
Screen "tellraw @a [\"\",{\"text\":\"[Welcome] \",\"color\":\"blue\"},{\"text\":\"${1} \"},{\"text\":\"${2}\",\"color\":\"green\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"${3}\"}]}}}]"
fi
}
# checks private network availability
function CheckPrivate {
privateChecks=0
while [ ${privateChecks} -lt 8 ]; do
if ping -c 1 "${private}" &>/dev/null; then
Log "ok" "interface is online" "${screenLog}"
Print "ok" "interface is online"
break
else
Log "warn" "interface is offline" "${screenLog}"
Print "warn" "interface is offline"
fi
if [ ${privateChecks} -eq 7 ]; then
Log "error" "interface timed out" "${screenLog}"
Print "error" "interface timed out"
fi
sleep 1s
privateChecks=$((privateChecks + 1))
done
}
# checks public network availability
function CheckPublic {
publicChecks=0
while [ ${publicChecks} -lt 8 ]; do
if ping -c 1 "${public}" &>/dev/null; then
Log "ok" "nameserver is online" "${screenLog}"
Print "ok" "nameserver is online"
break
else
Log "warn" "nameserver is offline" "${screenLog}"
Print "warn" "nameserver is offline"
fi
if [ ${publicChecks} -eq 7 ]; then
Log "error" "nameserver timed out" "${screenLog}"
Print "error" "nameserver timed out"
fi
sleep 1s
publicChecks=$((publicChecks + 1))
done
}
# performs server start
function Start {
Print "action" "starting server..."
screen -dmSL "${serverName}" -Logfile "${screenLog}" java -server "${javaArgs}" -jar "${executableServerFile}" -nogui
screen -r "${serverName}" -X colon "logfile flush 1^M"
}
# awaits server start
function AwaitStart {
startChecks=0
while [ ${startChecks} -lt 10 ]; do
if screen -list | grep -q "\.${serverName}"; then
break
fi
startChecks=$((startChecks + 1))
sleep 1s
done
}
# performs server stop
function Stop {
Print "action" "stopping server..."
TellrawScript "stopping server..."
sleep 1s
Screen "stop"
}
# awaits server stop
function AwaitStop {
stopChecks=0
while [ ${stopChecks} -lt 20 ]; do
if ! screen -list | grep -q "\.${serverName}"; then
break
fi
stopChecks=$((stopChecks + 1))
sleep 1s
done
}
# awaits given string in screen
function AwaitString {
stringChecks=0
while [ ${stringChecks} -lt ${2} ]; do
if tail -1 "${screenLog}" | grep -q "${1}"; then
break
fi
stringChecks=$((stringChecks + 1))
sleep 1s
done
}
# conditional force quit
function ForceQuit {
if screen -list | grep -q "${serverName}"; then
Log "warn" "${serverName} server still hasn't closed after 30 seconds, closing screen manually..." "${screenLog}"
Print "warn" "${serverName} server still hasn't closed after 30 seconds, closing screen manually..."
screen -S "${serverName}" -X quit
fi
}
# prints game over as pixel art on terminal
function GameOver {
echo "${red} ______ _____ ${noColor}"
echo "${red} / _____) / ___ \ ${noColor}"
echo "${red} | / ___ ____ ____ ____ | | | |_ _ ____ ____ ${noColor}"
echo "${red} | | (___)/ _ | \ / _ ) | | | | | | / _ )/ ___) ${noColor}"
echo "${red} | \____/( ( | | | | ( (/ / | |___| |\ V ( (/ /| | ${noColor}"
echo "${red} \_____/ \_||_|_|_|_|\____) \_____/ \_/ \____)_| ${noColor}"
echo "${red} ${noColor}"
}
# declare all scripts in an array
declare -a scripts=("start.sh" "restore.sh" "reset.sh" "restart.sh" "stop.sh" "backup.sh" "update.sh" "worker.sh" "vent.sh")
# get length of script array
scriptsLength=${#scripts[@]}
# function for removing scripts from serverdirectory
function RemoveScripts {
# remove scripts from serverdirectory
# loop through all entries in the array
Print "info" "removing scripts..."
for ((i = 0; i < ${scriptsLength}; i++)); do
Debug "removing script ${scripts[${i}]}"
rm "${scripts[${i}]}"
done
}
# function for downloading scripts from github
function DownloadScripts {
# downloading scripts from github
# loop through all entries in the array
Print "info" "downloading scripts..."
for ((i = 0; i < ${scriptsLength}; i++)); do
Debug "downloading script ${scripts[${i}]} from branch ${branch} on github..."
wget -q -O "${scripts[${i}]}" "https://raw.githubusercontent.com/simylein/minecraft-server/${branch}/${scripts[${i}]}"
done
}
# function for making scripts executable
function ExecutableScripts {
# make selected scripts executable
# loop through all entries in the array
Print "info" "making scripts executable..."
for ((i = 0; i < ${scriptsLength}; i++)); do
Debug "setting script ${scripts[${i}]} executable"
chmod +x ${scripts[${i}]}
done
}
# change to server directory with error checking
function ChangeServerDirectory {
if [ -d "${serverDirectory}" ]; then
cd ${serverDirectory}
else
Log "fatal" "server-directory is missing - path: ${serverDirectory}" "${screenLog}"
Print "fatal" "server-directory is missing - path: ${serverDirectory}"
exit 1
fi
}
# change to backup directory with error checking
function ChangeBackupDirectory {
if [ -d "${backupDirectory}" ]; then
cd ${backupDirectory}
else
Log "fatal" "backup-directory is missing - path: ${backupDirectory}" "${screenLog}"
Print "fatal" "backup-directory is missing - path: ${backupDirectory}"
exit 1
fi
}
# prints success messages to terminal screen and backup log
function OutputBackupSuccess {
Log "ok" "added ${backupDirectory}/${1}/${serverName}-${2}.tar.gz" "${backupLog}"
Log "ok" "removed ${backupDirectory}/${1}/${serverName}-${3}.tar.gz" "${backupLog}"
Log "info" "current world size: ${worldSizeHuman}, current backup size: ${backupSizeHuman}, current disk space: ${diskSpaceHuman}" "${backupLog}"
Log "info" "time spent on backup process: ${timeSpent} milliseconds, compression ratio: ${compressedBackupSize}/${worldSizeHuman}" "${backupLog}"
Print "ok" "added ${backupDirectory}/${1}/${serverName}-${2}.tar.gz"
Print "ok" "removed ${backupDirectory}/${1}/${serverName}-${3}.tar.gz"
Print "info" "current world size: ${worldSizeHuman}, current backup size: ${backupSizeHuman}, current disk space: ${diskSpaceHuman}"
Print "info" "time spent on backup process: ${timeSpent} milliseconds, compression ratio: ${compressedBackupSize}/${worldSizeHuman}"
Debug "backup script reports backup success while performing backup-${1}"
TellrawBackup "ok: successfully created new backup" "green" "created file: ${serverName}-${2}.tar.gz, removed file: ${serverName}-${3}.tar.gz, current world size: ${worldSizeHuman}, current backup size: ${backupSizeHuman}, current disk space: ${diskSpaceHuman}, time spent: ${timeSpent} ms, compression: ${compressedBackupSize}/${worldSizeHuman}"
}
# prints disk space warning to terminal screen and backup log
function OutputDiskSpaceWarning {
Log "warn" "free disk-space is getting rare - make some room for backups" "${backupLog}"
Log "info" "available disk space ${diskSpaceHuman} - warning at: ${diskSpaceWarning} bytes, limit at: ${diskSpaceError} bytes" "${backupLog}"
Print "warn" "free disk-space is getting rare - make some room for backups"
Print "info" "available disk space ${diskSpaceHuman} - warning at: ${diskSpaceWarning} bytes, limit at: ${diskSpaceError} bytes"
Debug "backup script reports low disk-space while performing backup-${1}"
TellrawBackup "warn: free disk space is getting rare - please tell your server admin" "yellow" "current world size: ${worldSizeHuman}, current backup size: ${backupSizeHuman}, current disk space: ${diskSpaceHuman}, warning: free disk space is getting rare"
}
# prints backup size warning to terminal screen and backup log
function OutputBackupSizeWarning {
Log "warn" "the created ${1}-backup is only ${compressedBackupSize} - this may point to a corrupted backup" "${backupLog}"
Log "info" "you may change the control variables in the server.settings file - warning at: ${backupSizeWarning} percent, error at: ${backupSizeError} percent"
Print "warn" "the created ${1}-backup is only ${compressedBackupSize} - this may point to a corrupted backup"
Print "info" "you may change the control variables in the server.settings file - warning at: ${backupSizeWarning} percent, error at: ${backupSizeError} percent"
Debug "backup script reports potentially corrupted backup at ${backupDirectory}/${1}/${serverName}-${2}"
TellrawBackup "warn: the created backup is unusally small - please tell your server admin" "yellow" "current world size: ${worldSizeHuman}, created backup size: ${compressedBackupSize}, warning: backup unusally small - this may point to corruption"
}
# prints disk space error to terminal screen and backup log
function OutputDiskSpaceError {
Log "error" "not enough disk-space to perform backup-${1}" "${backupLog}"
Log "info" "available disk space ${diskSpaceHuman} - warning at: ${diskSpaceWarning} bytes, limit at: ${diskSpaceError} bytes" "${backupLog}"
Print "error" "not enough disk-space to perform backup-${1}"
Print "info" "available disk space ${diskSpaceHuman} - warning at: ${diskSpaceWarning} bytes, limit at: ${diskSpaceError} bytes"
Debug "backup script reports not enough disk-space while performing backup-${1}"
TellrawBackup "fatal: not enough disk space - please immediately tell your server admin" "red" "could not create file: ${serverName}-${2}.tar.gz, could not remove file: ${serverName}-${3}.tar.gz, reason: not enough disk-space"
}
# prints backup size error to terminal screen and backup log
function OutputBackupSizeError {
Log "error" "the created ${1}-backup is only ${compressedBackupSize} - this will point to a corrupted backup" "${backupLog}"
Log "info" "you may change the control variables in the server.settings file - warning at: ${backupSizeWarning} percent, error at: ${backupSizeError} percent"
Print "error" "the created ${1}-backup is only ${compressedBackupSize} - this will point to a corrupted backup"
Print "info" "you may change the control variables in the server.settings file - warning at: ${backupSizeWarning} percent, error at: ${backupSizeError} percent"
Debug "backup script reports dangerous corrupted backup at ${backupDirectory}/${1}/${serverName}-${2}"
TellrawBackup "error: the created backup is too small - please tell your server admin" "red" "current world size: ${worldSizeHuman}, created backup size: ${compressedBackupSize}, reason: backup too small - this points to corruption"
}
# prints backup already exists messages to terminal screen and backup log
function OutputBackupAlreadyExists {
Log "error" "could not create new ${1}-backup - backup already exists" "${backupLog}"
Log "info" "the file ${backupDirectory}/${1}/${serverName}-${2} already exists"
Print "error" "could not create new ${1}-backup - backup already exists"
Print "info" "the file ${backupDirectory}/${1}/${serverName}-${2} already exists"
Debug "backup script reports backup already exists while performing backup-${1}"
TellrawBackup "error: backup already exists - please tell your server admin" "red" "could not create file: ${serverName}-${2}.tar.gz, could not remove file: ${serverName}-${3}.tar.gz, reason: backup already exists"
}
# prints tar backup error messages to terminal screen and backup log
function OutputBackupTarError {
Log "error" "tar reports errors during compression of world directory" "${backupLog}"
Print "error" "tar reports errors during compression of world directory"
Debug "backup script reports tar error while performing backup-${1}"
TellrawBackup "fatal: could not create new backup - please immediately tell your server admin" "red" "could not create file: ${serverName}-${2}.tar.gz, could not remove file: ${serverName}-${3}.tar.gz, reason: tar reports errors during compression of world directory"
}
# prints copy backup error messages to terminal screen and backup log
function OutputBackupCopyError {
Log "error" "copy reports errors during copying of world directory" "${backupLog}"
Print "error" "copy reports errors during copying of world directory"
Debug "backup script reports copy error while performing backup-${1}"
TellrawBackup "fatal: could not create new backup - please immediately tell your server admin" "red" "could not create file: ${serverName}-${2}.tar.gz, could not remove file: ${serverName}-${3}.tar.gz, reason: copy reports errors during copying of world directory"
}
# prints generic backup error messages to terminal screen and backup log
function OutputBackupGenericError {
Log "error" "could not backup world" "${backupLog}"
Print "error" "could not backup world"
Debug "backup script reports generic backup error while performing backup-${1}"
TellrawBackup "fatal: could not create new backup - please immediately tell your server admin" "red" "could not create file: ${serverName}-${2}.tar.gz, could not remove file: ${serverName}-${3}.tar.gz, reason: generic error - missing directories, empty file-paths or empty files"
}
# function for testing if all categories for backups exists if not create them also check for root backups directory
function BackupDirectoryIntegrity {
cd ${serverDirectory}
# check for root backup directory and create if missing
if ! ls ${backupDirectory} &>/dev/null; then
Log "error" "the root-backupdirectory is missing - your backups are likely gone :(" "${backupLog}"
Log "info" "creating a new root-backupdirectory with name backups at ${serverDirectory}" "${backupLog}"
Print "error" "the root-backupdirectory is missing - your backups are likely gone :("
Print "info" "creating a new root-backupdirectory with name backups at ${serverDirectory}"
Screen "tellraw @a [\"\",{\"text\":\"[Backup] \",\"color\":\"blue\"},{\"text\":\"error: the root-backupdirectory is missing - your backups are likely gone :(\",\"color\":\"red\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"reason: backup directory integrity check reports that the root backup directory is missing - the root backup directory has been recreated\"}]}}}]"
mkdir backups
fi
declare -a backupCategories=("cached" "hourly" "daily" "weekly" "monthly")
arrayLenght=${#backupCategories[@]}
for ((i = 0; i < ${arrayLenght}; i++)); do
# check for backup category directories and create if missing
if ! ls ${backupDirectory}/${backupCategories[${i}]} &>/dev/null; then
Log "warn" "the backup-directory ${backupCategories[${i}]} is missing" "${backupLog}"
Log "info" "creating ${backupDirectory}/${backupCategories[${i}]}" "${backupLog}"
Print "warn" "the backup-directory ${backupCategories[${i}]} is missing"
Print "info" "creating ${backupDirectory}/${backupCategories[${i}]}"
Screen "tellraw @a [\"\",{\"text\":\"[Backup] \",\"color\":\"blue\"},{\"text\":\"warn: the ${backupCategories[${i}]} backup category is missing - your ${backupCategories[${i}]} backups are likely gone :/\",\"color\":\"yellow\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"reason: backup directory integrity check reports that the ${backupCategories[${i}]} backup directory is missing - the ${backupCategories[${i}]} backup directory has been recreated\"}]}}}]"
cd ${backupDirectory}
mkdir ${backupCategories[${i}]}
cd ${serverDirectory}
fi
done
}
# function for creating a cached backup with name as input
function CachedBackup {
# remove all older cached backups
if [[ -s "${backupDirectory}/cached/${1}-"* ]]; then
rm "${backupDirectory}/cached/${1}-"*
fi
# user info about backup to create
Print "action" "creating cached ${1} backup..."
# create backup with given name
nice -n 19 cp -r "world" "tmp-${1}"
nice -n 19 tar -czf "world.tar.gz" "tmp-${1}"
nice -n 19 mv "${serverDirectory}/world.tar.gz" "${backupDirectory}/cached/${1}-${newDaily}-${newHourly}.tar.gz"
nice -n 19 rm -r "tmp-${1}"
# check if safety backup exists
if [[ -s "${backupDirectory}/cached/${1}-${newDaily}-${newHourly}.tar.gz" ]]; then
Log "ok" "created ${backupDirectory}/cached/${1}-${newDaily}-${newHourly}.tar.gz as a ${1} backup" "${backupLog}"
Print "ok" "${1} backup successful"
TellrawBackup "ok: backup successful" "green" "created ${backupDirectory}/cached/${1}-${newDaily}-${newHourly}.tar.gz"
else
Log "warn" "${1} backup failed" "${backupLog}"
Print "warn" "${1} backup failed"
TellrawBackup "warn: backup failed" "yellow" "failed to create ${backupDirectory}/cached/${1}-${newDaily}-${newHourly}.tar.gz"
fi
}
# check for help string
function Help {
if tail -1 "${screenLog}" | grep -q "help"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=help)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
Log "info" "the player ${player} requested help - server will print help info and admin contact" "${screenLog}"
TellrawPlayer "${player}" "info: available commands: (seperated by comma)"
TellrawPlayer "${player}" "info: list tasks, list backups"
TellrawPlayer "${player}" "info: perform backup, perform restart"
TellrawPlayer "${player}" "info: perform update, perform reset"
TellrawPlayer "${player}" "info: admin contact info: ${adminContact}"
fi
}
# check for list tasks string
function ListTasks {
if tail -1 "${screenLog}" | grep -q "list tasks"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=list tasks)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested list of all tasks and has permission - server will list all tasks" "${screenLog}"
TellrawPlayer "${player}" "info: you successfully requested all available tasks of the server"
# run list tasks
if [[ ${enablePerformBackup} == true ]]; then
TellrawPlayer "${player}" "perform backup is" "enabled" "green"
elif [[ "${enablePerformBackup}" == false ]]; then
TellrawPlayer "${player}" "perform backup is" "disabled" "red"
else
TellrawPlayer "${player}" "perform backup is" "undefined" "grey"
fi
if [[ ${enablePerformRestart} == true ]]; then
TellrawPlayer "${player}" "perform restart is" "enabled" "green"
elif [[ "${enablePerformRestart}" == false ]]; then
TellrawPlayer "${player}" "perform restart is" "disabled" "red"
else
TellrawPlayer "${player}" "perform restart is" "undefined" "grey"
fi
if [[ ${enablePerformUpdate} == true ]]; then
TellrawPlayer "${player}" "perform update is" "enabled" "green"
elif [[ ${enablePerformUpdate} == false ]]; then
TellrawPlayer "${player}" "perform update is" "disabled" "red"
else
TellrawPlayer "${player}" "perform update is" "undefined" "grey"
fi
if [[ ${enablePerformReset} == true ]]; then
TellrawPlayer "${player}" "perform reset is" "enabled" "green"
elif [[ ${enablePerformReset} == false ]]; then
TellrawPlayer "${player}" "perform reset is" "disabled" "red"
else
TellrawPlayer "${player}" "perform reset is" "undefined" "grey"
fi
TellrawPlayer "${player}" "config is located in file server.settings"
else
Log "warn" "the player ${player} requested a list of tasks and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "warn: you do not have permissions to list all available tasks of the server"
fi
fi
}
# check for list backups string
function ListBackups {
if tail -1 "${screenLog}" | grep -q "list backups"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=list backups)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested list of all backups and has permission - server will list all backups" "${screenLog}"
TellrawPlayer "${player}" "info: you successfully requested all available backups of the server"
# run list backups
if [[ ${doHourly} == true ]]; then
TellrawPlayer "${player}" "hourly backup is" "enabled" "green"
elif [[ "${doHourly}" == false ]]; then
TellrawPlayer "${player}" "hourly backup is" "disabled" "red"
else
TellrawPlayer "${player}" "hourly backup is" "undefined" "grey"
fi
if [[ ${doDaily} == true ]]; then
TellrawPlayer "${player}" "daily backup is" "enabled" "green"
elif [[ "${doDaily}" == false ]]; then
TellrawPlayer "${player}" "daily backup is" "disabled" "red"
else
TellrawPlayer "${player}" "daily backup is" "undefined" "grey"
fi
if [[ ${doWeekly} == true ]]; then
TellrawPlayer "${player}" "weekly backup is" "enabled" "green"
elif [[ ${doWeekly} == false ]]; then
TellrawPlayer "${player}" "weekly backup is" "disabled" "red"
else
TellrawPlayer "${player}" "weekly backup is" "undefined" "grey"
fi
if [[ ${doMonthly} == true ]]; then
TellrawPlayer "${player}" "monthly backup is" "enabled" "green"
elif [[ ${doMonthly} == false ]]; then
TellrawPlayer "${player}" "monthly backup is" "disabled" "red"
else
TellrawPlayer "${player}" "monthly backup is" "undefined" "grey"
fi
TellrawPlayer "${player}" "config is located in file server.settings"
else
Log "warn" "the player ${player} requested a list of backups and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "warn: you do not have permissions to list all available backups of the server"
fi
fi
}
# check for perform backup string
function PerformBackup {
if tail -1 "${screenLog}" | grep -q "perform backup"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=perform backup)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested a safety backup and has permission - server will perform safety backup" "${backupLog}"
TellrawPlayer "${player}" "info: you successfully requested a safety backup of the server"
# run safety backup
CachedBackup "safety"
else
Log "warn" "the player ${player} requested a safety backup and does not have permission to do so" "${backupLog}"
TellrawPlayer "${player}" "warn: you do not have permissions to safety backup the server"
fi
fi
}
# check for perform restart strings
function PerformRestart {
# check for perform restart now string
if tail -1 "${screenLog}" | grep -q "perform restart now"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=perform restart now)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested a restart and has permission - server will restart" "${screenLog}"
TellrawPlayer "${player}" "info: you successfully requested a restart of the server"
./restart.sh --quiet --now
exit 0
else
Log "warn" "the player ${player} requested a restart and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "warn: you do not have permissions to restart the server"
fi
fi
# check for perform restart string
if tail -1 "${screenLog}" | grep -q "perform restart"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=perform restart)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested a restart and has permission - server will restart" "${screenLog}"
TellrawPlayer "${player}" "info: you successfully requested a restart of the server"
./restart.sh --quiet
exit 0
else
Log "warn" "the player ${player} requested a restart and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "warn: you do not have permissions to restart the server"
fi
fi
}
# check for perform update strings
function PerformUpdate {
# check for perform update now string
if tail -1 "${screenLog}" | grep -q "perform update now"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=perform update now)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested an update and has permission - server will update" "${screenLog}"
TellrawPlayer "${player}" "you successfully requested an update of the server"
./update.sh --quiet --now
exit 0
else
Log "warn" "the player ${player} requested an update and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "you do not have permissions to update the server"
fi
fi
# check for perform update string
if tail -1 "${screenLog}" | grep -q "perform update"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=perform update)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested an update and has permission - server will update" "${screenLog}"
TellrawPlayer "${player}" "you successfully requested an update of the server"
./update.sh --quiet
exit 0
else
Log "warn" "the player ${player} requested an update and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "you do not have permissions to update the server"
fi
fi
}
# check for perform reset strings
function PerformReset {
# check for perform reset now string
if tail -1 "${screenLog}" | grep -q "perform reset now"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=perform reset now)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested a reset and has permission - server will reset" "${screenLog}"
TellrawPlayer "${player}" "info: you successfully requested a reset of the server"
./reset.sh --quiet --now
exit 0
else
Log "warn" "the player ${player} requested a reset and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "warn: you do not have permissions to reset the server"
fi
fi
# check for perform reset string
if tail -1 "${screenLog}" | grep -q "perform reset"; then
player=$(tail -1 screen.log | grep -oP '.*?(?=perform reset)' | cut -d ' ' -f 4- | sed 's/.$//' | rev | sed 's/.$//' | rev | sed 's/.$//')
if cat "ops.json" | grep -q "${player}"; then
Log "info" "the player ${player} requested a reset and has permission - server will reset" "${screenLog}"
TellrawPlayer "${player}" "info: you successfully requested a reset of the server"
./reset.sh --quiet
exit 0
else
Log "warn" "the player ${player} requested a reset and does not have permission to do so" "${screenLog}"
TellrawPlayer "${player}" "warn: you do not have permissions to reset the server"
fi
fi
}