-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathausrv-script.bash
More file actions
1295 lines (1165 loc) · 64.4 KB
/
ausrv-script.bash
File metadata and controls
1295 lines (1165 loc) · 64.4 KB
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright (C) 2022 7thCore
# This file is part of AuSrv-Script.
#
# AuSrv-Script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AuSrv-Script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#Among Us Impostor server script by 7thCore
#If you do not know what any of these settings are you are better off leaving them alone. One thing might brake the other if you fiddle around with it.
#Basics
export NAME="AuSrv" #Name of the tmux session
export VERSION="1.1-2" #Package and script version
export SERVICE_NAME="ausrv" #Name of the service files, user, script and script log
export LOG_DIR="/srv/$SERVICE_NAME/logs" #Location of the script's log files.
export LOG_STRUCTURE="$LOG_DIR/$(date +"%Y")/$(date +"%m")/$(date +"%d")" #Folder structure of the script's log files.
export LOG_SCRIPT="$LOG_STRUCTURE/$SERVICE_NAME-script.log" #Script log.
SRV_DIR="/srv/$SERVICE_NAME/server" #Location of the server located on your hdd/ssd.
CONFIG_DIR="/srv/$SERVICE_NAME/config" #Location of this script's configuration.
BCKP_DIR="/srv/$SERVICE_NAME/backups" #Location of stored backups.
BCKP_STRUCTURE="$(date +"%Y")/$(date +"%m")/$(date +"%d")" #How backups are sorted, by default it's sorted in folders by month and day.
#Script config file variables
BCKP_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf | grep script_bckp_delold= | cut -d = -f2) #Defines how many days old backups are deleted.
LOG_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf | grep script_log_delold= | cut -d = -f2) #Defines how many days old logs are deleted.
#Script config variables if config doesn't exist
BCKP_DELOLD=${BCKP_DELOLD:="7"} #If the variable for old backup deletion is not defined, assign a default value.
LOG_DELOLD=${LOG_DELOLD:="7"} #If the variable for old log deletion is not defined, assign a default value.
#Discord config file variables
DISCORD_START=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_start= | cut -d = -f2) #Send notifications when the server starts.
DISCORD_STOP=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_stop= | cut -d = -f2) #Send notifications when the server stops.
DISCORD_CRASH=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_crash= | cut -d = -f2) #Send notifications when the server crashes.
DISCORD_COLOR_PRESTART=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_prestart= | cut -d = -f2) #Discord embed color for prestart.
DISCORD_COLOR_POSTSTART=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_poststart= | cut -d = -f2) #Discord embed color for poststart.
DISCORD_COLOR_PRESTOP=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_prestop= | cut -d = -f2) #Discord embed color for prestop.
DISCORD_COLOR_POSTSTOP=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_poststop= | cut -d = -f2) #Discord embed color for poststop.
DISCORD_COLOR_CRASH=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_crash= | cut -d = -f2) #Discord embed color for crash.
#Discord config variables if config doesn't exist
DISCORD_START=${DISCORD_START:="0"} #If the variable for discord start is not defined, assign a default value.
DISCORD_STOP=${DISCORD_STOP:="0"} #If the variable for discord stop is not defined, assign a default value.
DISCORD_CRASH=${DISCORD_CRASH:="0"} #If the variable for discord crash is not defined, assign a default value.
DISCORD_COLOR_PRESTART=${DISCORD_COLOR_PRESTART:="16776960"} #If the variable discord pre-start color is not defined, assign a default value.
DISCORD_COLOR_POSTSTART=${DISCORD_COLOR_POSTSTART:="65280"} #If the variable discord post-start color is not defined, assign a default value.
DISCORD_COLOR_PRESTOP=${DISCORD_COLOR_PRESTOP:="16776960"} #If the variable discord pre-stop color is not defined, assign a default value.
DISCORD_COLOR_POSTSTOP=${DISCORD_COLOR_POSTSTOP:="65280"} #If the variable discord post-stop color is not defined, assign a default value.
DISCORD_COLOR_CRASH=${DISCORD_COLOR_CRASH:="16711680"} #If the variable for discord crash color is not defined, assign a default value.
#Email config file variables
EMAIL_SENDER=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_sender= | cut -d = -f2) #Send emails from this address.
EMAIL_RECIPIENT=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_recipient= | cut -d = -f2) #Send emails to this address.
EMAIL_START=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_start= | cut -d = -f2) #Send emails when the server starts up.
EMAIL_STOP=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_stop= | cut -d = -f2) #Send emails when the server shuts down.
EMAIL_CRASH=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_crash= | cut -d = -f2) #Send emails when the server crashes.
#Email config variables if config doesn't exist
EMAIL_SENDER=${EMAIL_SENDER:="none"} #If the variable for email sender is not defined, assign a default value.
EMAIL_RECIPIENT=${EMAIL_RECIPIENT:="none"} #If the variable for email recipient is not defined, assign a default value.
EMAIL_START=${EMAIL_START:="0"} #If the variable for email start is not defined, assign a default value.
EMAIL_STOP=${EMAIL_STOP:="0"} #If the variable for email stop is not defined, assign a default value.
EMAIL_CRASH=${EMAIL_CRASH:="0"} #If the variable for email crash is not defined, assign a default value.
#Console collors
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
LIGHTRED='\033[1;31m'
NC='\033[0m'
#--------------------------
#-- End of configuration --
#--------------------------
#Generate log folder structure
script_logs() {
#If there is not a folder for today, create one
if [ ! -d "$LOG_STRUCTURE" ]; then
mkdir -p $LOG_STRUCTURE
fi
}
#--------------------------
#Discord webhook message send
script_discord_message() {
while IFS="" read -r DISCORD_WEBHOOK || [ -n "$DISCORD_WEBHOOK" ]; do
curl -H "Content-Type: application/json" -X POST -d "{\"embeds\": [{ \"author\": { \"name\": \"$NAME Script\", \"url\": \"https://github.com/7thCore/$SERVICE_NAME-script\" }, \"color\": \"$1\", \"description\": \"$2\", \"footer\": {\"text\": \"Version $VERSION\"}, \"timestamp\": \"$(date -u --iso-8601=seconds)\"}] }" "$DISCORD_WEBHOOK"
done < $CONFIG_DIR/discord_webhooks.txt
}
#--------------------------
#Send email message
script_email_message() {
mail -r "$EMAIL_SENDER ($1)" -s "$2" $EMAIL_RECIPIENT <<- EOF
$3
EOF
}
#--------------------------
#Attaches to the server tmux session
script_attach() {
script_logs
if [ -z "$1" ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Attach) Failed to attach. Specify server ID: $SERVICE_NAME-script -attach ID" | tee -a "$LOG_SCRIPT"
else
tmux -L $SERVICE_NAME-$1-tmux.sock has-session -t $NAME 2>/dev/null
if [ $? == 0 ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Attach) User attached to server session with ID: $1" | tee -a "$LOG_SCRIPT"
tmux -L $SERVICE_NAME-$1-tmux.sock attach -t $NAME
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Attach) User deattached from server session with ID: $1" | tee -a "$LOG_SCRIPT"
else
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Attach) Failed to attach to server session with ID: $1" | tee -a "$LOG_SCRIPT"
fi
fi
}
#--------------------------
#Deletes old files
script_remove_old_files() {
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Beginning removal of old files." | tee -a "$LOG_SCRIPT"
#Delete old logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removing old script logs: $LOG_DELOLD days old." | tee -a "$LOG_SCRIPT"
find $LOG_DIR/* -mtime +$LOG_DELOLD -delete
#Delete empty folders
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removing empty script log folders." | tee -a "$LOG_SCRIPT"
find $LOG_DIR/ -type d -empty -delete
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Deleting old backups for $SERVER_INSTANCE: $BCKP_DELOLD days old." | tee -a "$LOG_SCRIPT"
# Delete old backups
find $BCKP_DIR/$SERVER_INSTANCE/* -type f -mtime +$BCKP_DELOLD -delete
# Delete empty folders
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Deleting empty backup folders for $SERVER_INSTANCE." | tee -a "$LOG_SCRIPT"
find $BCKP_DIR/$SERVER_INSTANCE/ -type d -empty -delete
fi
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removal of old files complete." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Prints out if the server is running
script_status() {
script_logs
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_INSTANCE is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_INSTANCE is running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_INSTANCE is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "activating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_INSTANCE is activating. Please wait." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "deactivating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_INSTANCE is in deactivating. Please wait." | tee -a "$LOG_SCRIPT"
fi
done
if pidof -x "$SCRIPT_PID_CHECK" -o $$ > /dev/null; then
echo "Is another instance of the script running?: YES"
else
echo "Is another instance of the script running?: NO"
fi
}
#--------------------------
#Adds a server instance to the server list file
script_add_server() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Add server instance) User adding new server instance." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to add a server instance? (y/n): " ADD_SERVER_INSTANCE
if [[ "$ADD_SERVER_INSTANCE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo ""
echo "List of current servers (your new server instance must NOT be identical to any of them!):"
if [ ! -f $CONFIG_DIR/$SERVICE_NAME-server-list.txt ] ; then
touch $CONFIG_DIR/$SERVICE_NAME-server-list.txt
fi
cat $CONFIG_DIR/$SERVICE_NAME-server-list.txt
echo ""
read -p "Specify your server instance (Can be a number or string. Eamples: 01, vanilla or moded_something): " SERVER_INSTANCE
echo "$SERVICE_NAME@$SERVER_INSTANCE.service" >> $CONFIG_DIR/$SERVICE_NAME-server-list.txt
systemctl --user enable $SERVICE_NAME@$SERVER_INSTANCE.service
mkdir $SRV_DIR/$SERVER_INSTANCE
mkdir $SRV_DIR/$SERVER_INSTANCE/{libraries,plugins}
touch $SRV_DIR/$SERVER_INSTANCE/config.json
cat >> $SRV_DIR/$SERVER_INSTANCE/config.json <<- EOF
{
"Server": {
"PublicIp": "0.0.0.0",
"PublicPort": 0,
"ListenIp": "0.0.0.0",
"ListenPort": 0
},
"AntiCheat": {
"Enabled": false,
"BanIpFromGame": false
}
}
EOF
echo ""
echo "Don't forget to edit the config.json file located in the $SRV_DIR/$SERVER_INSTANCE folder."
read -p "Server instance $SERVER_INSTANCE added successfully. Do you want to start it? (y/n): " START_SERVER_INSTANCE
if [[ "$START_SERVER_INSTANCE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
systemctl --user start $SERVICE_NAME@$SERVER_INSTANCE.service
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Add server instance) Server instance $SERVER_INSTANCE successfully added." | tee -a "$LOG_SCRIPT"
else
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Add server instance) User canceled adding new server instance." | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
#Removes a server instance from the server list file
script_remove_server() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove server instance) User started removal of server instance." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to remove a server instance? (y/n): " REMOVE_SERVER_INSTANCE
if [[ "$REMOVE_SERVER_INSTANCE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo ""
echo "List of current servers:"
cat $CONFIG_DIR/$SERVICE_NAME-server-list.txt
echo ""
read -p "Specify your server instance (Can be a number or string. Eamples: 01, vanilla or moded_something): " SERVER_INSTANCE
sed -e "s/$SERVICE_NAME@$SERVER_INSTANCE.service//g" -i $CONFIG_DIR/$SERVICE_NAME-server-list.txt
sed '/^$/d' -i $CONFIG_DIR/$SERVICE_NAME-server-list.txt
systemctl --user disable $SERVICE_NAME@$SERVER_INSTANCE.service
echo ""
read -p "Server instance $SERVER_INSTANCE removed successfully. Do you want to stop it? (y/n): " STOP_SERVER_INSTANCE
if [[ "$STOP_SERVER_INSTANCE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
systemctl --user stop $SERVICE_NAME@$SERVER_INSTANCE.service
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove server instance) Server instance $SERVER_INSTANCE successfully removed." | tee -a "$LOG_SCRIPT"
else
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove server instance) User canceled removal of server instance." | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
#Disable all script services
script_disable_services() {
script_logs
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
if [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
systemctl --user disable $SERVER_SERVICE
fi
done
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-1.timer)" == "enabled" ]]; then
systemctl --user disable $SERVICE_NAME-timer-1.timer
fi
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-2.timer)" == "enabled" ]]; then
systemctl --user disable $SERVICE_NAME-timer-2.timer
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Disable services) Services successfully disabled." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Disables all script services, available to the user
script_disable_services_manual() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Disable services) WARNING: This will disable all script services. The server will be disabled." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to disable all services? (y/n): " DISABLE_SCRIPT_SERVICES
if [[ "$DISABLE_SCRIPT_SERVICES" =~ ^([yY][eE][sS]|[yY])$ ]]; then
script_disable_services
elif [[ "$DISABLE_SCRIPT_SERVICES" =~ ^([nN][oO]|[nN])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Disable services) Disable services canceled." | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
# Enable script services by reading the configuration file
script_enable_services() {
script_logs
IFS=","
for SERVER_SERVICE in $(cat $CONFIG_DIR/$SERVICE_NAME-server-list.txt | tr "\\n" "," | sed 's/,$//'); do
if [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "disabled" ]]; then
systemctl --user enable $SERVER_SERVICE
fi
done
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-1.timer)" == "disabled" ]]; then
systemctl --user enable $SERVICE_NAME-timer-1.timer
fi
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-timer-2.timer)" == "disabled" ]]; then
systemctl --user enable $SERVICE_NAME-timer-2.timer
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Enable services) Services successfully Enabled." | tee -a "$LOG_SCRIPT"
}
#--------------------------
# Enable script services by reading the configuration file, available to the user
script_enable_services_manual() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Enable services) This will enable all script services. All added servers will be enabled." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to enable all services? (y/n): " ENABLE_SCRIPT_SERVICES
if [[ "$ENABLE_SCRIPT_SERVICES" =~ ^([yY][eE][sS]|[yY])$ ]]; then
script_enable_services
elif [[ "$ENABLE_SCRIPT_SERVICES" =~ ^([nN][oO]|[nN])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Enable services) Enable services canceled." | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
#Disables all script services an re-enables them by reading the configuration file
script_reload_services() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Reload services) This will reload all script services." | tee -a "$LOG_SCRIPT"
read -p "Are you sure you want to reload all services? (y/n): " RELOAD_SCRIPT_SERVICES
if [[ "$RELOAD_SCRIPT_SERVICES" =~ ^([yY][eE][sS]|[yY])$ ]]; then
script_disable_services
systemctl --user daemon-reload
script_enable_services
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Reload services) Reload services complete." | tee -a "$LOG_SCRIPT"
elif [[ "$RELOAD_SCRIPT_SERVICES" =~ ^([nN][oO]|[nN])$ ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Reload services) Reload services canceled." | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
#Pre-start functions to be called by the systemd service
script_prestart() {
script_logs
if [[ "$DISCORD_START" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_PRESTART" "Server startup for $1 was initialized."
fi
if [[ "$EMAIL_START" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server startup $1" "Server startup for $1 was initialized at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server startup for $1 was initialized." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Post-start functions to be called by the systemd service
script_poststart() {
script_logs
if [[ "$DISCORD_START" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_POSTSTART" "Server startup for $1 complete."
fi
if [[ "$EMAIL_START" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server startup $1" "Server startup for $1 was completed at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server startup for $1 complete." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Pre-stop functions to be called by the systemd service
script_prestop() {
script_logs
if [[ "$DISCORD_STOP" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_PRESTOP" "Server shutdown for $1 was initialized."
fi
if [[ "$EMAIL_STOP" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server shutdown $1" "Server shutdown was initiated at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server shutdown for $1 was initialized." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Post-stop functions to be called by the systemd service
script_poststop() {
script_logs
#Check if the server is still running, if it is wait for it to stop.
while true; do
tmux -L $SERVICE_NAME-$1-tmux.sock has-session -t $NAME 2>/dev/null
if [ $? -eq 1 ]; then
break
fi
sleep 1
done
if [ -f "/tmp/$SERVICE_NAME-$1-tmux.log" ]; then
rm /tmp/$SERVICE_NAME-$1-tmux.log
fi
if [ -f "/tmp/$SERVICE_NAME-$1-tmux.conf" ]; then
rm /tmp/$SERVICE_NAME-$1-tmux.conf
fi
if [[ "$DISCORD_STOP" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_POSTSTOP" "Server shutdown for $1 complete."
fi
if [[ "$EMAIL_STOP" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Server shutdown $1" "Server shutdown was complete at $(date +"%d.%m.%Y %H:%M:%S")"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server shutdown for $1 complete." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Start the server
script_start() {
script_logs
#Loop until the server is active and output the state of it
script_start_loop() {
SERVER_INSTANCE_LOOP=$(echo $1 | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
while [[ "$(systemctl --user show -p ActiveState --value $1)" == "activating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $1)" == "enabled" ]]; do
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $SERVER_INSTANCE_LOOP is activating. Please wait..." | tee -a "$LOG_SCRIPT"
sleep 1
done
if [[ "$(systemctl --user show -p ActiveState --value $1)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $1)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $SERVER_INSTANCE_LOOP has been successfully activated." | tee -a "$LOG_SCRIPT"
sleep 1
elif [[ "$(systemctl --user show -p ActiveState --value $1)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $1)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $SERVER_INSTANCE_LOOP failed to activate. See systemctl --user status $1 for details." | tee -a "$LOG_SCRIPT"
sleep 1
fi
}
if [[ -z "$1" ]] || [[ "$1" == "ignore" ]]; then
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $SERVER_INSTANCE start initialized." | tee -a "$LOG_SCRIPT"
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $SERVER_INSTANCE is already running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $SERVER_INSTANCE is in failed state. See systemctl --user status $SERVER_SERVICE for details." | tee -a "$LOG_SCRIPT"
if [[ "$1" == "ignore" ]]; then
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
else
read -p "Do you still want to start the server? (y/n): " FORCE_START
if [[ "$FORCE_START" =~ ^([yY][eE][sS]|[yY])$ ]]; then
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
fi
fi
fi
done
else
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@$1.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $1 start initialized." | tee -a "$LOG_SCRIPT"
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $1 is already running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Server $1 is in failed state. See systemctl --user status $SERVER_SERVICE for details." | tee -a "$LOG_SCRIPT"
if [[ "$2" == "ignore" ]]; then
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
else
read -p "Do you still want to start the server? (y/n): " FORCE_START
if [[ "$FORCE_START" =~ ^([yY][eE][sS]|[yY])$ ]]; then
systemctl --user start $SERVER_SERVICE
script_start_loop $SERVER_SERVICE
fi
fi
fi
done
fi
}
#--------------------------
#Stop the server
script_stop() {
script_logs
#Loop until the server is inactive and output the state of it
script_stop_loop() {
SERVER_INSTANCE_LOOP=$(echo $1 | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
while [[ "$(systemctl --user show -p ActiveState --value $1)" == "deactivating" ]]; do
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE_LOOP is deactivating. Please wait..." | tee -a "$LOG_SCRIPT"
sleep 1
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE_LOOP is deactivated." | tee -a "$LOG_SCRIPT"
}
if [ -z "$1" ]; then
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE shutdown in progress." | tee -a "$LOG_SCRIPT"
systemctl --user stop $SERVER_SERVICE
script_stop_loop $SERVER_SERVICE
fi
done
else
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@$1.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Stop) Server $SERVER_INSTANCE shutdown in progress." | tee -a "$LOG_SCRIPT"
systemctl --user stop $SERVER_SERVICE
script_stop_loop $SERVER_SERVICE
fi
done
fi
}
#--------------------------
#Restart the server
script_restart() {
script_logs
if [ -z "$1" ]; then
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is not running. Execute $SERVICE_NAME-script start to start the server." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "activating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is activating. Aborting restart." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "deactivating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is in deactivating. Aborting restart." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is going to restart in 15-30 seconds, please wait..." | tee -a "$LOG_SCRIPT"
sleep 1
script_stop $SERVER_INSTANCE
sleep 1
script_start $SERVER_INSTANCE
sleep 1
fi
done
else
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@$1.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is not running. Execute $SERVICE_NAME-script start to start the server." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "activating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is activating. Aborting restart." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "deactivating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is in deactivating. Aborting restart." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Restart) Server $SERVER_INSTANCE is going to restart in 15-30 seconds, please wait..." | tee -a "$LOG_SCRIPT"
sleep 1
script_stop $SERVER_INSTANCE
sleep 1
script_start $SERVER_INSTANCE
sleep 1
fi
done
fi
}
#--------------------------
#Systemd service sends email if email notifications for crashes enabled
script_send_notification_crash() {
script_logs
CRASH_TIME=$(date +"%Y-%m-%d_%H-%M")
if [ ! -d "$LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME" ]; then
mkdir -p "$LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME"
fi
systemctl --user status $SERVICE_NAME@$1.service > $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_log.txt
zip -j $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_logs.zip $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_log.txt
zip -j $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/script_logs.zip $LOG_SCRIPT
rm $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_log.txt
if [[ "$EMAIL_CRASH" == "1" ]]; then
mail -a $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_logs.zip -a $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/script_logs.zip -r "$EMAIL_SENDER ($NAME $SERVICE_NAME)" -s "Notification: Crash" $EMAIL_RECIPIENT <<- EOF
The $NAME server $1 crashed 3 times in the last 5 minutes. Automatic restart is disabled and the server is inactive. Please check the logs for more information.
Attachment contents:
service_logs.zip - Logs from the systemd service
script_logs.zip - Logs from the script
EOF
fi
if [[ "$DISCORD_CRASH" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_CRASH" "Server $1 crashed 3 times in the last 5 minutes.\nAutomatic restart is disabled and the server is inactive.\n\nPlease review your logs located in $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME."
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Crash) Server crashed. Please review your logs located in $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Creates a backup of the server
script_backup() {
script_logs
#If there is not a folder for today, create one
script_backup_create_folder() {
if [ ! -d "$BCKP_DIR/$1/$BCKP_STRUCTURE" ]; then
mkdir -p "$BCKP_DIR/$1/$BCKP_STRUCTURE"
fi
}
#Backup source to destination
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" != "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Autobackup) Server $SERVER_INSTANCE is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
script_backup_create_folder $SERVER_INSTANCE
cd "$SRV_DIR/$SERVER_INSTANCE"
tar -cpvzf $BCKP_DIR/$SERVER_INSTANCE/$BCKP_STRUCTURE/$(date +"%Y%m%d%H%M")_$SERVER_INSTANCE.tar.gz $SRV_DIR/$SERVER_INSTANCE/
fi
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Backup) Backup complete." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#First timer function for systemd timers to execute parts of the script in order without interfering with each other
script_timer_one() {
script_logs
RUNNING_SERVERS="0"
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_NUMBER=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE_NAME)" == "activating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is activating. Aborting until next scheduled execution." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE_NAME)" == "deactivating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is in deactivating. Aborting until next scheduled execution." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is running." | tee -a "$LOG_SCRIPT"
RUNNING_SERVERS=$(($RUNNING_SERVERS + 1))
fi
done
if [ $RUNNING_SERVERS -gt "0" ]; then
script_remove_old_files
script_backup
fi
}
#--------------------------
#Second timer function for systemd timers to execute parts of the script in order without interfering with each other
script_timer_two() {
script_logs
RUNNING_SERVERS="0"
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_NUMBER=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "inactive" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is not running." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "failed" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is in failed state. Please check logs." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE_NAME)" == "activating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is activating. Aborting until next scheduled execution." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE_NAME)" == "deactivating" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is in deactivating. Aborting until next scheduled execution." | tee -a "$LOG_SCRIPT"
elif [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]] && [[ "$(systemctl --user show -p UnitFileState --value $SERVER_SERVICE)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Status) Server $SERVER_NUMBER is running." | tee -a "$LOG_SCRIPT"
RUNNING_SERVERS=$(($RUNNING_SERVERS + 1))
fi
done
if [ $RUNNING_SERVERS -gt "0" ]; then
script_remove_old_files
fi
}
#--------------------------
#Runs the diagnostics
script_diagnostics() {
echo "Initializing diagnostics. Please wait..."
echo ""
sleep 3
#Check package versions
echo "Checkign package versions:"
if [ -f "/usr/bin/pacman" ]; then
echo "bash version:$(pacman -Qi bash | grep "^Version" | cut -d : -f2)"
echo "coreutils version:$(pacman -Qi coreutils | grep "^Version" | cut -d : -f2)"
echo "sudo version:$(pacman -Qi sudo | grep "^Version" | cut -d : -f2)"
echo "grep version:$(pacman -Qi grep | grep "^Version" | cut -d : -f2)"
echo "sed version:$(pacman -Qi sed | grep "^Version" | cut -d : -f2)"
echo "awk version:$(pacman -Qi awk | grep "^Version" | cut -d : -f2)"
echo "curl version:$(pacman -Qi curl | grep "^Version" | cut -d : -f2)"
echo "rsync version:$(pacman -Qi rsync | grep "^Version" | cut -d : -f2)"
echo "wget version:$(pacman -Qi wget | grep "^Version" | cut -d : -f2)"
echo "findutils version:$(pacman -Qi findutils | grep "^Version" | cut -d : -f2)"
echo "tmux version:$(pacman -Qi tmux | grep "^Version" | cut -d : -f2)"
echo "zip version:$(pacman -Qi zip | grep "^Version" | cut -d : -f2)"
echo "unzip version:$(pacman -Qi unzip | grep "^Version" | cut -d : -f2)"
echo "p7zip version:$(pacman -Qi p7zip | grep "^Version" | cut -d : -f2)"
echo "postfix version:$(pacman -Qi postfix | grep "^Version" | cut -d : -f2)"
echo "samba version:$(pacman -Qi samba | grep "^Version" | cut -d : -f2)"
elif [ -f "/usr/bin/dpkg" ]; then
echo "bash version:$(dpkg -s bash | grep "^Version" | cut -d : -f2)"
echo "coreutils version:$(dpkg -s coreutils | grep "^Version" | cut -d : -f2)"
echo "sudo version:$(dpkg -s sudo | grep "^Version" | cut -d : -f2)"
echo "libpam-systemd version:$(dpkg -s libpam-systemd | grep "^Version" | cut -d : -f2)"
echo "grep version:$(dpkg -s grep | grep "^Version" | cut -d : -f2)"
echo "sed version:$(dpkg -s sed | grep "^Version" | cut -d : -f2)"
echo "gawk version:$(dpkg -s gawk | grep "^Version" | cut -d : -f2)"
echo "curl version:$(dpkg -s curl | grep "^Version" | cut -d : -f2)"
echo "rsync version:$(dpkg -s rsync | grep "^Version" | cut -d : -f2)"
echo "wget version:$(dpkg -s wget | grep "^Version" | cut -d : -f2)"
echo "findutils version:$(dpkg -s findutils | grep "^Version" | cut -d : -f2)"
echo "tmux version:$(dpkg -s tmux | grep "^Version" | cut -d : -f2)"
echo "zip version:$(dpkg -s zip | grep "^Version" | cut -d : -f2)"
echo "unzip version:$(dpkg -s unzip | grep "^Version" | cut -d : -f2)"
echo "p7zip version:$(dpkg -s p7zip | grep "^Version" | cut -d : -f2)"
echo "postfix version:$(dpkg -s postfix | grep "^Version" | cut -d : -f2)"
fi
echo ""
echo "Checking if files and folders present:"
#Check if files/folders present
if [ -f "/usr/bin/$SERVICE_NAME-script" ] ; then
echo "Script present: Yes"
else
echo "Script present: No"
fi
if [ -d "$CONFIG_DIR" ]; then
echo "Configuration folder present: Yes"
else
echo "Configuration folder present: No"
fi
if [ -d "$BCKP_DIR" ]; then
echo "Backups folder present: Yes"
else
echo "Backups folder present: No"
fi
if [ -d "/srv/$SERVICE_NAME/logs" ]; then
echo "Logs folder present: Yes"
else
echo "Logs folder present: No"
fi
if [ -f "$CONFIG_DIR/$SERVICE_NAME-script.conf" ] ; then
echo "Script configuration file present: Yes"
else
echo "Script configuration file present: No"
fi
if [ -f "$CONFIG_DIR/$SERVICE_NAME-discord.conf" ] ; then
echo "Discord configuration file present: Yes"
else
echo "Discord configuration file present: No"
fi
if [ -f "$CONFIG_DIR/$SERVICE_NAME-email.conf" ] ; then
echo "Email configuration file present: Yes"
else
echo "Email configuration file present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME@.service" ]; then
echo "Basic service present: Yes"
else
echo "Basic service present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME-timer-1.timer" ]; then
echo "Timer 1 timer present: Yes"
else
echo "Timer 1 timer present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME-timer-1.service" ]; then
echo "Timer 1 service present: Yes"
else
echo "Timer 1 service present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME-timer-2.timer" ]; then
echo "Timer 2 timer present: Yes"
else
echo "Timer 2 timer present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME-timer-2.service" ]; then
echo "Timer 2 service present: Yes"
else
echo "Timer 2 service present: No"
fi
if [ -f "/srv/$SERVICE_NAME/.config/systemd/user/$SERVICE_NAME-send-notification@.service" ]; then
echo "Notification sending service present: Yes"
else
echo "Notification sending service present: No"
fi
if [ -f "/usr/bin/impostor-server" ]; then
echo "Game executable present: Yes"
else
echo "Game executable present: No"
fi
echo "Diagnostics complete."
}
#--------------------------
#Install tmux configuration for specific server when first ran
script_server_tmux_install() {
if [ -z "$2" ]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Server tmux configuration) Installing tmux configuration for server $1." | tee -a "$LOG_SCRIPT"
TMUX_CONFIG_FILE="/tmp/$SERVICE_NAME-$1-tmux.conf"
elif [[ "$2" == "override" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Server tmux configuration) Installing tmux override configuration for server $1." | tee -a "$LOG_SCRIPT"
TMUX_CONFIG_FILE="$CONFIG_DIR/$SERVICE_NAME-$1-tmux.conf"
fi
if [ -f $CONFIG_DIR/$SERVICE_NAME-$1-tmux.conf ]; then
cp $CONFIG_DIR/$SERVICE_NAME-$1-tmux.conf /tmp/$SERVICE_NAME-$1-tmux.conf
else
if [ ! -f $TMUX_CONFIG_FILE ]; then
touch $TMUX_CONFIG_FILE
cat > $TMUX_CONFIG_FILE <<- EOF
#Tmux configuration
set -g activity-action other
set -g allow-rename off
set -g assume-paste-time 1
set -g base-index 0
set -g bell-action any
set -g default-command "${SHELL}"
#set -g default-terminal "tmux-256color"
set -g default-terminal "screen-hack_color"
set -g default-shell "/bin/bash"
set -g default-size "132x42"
set -g destroy-unattached off
set -g detach-on-destroy on
set -g display-panes-active-colour red
set -g display-panes-colour blue
set -g display-panes-time 1000
set -g display-time 3000
set -g history-limit 10000
set -g key-table "root"
set -g lock-after-time 0
set -g lock-command "lock -np"
set -g message-command-style fg=yellow,bg=black
set -g message-style fg=black,bg=yellow
set -g mouse on
#set -g prefix C-b
set -g prefix2 None
set -g renumber-windows off
set -g repeat-time 500
set -g set-titles off
set -g set-titles-string "#S:#I:#W - \"#T\" #{session_alerts}"
set -g silence-action other
set -g status on
set -g status-bg green
set -g status-fg black
set -g status-format[0] "#[align=left range=left #{status-left-style}]#{T;=/#{status-left-length}:status-left}#[norange default]#[list=on align=#{status-justify}]#[list=left-marker]<#[list=right-marker]>#[list=on]#{W:#[range=window|#{window_index} #{window-status-style}#{?#{&&:#{window_last_flag},#{!=:#{window-status-last-style},default}}, #{window-status-last-style},}#{?#{&&:#{window_bell_flag},#{!=:#{window-status-bell-style},default}}, #{window-status-bell-style},#{?#{&&:#{||:#{window_activity_flag},#{window_silence_flag}},#{!=:#{window-status-activity-style},default}}, #{window-status-activity-style},}}]#{T:window-status-format}#[norange default]#{?window_end_flag,,#{window-status-separator}},#[range=window|#{window_index} list=focus #{?#{!=:#{window-status-current-style},default},#{window-status-current-style},#{window-status-style}}#{?#{&&:#{window_last_flag},#{!=:#{window-status-last-style},default}}, #{window-status-last-style},}#{?#{&&:#{window_bell_flag},#{!=:#{window-status-bell-style},default}}, #{window-status-bell-style},#{?#{&&:#{||:#{window_activity_flag},#{window_silence_flag}},#{!=:#{window-status-activity-style},default}}, #{window-status-activity-style},}}]#{T:window-status-current-format}#[norange list=on default]#{?window_end_flag,,#{window-status-separator}}}#[nolist align=right range=right #{status-right-style}]#{T;=/#{status-right-length}:status-right}#[norange default]"
set -g status-format[1] "#[align=centre]#{P:#{?pane_active,#[reverse],}#{pane_index}[#{pane_width}x#{pane_height}]#[default] }"
set -g status-interval 15
set -g status-justify left
set -g status-keys emacs
set -g status-left "[#S] "
set -g status-left-length 10
set -g status-left-style default
set -g status-position bottom
set -g status-right "#{?window_bigger,[#{window_offset_x}#,#{window_offset_y}] ,}\"#{=21:pane_title}\" %H:%M %d-%b-%y"
set -g status-right-length 40
set -g status-right-style default
set -g status-style fg=black,bg=green
set -g update-environment[0] "DISPLAY"
set -g update-environment[1] "KRB5CCNAME"
set -g update-environment[2] "SSH_ASKPASS"
set -g update-environment[3] "SSH_AUTH_SOCK"
set -g update-environment[4] "SSH_AGENT_PID"
set -g update-environment[5] "SSH_CONNECTION"
set -g update-environment[6] "WINDOWID"
set -g update-environment[7] "XAUTHORITY"
set -g visual-activity off
set -g visual-bell off
set -g visual-silence off
set -g word-separators " -_@"
#Change prefix key from ctrl+b to ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
#Bind C-a r to reload the config file
bind-key r source-file /tmp/$SERVICE_NAME-$1-tmux.conf \; display-message "Config reloaded!"
set-hook -g session-created 'resize-window -y 24 -x 10000'
set-hook -g session-created "pipe-pane -o 'tee >> /tmp/$SERVICE_NAME-$1-tmux.log'"
set-hook -g client-attached 'resize-window -y 24 -x 10000'
set-hook -g client-detached 'resize-window -y 24 -x 10000'
set-hook -g client-resized 'resize-window -y 24 -x 10000'
#Default key bindings (only here for info)
#Ctrl-b l (Move to the previously selected window)
#Ctrl-b w (List all windows / window numbers)
#Ctrl-b <window number> (Move to the specified window number, the default bindings are from 0 – 9)
#Ctrl-b q (Show pane numbers, when the numbers show up type the key to goto that pane)
#Ctrl-b f <window name> (Search for window name)
#Ctrl-b w (Select from interactive list of windows)
#Copy/ scroll mode
#Ctrl-b [ (in copy mode you can navigate the buffer including scrolling the history. Use vi or emacs-style key bindings in copy mode. The default is emacs. To exit copy mode use one of the following keybindings: vi q emacs Esc)
EOF
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Server tmux configuration) Tmux configuration for server $1 installed successfully." | tee -a "$LOG_SCRIPT"
fi
fi
}
#--------------------------
#Configures discord integration
script_config_discord() {
echo ""
read -p "Enable discord notifications (y/n): " INSTALL_DISCORD_ENABLE
if [[ "$INSTALL_DISCORD_ENABLE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo ""
echo "You are able to add multiple webhooks for the script to use in the discord_webhooks.txt file located in the config folder."
echo "EACH ONE HAS TO BE IN IT'S OWN LINE!"
echo ""
read -p "Enter your first webhook for the server: " INSTALL_DISCORD_WEBHOOK
if [[ "$INSTALL_DISCORD_WEBHOOK" == "" ]]; then
INSTALL_DISCORD_WEBHOOK="none"
fi
echo ""
read -p "Discord notifications for server startup? (y/n): " INSTALL_DISCORD_START_ENABLE
if [[ "$INSTALL_DISCORD_START_ENABLE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
INSTALL_DISCORD_START="1"
else
INSTALL_DISCORD_START="0"
fi
echo ""
read -p "Discord notifications for server shutdown? (y/n): " INSTALL_DISCORD_STOP_ENABLE
if [[ "$INSTALL_DISCORD_STOP_ENABLE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
INSTALL_DISCORD_STOP="1"
else
INSTALL_DISCORD_STOP="0"
fi
echo ""
read -p "Discord notifications for crashes? (y/n): " INSTALL_DISCORD_CRASH_ENABLE
if [[ "$INSTALL_DISCORD_CRASH_ENABLE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
INSTALL_DISCORD_CRASH="1"
else
INSTALL_DISCORD_CRASH="0"
fi
elif [[ "$INSTALL_DISCORD_ENABLE" =~ ^([nN][oO]|[nN])$ ]]; then
INSTALL_DISCORD_START="0"
INSTALL_DISCORD_STOP="0"
INSTALL_DISCORD_CRASH="0"
fi
echo "Writing configuration file..."
touch $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_start='"$INSTALL_DISCORD_START" >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_stop='"$INSTALL_DISCORD_STOP" >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_crash='"$INSTALL_DISCORD_CRASH" >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_color_prestart=16776960' >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_color_poststart=65280' >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_color_prestop=16776960' >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_color_poststop=65280' >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo 'discord_color_crash=16711680' >> $CONFIG_DIR/$SERVICE_NAME-discord.conf
echo "$INSTALL_DISCORD_WEBHOOK" > $CONFIG_DIR/discord_webhooks.txt