aboutsummaryrefslogtreecommitdiff
path: root/server.sh
blob: 9b8156b6ba6a8feaff8d58e511ba26e5e508b90a (plain)
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
#!/bin/bash

#CONFIG
JRE_JAVA="java"
JVM_ARGS="-Xms512M -Xmx512M" 
JAR="server.jar"
JAR_ARGS="-nogui"
SCREEN_WINDOW="minecraftserverscreen"
WORLD_NAME="world"
LOGFILE="server-screen.log"
PIDFILE="server-screen.pid"
#HOOKS
BACKUP_HOOK='backup_hook_example'

function backup_hook_example {
	echo $ARCHNAME
}

function send_cmd () {
	screen -S $SCREEN_WINDOW -p 0 -X stuff "$1^M"
}

function assert_running() {
	if [ ! -e $PIDFILE ]; then
		return
	fi

	ps -p $(cat $PIDFILE) > /dev/null
	if [ $? -eq 0 ]
	then
		echo "It seems a server is already running. If this is not the case,\
			manually attach to the running screen and close it."
		exit 1
	fi
}

function assert_not_running() {
	if [ ! -e $PIDFILE ]; then
		echo "Server not running"
		exit 1
	fi

	ps -p $(cat $PIDFILE) > /dev/null
	if [ ! $? -eq 0 ]
	then
		echo "Server not running"
		exit 1
	fi
}

function server_start() {
	assert_running

	if [ ! -e "eula.txt" ]
	then
		echo "eula.txt not found. Creating and accepting EULA."
		echo "eula=true" > eula.txt
	fi

	rm -f $LOGFILE
	screen -L -Logfile "$LOGFILE" -S $SCREEN_WINDOW -p 0 -D -m \
		$JRE_JAVA $JVM_ARGS -jar $JAR $JAR_ARGS > /dev/null &
	echo $! > $PIDFILE
	echo Started with PID $!
	exit
}

function server_stop() {
	assert_not_running
	send_cmd "stop"

	local RET=1
	while [ ! $RET -eq 0 ]
	do
		sleep 1
		ps -p $(cat $PIDFILE) > /dev/null
		RET=$?
	done

	echo "stopped the server"

	rm -f $PIDFILE

	exit
}

function server_attach() {
	assert_not_running
	screen -r -p 0 $SCREEN_WINDOW
	exit
}

function server_status() {
	ps -p $(cat $PIDFILE) > /dev/null

	if [ $? -eq 0 ]
	then
		echo "Server is running"
	else
		echo "Server is not running"
	fi
	exit
}

function server_backup_safe() {
	echo "Detected running server. Disabling autosave"
	send_cmd "save-off"
	send_cmd "save-all flush"
	echo "Waiting for save... If froze, run /save-on to re-enable autosave!!"
	
	sleep 1
	while [ $(tail -n 3 "$LOGFILE" | grep -c "Saved the game") -lt 1 ]
	do
		sleep 1
	done
	echo "Done! starting backup..."

	create_backup_archive
	local RET=$?

	echo "Re-enabling auto-save"
	send_cmd "save-on"

	if [ $RET -eq 0 ]
	then
		echo Running backup hook
		$BACKUP_HOOK
	fi
}

function server_backup_unsafe() {
	echo "No running server detected. Running Backup"

	create_backup_archive

	if [ $? -eq 0 ]
	then
		echo Running backup hook
		$BACKUP_HOOK
	fi
}

function create_backup_archive() {
	ARCHNAME="backup/$WORLD_NAME-backup_`date +%d-%m-%y-%T`.tar.gz"
	tar -czf "$ARCHNAME" "./$WORLD_NAME"

	if [ ! $? -eq 0 ]
	then
		echo "TAR failed. No Backup created."
		rm $ARCHNAME #remove (probably faulty) archive
		return 1
	else
		echo $ARCHNAME created.
	fi
}

function server_backup() {
	screen -list $SCREEN_WINDOW > /dev/null
	if [ $? -eq 0 ]
	then #Server is running
		server_backup_safe
	else #Not running
		server_backup_unsafe
	fi

	exit
}

cd $(dirname $0)

case $1 in
	"start")
		server_start
		;;
	"stop")
		server_stop
		;;
	"attach")
		server_attach
		;;
	"backup")
		server_backup
		;;
	"status")
		server_status
		;;
	*)
		echo "Usage: $0 start|stop|attach|status|backup"
		;;
esac