-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathisrsrv-script.bash
More file actions
2239 lines (2021 loc) · 120 KB
/
isrsrv-script.bash
File metadata and controls
2239 lines (2021 loc) · 120 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 IsRSrv-Script.
#
# IsRSrv-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.
#
# IsRSrv-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/>.
#Interstellar Rift 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.
#Static script variables
export NAME="IsRSrv" #Name of the tmux session.
export VERSION="1.7-5" #Package and script version.
export SERVICE_NAME="isrsrv" #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.
TMPFS_DIR="/srv/$SERVICE_NAME/tmpfs" #Locaton of your tmpfs partition.
CONFIG_DIR="/srv/$SERVICE_NAME/config" #Location of this script's configuration.
UPDATE_DIR="/srv/$SERVICE_NAME/updates" #Location of update information for the script's automatic update feature.
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.
#Static wine variables
WINE_ARCH="win64" #Architecture of the wine prefix
WINE_PREFIX_GAME_DIR="drive_c/Games/InterstellarRift" #Location of server files.
WINE_PREFIX_GAME_CONFIG="drive_c/users/$SERVICE_NAME/AppData/Roaming/InterstellarRift" #Server save and configuration location.
WINE_PREFIX_GAME_EXE="$SRV_DIR/$WINE_PREFIX_GAME_DIR/Build/IR.exe" #Server executable.
APPID="363360" #Steam app id for the server
#Script config file variables
BCKP_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_bckp_delold= | cut -d = -f2) #Defines how many days old backups are deleted.
LOG_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_log_delold= | cut -d = -f2) #Defines how many days old logs are deleted.
LOG_GAME_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_log_game_delold= | cut -d = -f2) #Defines how many days old games logs are deleted.
DUMP_GAME_DELOLD=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_update_game= | cut -d = -f2) #Defines how many days old games dumps are deleted.
UPDATE_IGNORE_FAILED_ACTIVATIONS=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_update_ignore_failed_startups= | cut -d = -f2) #Defines if errors during startup after updates should be ignored.
TIMEOUT_SAVE=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_timeout_save= | cut -d = -f2) #Defines how much time in seconds the script waits for the certain functions to complete.
TMPFS_SPACE=$(cat $CONFIG_DIR/$SERVICE_NAME-script.conf 2> /dev/null | grep script_tmpfs_space= | cut -d = -f2) #Defines how much can the tmpfs parition be filled until an automatic server shutdown is issued.
#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.
LOG_GAME_DELOLD=${LOG_GAME_DELOLD:="7"} #If the variable for old game log deletion is not defined, assign a default value.
DUMP_GAME_DELOLD=${DUMP_GAME_DELOLD:="0"} #If the variable for old game dumps is not defined, assign a default value.
UPDATE_IGNORE_FAILED_ACTIVATIONS=${UPDATE_IGNORE_FAILED_ACTIVATIONS:="0"} #If the variable for ignoring errors after updates is not defined, assign a default value.
TIMEOUT_SAVE=${TIMEOUT_SAVE:="120"} #If the variable for save timeout is not defined, assign a default value.
TMPFS_SPACE=${TMPFS_SPACE:="90"} #If the variable for tmpfs space is not defined, assign a default value.
#Steamcmd config file variables
STEAMCMD_UID=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_username= | cut -d = -f2) #Defines your steam username.
STEAMCMD_PSW=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_password= | cut -d = -f2) #Defines your steam password.
STEAMCMD_BETA_BRANCH=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_beta_branch= | cut -d = -f2) #Defines if the beta branch is enabled.
STEAMCMD_BETA_BRANCH_NAME=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamcmd_beta_branch_name= | cut -d = -f2) #Defines the beta branch name.
STEAMGUARD_CLI=$(cat $CONFIG_DIR/$SERVICE_NAME-steam.conf 2> /dev/null | grep steamguard_cli= | cut -d = -f2) #Defines usage of steamguard_cli.
#Steamcmd config variables if config doesn't exist
STEAMCMD_UID=${STEAMCMD_UID:="disabled"} #If the variable for steam username is not defined, assign a default value.
STEAMCMD_PSW=${STEAMCMD_PSW:="disabled"} #If the variable for steam password is not defined, assign a default value.
STEAMCMD_BETA_BRANCH=${STEAMCMD_BETA_BRANCH:="0"} #If the variable for steam beta branch selection is not defined, assign a default value.
STEAMCMD_BETA_BRANCH_NAME=${STEAMCMD_BETA_BRANCH_NAME:="none"} #If the variable for steam beta branch name is not defined, assign a default value.
STEAMGUARD_CLI=${STEAMGUARD_CLI:="0"} #If the variable for steam guard is not defined, assign a default value.
#Discord config file variables
DISCORD_UPDATE=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_update= | cut -d = -f2) #Send notification when the server updates.
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_TMPFS_SPACE=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_tmpfs_space= | cut -d = -f2) #Send notifications if tmpfs space is over the designated percentage.
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_UPDATE=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_update= | cut -d = -f2) #Discord embed color for update.
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_COLOR_TMPFS_SPACE=$(cat $CONFIG_DIR/$SERVICE_NAME-discord.conf 2> /dev/null | grep discord_color_tmpfs_space= | cut -d = -f2) #Discord embed color for tmpfs space.
#Discord config variables if config doesn't exist
DISCORD_UPDATE=${DISCORD_UPDATE:="0"} #If the variable for discord update is not defined, assign a default value.
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_TMPFS_SPACE=${DISCORD_TMPFS_SPACE:="0"} #If the variable for discord tmpfs space 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_UPDATE=${DISCORD_COLOR_UPDATE:="47083"} #If the variable discord update 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.
DISCORD_COLOR_TMPFS_SPACE=${DISCORD_COLOR_TMPFS_SPACE:="16711680"} #If the variable for discord tmpfs space 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_UPDATE=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_update= | cut -d = -f2) #Send emails when server updates.
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_TMPFS_SPACE=$(cat $CONFIG_DIR/$SERVICE_NAME-email.conf 2> /dev/null | grep email_tmpfs_space= | cut -d = -f2) #Send emails if tmpfs space is over the designated percentage.
#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_UPDATE=${EMAIL_UPDATE:="0"} #If the variable for email update 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.
EMAIL_TMPFS_SPACE=${EMAIL_TMPFS_SPACE:="0"} #If the variable for email tmpfs space 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
}
#--------------------------
#Send discord message
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
#Delete old game dumps
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removing old dump files: $DUMP_GAME_DELOLD days old." | tee -a "$LOG_SCRIPT"
if [ -d $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Dumps/ ]; then
find $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Dumps/* -mtime +$DUMP_GAME_DELOLD -delete
fi
if [ -d $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Dumps/ ]; then
find $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Dumps/* -mtime +$DUMP_GAME_DELOLD -delete
fi
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service $SERVICE_NAME-tmpfs@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
SERVER_TYPE=$(echo $SERVER_SERVICE | awk -F '@' '{print $1}')
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
#Delete old game logs
if [[ "$SERVER_TYPE" == "$SERVICE_NAME-tmpfs" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove old files) Removing old game logs for $SERVER_INSTANCE: $LOG_GAME_DELOLD days old." | tee -a "$LOG_SCRIPT"
find $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Logs_$SERVER_INSTANCE/* -mtime +$LOG_GAME_DELOLD -delete
fi
find $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Logs_$SERVER_INSTANCE/* -mtime +$LOG_GAME_DELOLD -delete
# Delete old backups
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"
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 $SERVICE_NAME-tmpfs@*.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_ADD
if [[ "$ADD_SERVER_INSTANCE_ADD" =~ ^([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 (Single digit numbers must have a 0 before them. Examples: 01, 02 or 58): " SERVER_INSTANCE_ADD
read -p "Enable tmpfs for this server? (y/n): " SERVER_INSTANCE_ADD_TMPFS
if [[ "$SERVER_INSTANCE_ADD_TMPFS" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "$SERVICE_NAME-tmpfs@$SERVER_INSTANCE_ADD.service" >> $CONFIG_DIR/$SERVICE_NAME-server-list.txt
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-sync-tmpfs.service)" == "disabled" ]]; then
systemctl --user enable --now $SERVICE_NAME-sync-tmpfs.service
fi
systemctl --user enable $SERVICE_NAME-tmpfs@$SERVER_INSTANCE_ADD.service
else
echo "$SERVICE_NAME@$SERVER_INSTANCE_ADD.service" >> $CONFIG_DIR/$SERVICE_NAME-server-list.txt
systemctl --user enable $SERVICE_NAME@$SERVER_INSTANCE_ADD.service
fi
systemctl --user daemon-reload
if [ -f "$SRV_DIR/$WINE_PREFIX_GAME_DIR/$WINE_PREFIX_GAME_EXE" ]; then
read -p "Server instance $SERVER_INSTANCE_ADD added successfully. Do you want to start it? (y/n): " START_SERVER_INSTANCE_ADD
if [[ "$START_SERVER_INSTANCE_ADD" =~ ^([yY][eE][sS]|[yY])$ ]]; then
if [[ "$SERVER_INSTANCE_ADD_TMPFS" =~ ^([yY][eE][sS]|[yY])$ ]]; then
systemctl --user start $SERVICE_NAME-tmpfs@$SERVER_INSTANCE_ADD.service
else
systemctl --user start $SERVICE_NAME@$SERVER_INSTANCE_ADD.service
fi
fi
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Add server instance) Server instance $SERVER_INSTANCE_ADD added successfully." | 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
IFS=","
for SERVER_SERVICE in $(cat $CONFIG_DIR/$SERVICE_NAME-server-list.txt | tr "\\n" "," | sed 's/,$//'); do
SERVER_INSTANCE_LIST=("$SERVER_SERVICE" "${SERVER_INSTANCE_LIST[@]}")
done
SERVER_INSTANCE_LIST=("Cancel" "${SERVER_INSTANCE_LIST[@]}")
select SERVER_INSTANCE in "${SERVER_INSTANCE_LIST[@]}"; do
SERVER_INSTANCE_REMOVE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
INSTANCE_FOLDERS=(server_${SERVER_INSTANCE_REMOVE} userdb_${SERVER_INSTANCE_REMOVE} workshop_${SERVER_INSTANCE_REMOVE} backups_${SERVER_INSTANCE_REMOVE} cache_${SERVER_INSTANCE_REMOVE} Logs_${SERVER_INSTANCE_REMOVE})
if [[ "$SERVER_INSTANCE_REMOVE" == "Cancel" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove server instance) User canceled removal of server instance." | tee -a "$LOG_SCRIPT"
break
fi
sed -e "s/$SERVER_INSTANCE//g" -i $CONFIG_DIR/$SERVICE_NAME-server-list.txt
sed '/^$/d' -i $CONFIG_DIR/$SERVICE_NAME-server-list.txt
if [ -d "/srv/$SERVICE_NAME/.config/systemd/user/$SERVER_INSTANCE.d" ]; then
rm -rf /srv/$SERVICE_NAME/.config/systemd/user/$SERVER_INSTANCE.d
fi
for SERVER_SERVICE in $(cat $CONFIG_DIR/$SERVICE_NAME-server-list.txt | grep tmpfs | tr "\\n" "," | sed 's/,$//'); do
SERVER_TMPFS_LIST=("$SERVER_SERVICE" "${SERVER_TMPFS_LIST[@]}")
done
if [ "${#SERVER_TMPFS_LIST[@]}" -eq "0" ]; then
systemctl --user disable $SERVICE_NAME-sync-tmpfs.service
fi
systemctl --user disable $SERVER_INSTANCE
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 $SERVER_INSTANCE
read -p "Delete the server folder for $SERVER_INSTANCE_REMOVE? (y/n): " DELETE_SERVER_INSTANCE
if [[ "$DELETE_SERVER_INSTANCE" =~ ^([yY][eE][sS]|[yY])$ ]]; then
if [ -f "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/server_$SERVER_INSTANCE_REMOVE.json" ]; then
rm -rf $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/server_$SERVER_INSTANCE_REMOVE.json
fi
if [ -f "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG/server_$SERVER_INSTANCE_REMOVE.json" ]; then
rm -rf $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/server_$SERVER_INSTANCE_REMOVE.json
fi
for INSTANCE_FOLDER in ${INSTANCE_FOLDERS[@]}; do
if [ -d "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER" ]; then
rm -rf $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER
fi
if [ -d "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER" ]; then
rm -rf $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER
fi
done
fi
fi
read -p "Delete the backup folder for $SERVER_INSTANCE_REMOVE? (y/n): " DELETE_SERVER_BACKUP
if [[ "$DELETE_SERVER_BACKUP" =~ ^([yY][eE][sS]|[yY])$ ]]; then
rm -rf $BCKP_DIR/$SERVER_INSTANCE_REMOVE
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Remove server instance) Server instance $SERVER_INSTANCE_REMOVE successfully removed." | tee -a "$LOG_SCRIPT"
break
done
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 $SERVICE_NAME-tmpfs@*.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-sync-tmpfs.service)" == "enabled" ]]; then
systemctl --user disable $SERVICE_NAME-sync-tmpfs.service
fi
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
for SERVER_SERVICE in $(cat $CONFIG_DIR/$SERVICE_NAME-server-list.txt | grep tmpfs | tr "\\n" "," | sed 's/,$//'); do
SERVER_TMPFS_LIST=("$SERVER_SERVICE" "${SERVER_TMPFS_LIST[@]}")
done
if [ "${#SERVER_TMPFS_LIST[@]}" -gt "0" ]; then
systemctl --user enable $SERVICE_NAME-sync-tmpfs.service
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
systemctl --user reset-failed
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
}
#--------------------------
#Syncs the wine prefix to tmpfs
script_initial_tmpfs_sync() {
if [[ "$1" == "start" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Initial sync) Starting initial sync to tmpfs service." | tee -a "$LOG_SCRIPT"
rsync -aAX --info=progress2 --exclude="$WINE_PREFIX_GAME_CONFIG/*" $SRV_DIR/ $TMPFS_DIR
if [ ! -d "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG" ]; then
mkdir -p "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG"
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Initial sync) Sync from disk to tmpfs complete." | tee -a "$LOG_SCRIPT"
elif [[ "$1" == "stop" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Initial sync) Shuting down initial sync to tmpfs service." | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
#Sync server files from ramdisk to hdd/ssd
script_sync() {
script_logs
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME-tmpfs@*.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] (Sync) 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] (Sync) Sync from tmpfs to disk for server $SERVER_INSTANCE has been initiated." | tee -a "$LOG_SCRIPT"
/usr/bin/tmux -L $SERVICE_NAME-$SERVER_INSTANCE-tmux.sock send-keys -t $NAME.0 "say Sync from tmpfs to disk has been initiated." ENTER
INSTANCE_FOLDERS=(server_$SERVER_INSTANCE userdb_$SERVER_INSTANCE workshop_$SERVER_INSTANCE backups_$SERVER_INSTANCE cache_$SERVER_INSTANCE Logs_$SERVER_INSTANCE)
if [ -f "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/server_$SERVER_INSTANCE.json" ]; then
rsync -aAX --info=progress2 $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/server_$SERVER_INSTANCE.json $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/server_$SERVER_INSTANCE.json
fi
for INSTANCE_FOLDER in ${INSTANCE_FOLDERS[@]}; do
if [ -d "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER" ]; then
rsync -aAX --info=progress2 $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER/ $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER
fi
done
/usr/bin/tmux -L $SERVICE_NAME-$SERVER_INSTANCE-tmux.sock send-keys -t $NAME.0 "say Sync from tmpfs to disk has been completed." ENTER
sleep 1
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Sync) Sync from tmpfs to disk for server $SERVER_INSTANCE has been completed." | tee -a "$LOG_SCRIPT"
fi
done
}
#--------------------------
#Checks the space occupied on the tmpfs partition and if it's over the user designated value, shuts down tmpfs servers
script_tmpfs_space_check() {
script_logs
CURRENT_TMPFS_SPACE=$(df -H | grep "$TMPFS_DIR" | awk '{print $5}' | cut -d'%' -f1)
if [ $CURRENT_TMPFS_SPACE -ge $TMPFS_SPACE ] ; then
if [[ "$DISCORD_TMPFS_SPACE" == "1" ]]; then
script_discord_message "$DISCORD_COLOR_TMPFS_SPACE" "The tmpfs partition is $CURRENT_TMPFS_SPACE percent filled. Automatic shutdown of tmpfs servers has been initiated."
fi
if [[ "$EMAIL_TMPFS_SPACE" == "1" ]]; then
script_email_message "$NAME-$1" "Notification: Tmpfs running out of space" "The tmpfs partition is $CURRENT_TMPFS_SPACE percent filled. Automatic shutdown of tmpfs servers has been initiated."
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Tmpfs space check) The tmpfs partition is at $CURRENT_TMPFS_SPACE percent filled. Automatic shutdown of tmpfs servers has been initiated." | tee -a "$LOG_SCRIPT"
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME-tmpfs@*.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
script_stop $SERVER_SERVICE
fi
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Tmpfs space check) Shutdown of tmpfs servers complete." | 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"
if [ ! -d "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG" ]; then
mkdir -p "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG"
fi
if [[ "$2" == "tmpfs" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Sync from disk to tmpfs for $1 has been initiated." | tee -a "$LOG_SCRIPT"
INSTANCE_FOLDERS=(server_$1 userdb_$1 workshop_$1 backups_$1 cache_$1 Logs_$1)
if [ -f "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG" ]; then
mkdir -p "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG"
fi
if [ -f "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json" ]; then
rsync -aAX --info=progress2 $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json
fi
if [ -f "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG/server_$1.json" ]; then
rsync -aAX --info=progress2 $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/server_$1.json $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/server_$1.json
fi
for INSTANCE_FOLDER in ${INSTANCE_FOLDERS[@]}; do
if [ -d "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER" ]; then
rsync -aAX --info=progress2 $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER/ $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER
fi
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Sync from disk to tmpfs for $1 complete." | tee -a "$LOG_SCRIPT"
fi
}
#--------------------------
#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 [[ "$2" == "tmpfs" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Sync from tmpfs to disk for $1 has been initiated." | tee -a "$LOG_SCRIPT"
INSTANCE_FOLDERS=(server_$1 userdb_$1 workshop_$1 backups_$1 cache_$1 Logs_$1)
if [ -f "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json" ]; then
rsync -aAX --info=progress2 $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json
fi
if [ -f "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json" ]; then
rsync -aAX --info=progress2 $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Config.json
fi
for INSTANCE_FOLDER in ${INSTANCE_FOLDERS[@]}; do
if [ -d "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER" ]; then
rsync -aAX --info=progress2 $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER/ $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/$INSTANCE_FOLDER
fi
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Start) Sync from tmpfs to disk for $1 complete." | tee -a "$LOG_SCRIPT"
fi
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 [ -f "/tmp/$SERVICE_NAME-wine-$1.log" ]; then
FILE_WINE_LOG=$SERVICE_NAME-wine-$1
if [[ -e $LOG_STRUCTURE/$FILE_WINE_LOG.log || -L $LOG_STRUCTURE/$FILE_WINE_LOG.log ]]; then
i=0
while [[ -e $LOG_STRUCTURE/${FILE_WINE_LOG}_$i.log || -L $LOG_STRUCTURE/${FILE_WINE_LOG}_$i.log ]]; do
let i++
done
FILE_WINE_LOG=${FILE_WINE_LOG}_$i
fi
mv /tmp/$SERVICE_NAME-wine-$1.log $LOG_STRUCTURE/$FILE_WINE_LOG.log
else
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Move wine log) Nothing to move." | tee -a "$LOG_SCRIPT"
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 $SERVICE_NAME-tmpfs@*.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 $SERVICE_NAME-tmpfs@$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 $SERVICE_NAME-tmpfs@*.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 $SERVICE_NAME-tmpfs@$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 $SERVICE_NAME-tmpfs@*.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 $SERVICE_NAME-tmpfs@$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
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-tmpfs@$1.service)" == "enabled" ]]; then
systemctl --user status $SERVICE_NAME-tmpfs@$1.service > $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_log.txt
find "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/Logs_$1/" -maxdepth 1 -type f \( ! -iname "chat.txt" \) -mmin -30 -exec zip $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/game_logs.zip -j {} +
elif [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME@$1.service)" == "enabled" ]]; then
systemctl --user status $SERVICE_NAME@$1.service > $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_log.txt
find "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG/Logs_$1/" -maxdepth 1 -type f \( ! -iname "chat.txt" \) -mmin -30 -exec zip $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/game_logs.zip -j {} +
fi
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
zip -j $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/wine_logs.zip "$(find $LOG_STRUCTURE/$SERVICE_NAME-wine-$1*.log -type f -printf '%T@\t%p\n' | sort -t $'\t' -g | tail -n -1 | cut -d $'\t' -f 2-)"
rm $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/service_log.txt
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
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 -a $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/game_logs.zip -a $LOG_STRUCTURE/Server-$1-crash_$CRASH_TIME/wine_logs.zip -r "$EMAIL_SENDER ($NAME $SERVICE_NAME)" -s "Notification: Server $1 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
game_logs.zip - Logs from the game
wine_logs.zip - Logs from the wine compatibility layer
EOF
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Crash) Server $1 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
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Backup) Backup has been initiated." | tee -a "$LOG_SCRIPT"
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME-tmpfs@*.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
/usr/bin/tmux -L $SERVICE_NAME-$SERVER_INSTANCE-tmux.sock send-keys -t $NAME.0 "say Server backup in progress." ENTER
script_backup_create_folder $SERVER_INSTANCE
cd "$TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG"
tar -cpvzf $BCKP_DIR/$SERVER_INSTANCE/$BCKP_STRUCTURE/$(date +"%Y%m%d%H%M")_$SERVER_INSTANCE.tar.gz $TMPFS_DIR/$WINE_PREFIX_GAME_CONFIG/*_$SERVER_INSTANCE
/usr/bin/tmux -L $SERVICE_NAME-$SERVER_INSTANCE-tmux.sock send-keys -t $NAME.0 "say Server backup complete." ENTER
fi
done
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
/usr/bin/tmux -L $SERVICE_NAME-$SERVER_INSTANCE-tmux.sock send-keys -t $NAME.0 "say Server backup in progress." ENTER
script_backup_create_folder $SERVER_INSTANCE
cd "$SRV_DIR/$WINE_PREFIX_GAME_CONFIG"
tar -cpvzf $BCKP_DIR/$SERVER_INSTANCE/$BCKP_STRUCTURE/$(date +"%Y%m%d%H%M")_$SERVER_INSTANCE.tar.gz $SRV_DIR/$WINE_PREFIX_GAME_CONFIG/*_$SERVER_INSTANCE
/usr/bin/tmux -L $SERVICE_NAME-$SERVER_INSTANCE-tmux.sock send-keys -t $NAME.0 "say Server backup complete." ENTER
fi
done
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Backup) Backup complete." | tee -a "$LOG_SCRIPT"
}
#--------------------------
#Get steam credentials for script operations
script_steamcmd_credentials() {
while [[ "$STEAMCMDSUCCESS" != "0" ]]; do
read -p "Enter your Steam username: " STEAMCMD_UID
echo ""
read -p "Enter your Steam password: " STEAMCMD_PSW
steamcmd +login $STEAMCMD_UID $STEAMCMD_PSW +quit
STEAMCMDSUCCESS=$?
if [[ "$STEAMCMDSUCCESS" == "0" ]]; then
echo "Steam login for $STEAMCMD_UID: SUCCEDED!"
elif [[ "$STEAMCMDSUCCESS" != "0" ]]; then
echo "Steam login for $STEAMCMD_UID: FAILED!"
echo "Please try again."
fi
done
}
#--------------------------
#Change the steam branch of the app
script_change_branch() {
script_logs
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Change branch) Server branch change initiated. Waiting on user configuration." | tee -a "$LOG_SCRIPT"
if [[ "$STEAMCMD_UID" == "disabled" ]] && [[ "$STEAMCMD_PSW" == "disabled" ]]; then
script_steamcmd_credentials
fi
read -p "Are you sure you want to change the server branch? (y/n): " CHANGE_SERVER_BRANCH
if [[ "$CHANGE_SERVER_BRANCH" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Current configuration:"
echo 'Beta branch enabled: '"$STEAMCMD_BETA_BRANCH"
echo 'Beta branch name: '"$STEAMCMD_BETA_BRANCH_NAME"
echo ""
read -p "Public branch or beta branch? (public/beta): " SET_BRANCH_STATE
echo ""
if [[ "$SET_BRANCH_STATE" =~ ^([bB][eE][tT][aA]|[bB])$ ]]; then
STEAMCMD_BETA_BRANCH="1"
echo "Look up beta branch names at https://steamdb.info/app/$APPID/depots/"
echo "Name examples: experimental, ir_0.1.60, ir_0.1.55"
read -p "Enter beta branch name: " STEAMCMD_BETA_BRANCH_NAME
elif [[ "$SET_BRANCH_STATE" =~ ^([pP][uU][bB][lL][iI][cC]|[pP])$ ]]; then
STEAMCMD_BETA_BRANCH="0"
STEAMCMD_BETA_BRANCH_NAME="none"
fi
sed -i '/steamcmd_beta_branch/d' $CONFIG_DIR/$SERVICE_NAME-steam.conf
sed -i '/steamcmd_beta_branch_name/d' $CONFIG_DIR/$SERVICE_NAME-steam.conf
echo 'steamcmd_beta_branch='"$STEAMCMD_BETA_BRANCH" >> $CONFIG_DIR/$SERVICE_NAME-steam.conf
echo 'steamcmd_beta_branch_name='"$STEAMCMD_BETA_BRANCH_NAME" >> $CONFIG_DIR/$SERVICE_NAME-steam.conf
steamcmd +login anonymous +app_info_update 1 +app_info_print $APPID +quit > $UPDATE_DIR/steam_app_data.txt
IFS=","
for SERVER_SERVICE in $(systemctl --user list-units -all --no-legend --no-pager --plain $SERVICE_NAME@*.service $SERVICE_NAME-tmpfs@*.service | awk '{print $1}' | tr "\\n" "," | sed 's/,$//'); do
if [[ "$(systemctl --user show -p ActiveState --value $SERVER_SERVICE)" == "active" ]]; then
WAS_ACTIVE_BRANCH=("$SERVER_SERVICE" "${WAS_ACTIVE_BRANCH[@]}")
fi
done
script_stop
if [[ "$(systemctl --user show -p UnitFileState --value $SERVICE_NAME-sync-tmpfs.service)" == "enabled" ]]; then
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Change branch) Deleting TmpFs content." | tee -a "$LOG_SCRIPT"
rm -rf $TMPFS_DIR
fi
echo "$(date +"%Y-%m-%d %H:%M:%S") [$VERSION] [$NAME] (Change branch) Deleting game installation." | tee -a "$LOG_SCRIPT"
rm -rf $SRV_DIR/$WINE_PREFIX_GAME_DIR/*
if [[ "$STEAMCMD_BETA_BRANCH" == "0" ]]; then
INSTALLED_BUILDID=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"public\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"buildid\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_BUILDID" > $UPDATE_DIR/installed.buildid
INSTALLED_TIME=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"public\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"timeupdated\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_TIME" > $UPDATE_DIR/installed.timeupdated
if [[ "$STEAMGUARD_CLI" == "1" ]]; then
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update $APPID validate +quit
else
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID validate +quit
fi
elif [[ "$STEAMCMD_BETA_BRANCH" == "1" ]]; then
INSTALLED_BUILDID=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"$STEAMCMD_BETA_BRANCH_NAME\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"buildid\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_BUILDID" > $UPDATE_DIR/installed.buildid
INSTALLED_TIME=$(cat $UPDATE_DIR/steam_app_data.txt | grep -EA 1000 "^\s+\"branches\"$" | grep -EA 5 "^\s+\"$STEAMCMD_BETA_BRANCH_NAME\"$" | grep -m 1 -EB 10 "^\s+}$" | grep -E "^\s+\"timeupdated\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d' ' -f3)
echo "$INSTALLED_TIME" > $UPDATE_DIR/installed.timeupdated
if [[ "$STEAMGUARD_CLI" == "1" ]]; then
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW $(steamguard) +app_update $APPID -beta $INSTALL_STEAMCMD_BETA_BRANCH_NAME validate +quit
else
steamcmd +@sSteamCmdForcePlatformType windows +force_install_dir $SRV_DIR/$WINE_PREFIX_GAME_DIR +login $STEAMCMD_UID $STEAMCMD_PSW +app_update $APPID -beta $STEAMCMD_BETA_BRANCH_NAME validate +quit
fi
fi
script_initial_tmpfs_sync start
for SERVER_SERVICE in "${WAS_ACTIVE_BRANCH[@]}"; do
SERVER_INSTANCE=$(echo $SERVER_SERVICE | awk -F '@' '{print $2}' | awk -F '.service' '{print $1}')
if [[ "$UPDATE_IGNORE_FAILED_ACTIVATIONS" == "1" ]]; then
script_start $SERVER_INSTANCE "ignore"
else
script_start $SERVER_INSTANCE
fi
done