-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitscript
executable file
·534 lines (483 loc) · 15.9 KB
/
initscript
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
#!/bin/bash
#
# Minecraft SMP Server startup script
### BEGIN INIT INFO
# Provides: Minecraft
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Minecraft
# Description: Enable Minecraft server for #minecracy
### END INIT INFO
#Add to config:
#BACKUP_DIRS="DIR1 DIR2 DIR3"
#BACKUP_LIST_IGNORE=( DIR1 FILE1 FILE2 DIR2 )
homedir=/home/minecraft
icondir=$homedir/server-icons
get_configuration() {
source /home/minecraft/minecraft.conf
}
dump_settings () {
local varnames="MC_USER MC_DIR MEM_USE HEY0_MOD GRACE_TIME MC_JAR"
local tmp
echo "# Minecraft server configuration"
echo "# Date: $(date)"
echo ""
for tmp in $varnames; do
echo "$tmp=${!tmp}"
done
}
getserverpids () {
ps -C java -o pid=,cmd= | grep $MC_JAR | awk '{ print $1 }' 2>/dev/null
}
server_send_message () {
local message=$*
tmux send-keys -t minecraft "$message" C-m
}
server_send_chat_message () {
server_send_message "say $*"
server_send_message "discord bcast $*"
}
send_stop_cmd () {
server_send_message "stop"
}
construct_cmd () {
# echo "cd $MC_DIR; tmux new-session -d -n minecraft -s minecraft \"/usr/bin/java -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false -Xms256M -Xmx${MEM_USE}M -XX:+UseConcMarkSweepGC -jar $MC_JAR nogui\""
echo "cd $MC_DIR; tmux new-session -d -n minecraft -s minecraft \"/usr/bin/java -Xms${MEM_USE} -Xmx${MEM_USE} ${JAVA_FLAGS} -jar $MC_JAR nogui\""
}
server_is_up() {
# if [ ! -e ${MC_DIR}/server.log.lck ]; then
# # No lockfile means the server is almost certainly down.
# server_up="1"
# return 1
# else
# server_up="0"
# fi
[[ -n $(getserverpids) ]]
# status = [ `ps ux | grep tmux | grep -c $MC_JAR` -gt 0 ]
# return $status
}
server_start () {
local status
local cmd=$(construct_cmd)
echo "Running: $cmd"
eval $cmd
status=$?
if [ ! $status -eq 0 ]; then
echo "Error: Command exited with non-zero status!" >&2
exit
fi
for (( second=0; second < 30; second++ )); do
if server_is_up; then
echo "Minecraft server started successfully, needed $second seconds."
:>$MC_DIR/mcrunning
exit
fi
sleep 1
done
echo "Minecraft server failed to start properly."
exit 1
}
server_stop () {
local second
local cleanshutdown
rotate_servericon
if [ $GRACE_TIME -ne 0 ]; then
echo "Giving Minecraft players $GRACE_TIME seconds warning..."
server_send_message "say ACHTUNG: Der Server wird in $GRACE_TIME Sekunden heruntergefahren!"
server_send_message "discord bcast :warning: **ACHTUNG: Der Server wird in $GRACE_TIME Sekunden heruntergefahren!**"
sleep $GRACE_TIME
else
server_send_message "discord bcast :octagonal_sign: **Der Server wurde heruntergefahren. Einfach so.**"
fi
server_send_message "say Der Server fährt JETZT herunter."
send_stop_cmd
for ((second=1 ; second < 30 ; second++)); do
sleep 1
if server_is_up; then
dummyvar=0
else
cleanshutdown="1"
rm $MC_DIR/mcrunning
break
fi
done
if [ "$cleanshutdown" == "1" ]; then
echo "Minecraft server stopped successfully."
else
echo "Minecraft server failed to shutdown within 30 seconds!" >&2
exit 1
fi
}
# Backup stuff functions
backup_list () {
list=$(ls -p "${MC_DIR}/backups")
bold=$(tput bold)
normal=$(tput sgr0)
for entry in $list; do
entry="${entry%?}"
ignore=1
for ((i=0; i < ${#BACKUP_LIST_IGNORE[@]}; i++ )); do
if [ "$entry" == "${BACKUP_LIST_IGNORE[$i]}" ]; then
ignore=0
fi
done
if [ $ignore == 1 ]; then
echo "${bold}${entry}${normal}"
if [ -e ${MC_DIR}/backups/${entry}/backup_info ]; then
cat ${MC_DIR}/backups/${entry}/backup_info
else
echo "Backup Information for ${entry} not found."
fi
echo " "
fi
done
}
backup_Log () {
if server_is_up; then
echo "Cannot backup server.log while server is running."
exit
fi
timestamp=$(date "+%Y-%m-%d")
}
backup_data () {
arg_o=1
arg_s=1
arg_n=1
if [ $# != 1 ]; then
typeArgs=$1
for (( i=0; i < ${#typeArgs}; i++ )); do
if [ "${typeArgs:$i:1}" == "o" ]; then
arg_o=0
elif [ "${typeArgs:$i:1}" == "s" ]; then
arg_s=0
echo "Will simulate backup."
elif [ "${typeArgs:$i:1}" == "n" ]; then
arg_n=0
fi
done
fi
if [ ${arg_n} == 0 ]; then
noteArg="${2}"
fi
backup_name=${!#}
if [ -d "${MC_DIR}/backups/${backup_name}" ]; then
if [ ${arg_o} == 0 ]; then
echo "Backup \"${backup_name}\" already exists and will be overwritten."
if [ ${arg_s} == 1 ]; then
backup_remove $backup_name
else
echo "Backup removed [simulated]."
fi
else
echo "Backup \"${backup_name}\" already exists. To overwrite, set -o flag."
exit
fi
fi
# Do actual backup
save_off=1
if server_is_up; then
echo "Server is running, doing save-off; save-all"
server_send_message "save-off"
server_send_message "save-all"
save_off=0
echo "Server world saving switched off."
fi
echo "Copy data:"
if [ $arg_s == 1 ]; then
mkdir "${MC_DIR}/backups/${backup_name}"
for (( i=0; i < ${#BACKUP_DIRS[@]}; i++ )); do
echo "Copying ${BACKUP_DIRS[$i]} ..."
cp -a ${BACKUP_DIRS[$i]} ${MC_DIR}/backups/${backup_name}
done
if [ ! -d "${MC_DIR}/backups/${backup_name}" ]; then
echo "Copy process failed. Do you have writing permission in ${MC_DIR}/backups/${backup_name}?"
exit
fi
else
for (( i=0; i < ${#BACKUP_DIRS[@]}; i++ )); do
echo "Copying ${BACKUP_DIRS[$i]} ..."
done
fi
echo "Backup written to ${MC_DIR}/backups."
if server_is_up; then
server_send_message "save-on"
echo "Server world saving switched on."
elif [ $save_off == 0 ]; then
echo "Server exited while doing backup. Could not set \"save-on\"."
fi
# Provide extra information about the backup
timestamp=$(date "+%d.%m.%Y %T")
if [ $arg_n == 0 ]; then
template=$noteArg
backup_reason=""
echo "Using $noteArg as template for backup_info."
else
template="${MC_DIR}/backups/script_resource/std_template"
echo "Enter backup reason, i.e. \"before update from x to z\""
read -e backup_reason
fi
if [ $arg_s == 1 ]; then
touch ${MC_DIR}/backups/${backup_name}/backup_info
sed -e "s;%date%;$timestamp;"\
-e "s;%mc_ver%;$MC_VER;"\
$template > ${MC_DIR}/backups/${backup_name}/backup_info
echo "$backup_reason" >> ${MC_DIR}/backups/${backup_name}/backup_info
fi
echo "Backup info written."
#if [ $arg_s == 1 ]; then
# chmod -R u-w "${MC_DIR}/backups/${backup_name}"
#fi
}
backup_remove () {
if [ -d "${MC_DIR}/backups/$1" ]; then
for (( i=0; i < ${#BACKUP_LIST_IGNORE[@]}; i++ )); do
if [ ${BACKUP_LIST_IGNORE[$i]} == $1 ]; then
echo "This is not a backup. I will not remove or overwrite it."
exit
fi
done
chmod -R u+w "${MC_DIR}/backups/$1"
rm -rf "${MC_DIR}/backups/$1"
echo "Backup $1 removed."
else
echo "Backup $1 does not exist, so obviously I cannot remove it."
exit
fi
}
rotate_servericon() {
icon=$(ls $icondir/*.png | shuf -n 1)
cp $icon $homedir/server-icon.png
}
get_configuration
# User Interface
case "$1" in
'start')
if server_is_up; then
echo "The minecraft server is already running."
else
server_start
fi
;;
'stop')
if server_is_up; then
if [ "$2" == "-f" ]; then
GRACE_TIME=0
fi
server_stop
else
echo "The minecraft server is not running."
fi
;;
'restart')
if server_is_up; then
if [ "$2" == "-f" ]; then
GRACE_TIME=0
fi
server_stop
sleep 5
server_start
else
echo "The minecraft server is not running."
fi
;;
'autorestart')
if [ -e ~/mcrunning ] && ! server_is_up ] ; then
server_start
fi
;;
'status')
if [ "$2" == "-s" ]; then
if server_is_up; then
echo "1"
else
echo "0"
fi
else
if server_is_up; then
echo "The minecraft server is currently running."
else
echo "The minecraft server is currently NOT running."
fi
fi
;;
'backup')
cd $homedir
#argument check
if [ $# -eq 1 ]; then
less <<EOF
Usage: $0 backup [arguments] BACKUP_NAME
Creates a backup of the server files.
The backup will be named "BACKUP_NAME" and be located in a directory called
"backups" which is located besides the server files.
Backups are read-only, so make sure to give writing permissions when restoring.
Arguments:
-e execute: Executes a given shell script before doing backup, even
before checking if the backup already exists.
-l list: Lists all backups.
-n note: Reads the backup note from a given file.
-o overwrite: Overwrites any existing backup named as the given
backup name.
-r remove: Removes the given backup.
-s simulate: Simulates a backup process without actually writing anything.
Mainly made for debugging and testing.
EOF
exit
fi
#Map: i 0 1 2 3 4 5
args_map=( e l n o r s )
args=( 1 1 1 1 1 1 )
possible_strArgs=( e n )
strArgs=( "${!#}" ) # stores the string arguments
strArgs_types=( b ) # stores the mapping of string arguments
count=2 #cause the for is for ... in
for i in "${@:2}"; do
# Handle Type Arguments
if [ "${i:0:1}" == "-" ]; then
for ((j=1; j < ${#i}; j++ )); do
# Collect String Argument Types in the right order
for (( k=0; k < ${#possible_strArgs[@]}; k++ )); do
strArg_type=${possible_strArgs[$k]}
if [ "${i:$j:1}" == $strArg_type ]; then
strArgs_types[${#strArgs_types[@]}]=$strArg_type
fi
done
# Set Booleans in $args if argument is passed
for (( arg=0; arg <= ${#args[@]}; arg++ )); do
if [ "${i:$j:1}" == "${args_map[$arg]}" ]; then
args[$arg]=0
fi
done
done
# Handle String Arguments
elif [ "$count" != "$#" ]; then
strArgs[${#strArgs[@]}]="$i"
fi
count=$((count + 1))
done
# Check for compatibility of arguments. In case of list, Log
# or remove, immediately act as desired.
# Otherwise, do backup after check.
for (( i=0; i <= ${#args[@]}; i++ )); do
if [ "${args[$i]}" == 0 ]; then
case "${args_map[$i]}" in
'L')
for (( j=0; j <= ${#args[@]}; j++ )); do
if [ "${args[$j]}" == 0 ] && [ "${args_map[$j]}" != 'L' ]; then
echo "-L is incompatible to all other arguments."
exit
fi
done
backup_Log
exit
;;
'l')
for (( j=0; j <= ${#args[@]}; j++ )); do
if [ "${args[$j]}" == 0 ] && [ "${args_map[$j]}" != 'l' ]; then
echo "-l is incompatible to all other arguments."
exit
fi
done
backup_list
exit
;;
'r')
for (( j=0; j <= ${#args[@]}; j++ )); do
if [ "${args[$j]}" == 0 ] && [ "${args_map[$j]}" != 'r' ]; then
echo "-r is incompatible to all other arguments."
exit
fi
done
if [ $# == 2 ]; then
echo "No backup name given to remove."
exit
fi
;;
esac
fi
done
# just in case something weird happens and the mapping above goes wrong
if [ ${#strArgs[@]} != ${#strArgs_types[@]} ]; then
echo "Error: Argument mapping went wrong"
exit
fi
# pass arguments to data_backup()
passArgs=""
noteArg=""
sim=1
exec=1
overwrite=1
backup_name="${strArgs[0]}"
for (( i=0; i < ${#args[@]}; i++ )); do
if [ "${args[$i]}" == 0 ]; then
case ${args_map[$i]} in
'l')
backup_list
exit
;;
'e')
for (( j=0; j <= ${#strArgs}; j++ )); do
if [ "${strArgs_types[$j]}" == 'e' ]; then
exec_arg=${strArgs[$j]}
fi
done
if [ "${exec_arg}" == "" ]; then
echo "Error: Execute argument is empty."
exit
fi
exec=0
;;
'l')
backup_list
exit
;;
'n')
for (( j=0; j <= ${#strArgs[@]}; j++ )); do
if [ "${strArgs_types[$j]}" == "n" ]; then
passArgs+='n'
noteArg=${strArgs[$j]}
fi
done
;;
'o')
passArgs+='o'
overwrite=0
;;
'r')
backup_remove "${backup_name}"
exit
;;
's')
passArgs+='s'
sim=0
;;
esac
fi
done
passArgs+=" ${noteArg}"
if [ $exec == 0 ]; then
if [ ! -d ${MC_DIR}/backups/${backup_name} ]; then
echo "Executing: ${exec_arg}"
. "${exec_arg}"
else
if [ $overwrite == 0 ]; then
echo "Executing: ${exec_arg}"
. "${exec_arg}"
else
echo "${MC_DIR}/backups/${backup_name} already exists."
echo "Use -o flag to overwrite it."
fi
fi
fi
backup_data $passArgs $noteArg $backup_name
exit
;;
'dumpconfig')
dump_settings
;;
*)
echo "Usage: $0 start|stop|restart|status|backup|dumpconfig"
;;
esac
# vim: set ts=2: