aboutsummarylogtreecommitdiffstats
path: root/minecraftd.sh
blob: 8743bd7466544f0b9f96dbdbe901c16f61656981 (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
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
#!/bin/bash

source /etc/conf.d/minecraft || echo "Could not source /etc/conf.d/minecraft"

# General rule for the variable-naming-schema:
# Variables in capital letters may be passed through the command line others not.

# You may use this script for any minecraft server of your choice, just alter the config file
SERVER_ROOT="${SERVER_ROOT:-/srv/minecraft}"
BACKUPPATH="${BACKUPPATH:-/srv/minecraft/backup}"
LOGPATH="${LOGPATH:-/srv/minecraft/logs}"
WORLDPATHS="${WORLDPATHS:-world}"
KEEP_BACKUPS="${KEEP_BACKUPS:-10}"
MC_USER="${MC_USER:-minecraft}"
MAIN_JAR="${MAIN_JAR:-minecraft_server.jar}"
SESSION_NAME="${SESSION_NAME:-minecraft}"

# Specify system parameters for the minecraft server
MINHEAP="${MINHEAP:-512M}"
MAXHEAP="${MAXHEAP:-1024M}"
THREADS="${THREADS:-1}"
JAVA_PARMS="${JAVA_PARMS:-"-Xmx${MAXHEAP} -Xms${MINHEAP} -XX:ParallelGCThreads=${THREADS}"}"

# The actual program name
declare -r myname="minecraftd"

# Check whether sudo is needed at all
if [[ $(whoami) == ${MC_USER} ]]; then
	SUDO_CMD=""
else
	SUDO_CMD="sudo -u ${MC_USER}"
fi

# Check for sudo rigths
if [[ $(${SUDO_CMD} whoami) != ${MC_USER} ]]; then
	echo -e "You have \e[39;1mno permission\e[0m to run commands as $MC_USER user."
	exit 1
fi

# Pipe any given argument to the minecraft server console
mc_command() {
	${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "`printf \"$*\r\"`"
}

# Start the server if it is not already running
server_start() {
	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		echo "A screen ${SESSION_NAME} session is already running. Please close it first."
	else
		echo -en "Starting server... "
		${SUDO_CMD} screen -dmS "${SESSION_NAME}" /bin/bash -c "cd '${SERVER_ROOT}'; java ${JAVA_PARMS} -jar '${SERVER_ROOT}/${MAIN_JAR}' nogui"
		echo -e "\e[39;1m done\e[0m"
	fi
}

# Stop the server gracefully by saving everything prior and warning the users
server_stop() {
	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		mc_command save-all
		mc_command say "Server is going down in 10 seconds! HURRY UP WITH WATHEVER YOU ARE DOING!" # Warning the users
		echo -en "Server is going down in... "
		for i in $(seq 1 10); do
			mc_command say "down in... $(expr 10 - $i)"
			echo -n " $(expr 10 - $i)"
			sleep 1
		done
		mc_command stop
		echo -e "\e[39;1m done\e[0m"
	else
		echo "The corresponding screen session for ${SESSION_NAME} was already dead."
	fi
}

# Print whether the server is running and if so give some information about memory usage and threads
server_status() {
	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		echo -e "Status:\e[39;1m running\e[0m"

		# Calculating memory usage
		for p in $(${SUDO_CMD} pgrep -f "${MAIN_JAR}"); do
			ps -p${p} -O rss | tail -n1;
		done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(screen, bash,", count-2, "x java)"; print "Total memory usage =", sum/1024, "MB" ;};'
	else
		echo -e "Status:\e[39;1m stopped\e[0m"
	fi
}

# Restart the complete server by shutting it down and starting it again
server_restart() {
	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		server_stop
		sleep 0.1
		server_start
	else
		server_start
	fi
}

# Backup the directories specified in $WORLDPATHS
backup_files() {
	# Check for the availability of the tar binaries
	which tar &> /dev/null
	if [[ $? -ne 0 ]]; then
		echo "The tar binaries are needed for a backup."
		exit 2
	fi

	echo "Starting backup..."
	FILE="$(date +%Y_%m_%d_%H.%M.%S).tar.gz"
	${SUDO_CMD} mkdir -p "${BACKUPPATH}"
	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		mc_command save-off
		mc_command save-all
		sync && wait
		${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals ${WORLDPATHS} 2>&1 | grep -v "tar: Removing leading "
		mc_command save-on
	else
		${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals ${WORLDPATHS} 2>&1 | grep -v "tar: Removing leading "
	fi
	echo -e "\e[39;1mbackup completed\e[0m\n"

	echo -n "Only keeping the last ${KEEP_BACKUPS} backups and removing the other ones..."
	BACKUP_COUNT=$(for f in "${BACKUPPATH}"/[0-9_.]*; do echo ${f}; done | wc -l)
	if [[ $(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) -gt 0 ]]; then
		${SUDO_CMD} rm $(for f in "${BACKUPPATH}"/[0-9_.]*; do echo ${f}; done | head -n$(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}))
		echo -e "\e[39;1m done\e[0m ($(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) backup(s) pruned)"
	else
		echo -e "\e[39;1m done\e[0m (no backups pruned)"
	fi
}

# Restore backup
backup_restore() {
	# Check for the availability of the tar binaries
	which tar &> /dev/null
	if [[ $? -ne 0 ]]; then
		echo "The tar binaries are needed for a backup."
		exit 2
	fi

	# Only allow the user to restore a backup if the server is down
	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data."
		exit 3
	fi

	# Either let the user choose a backup or expect one as an argument
	if [[ $# -lt 1 ]]; then
		echo "Please enter the corresponding number for the backup to be restored: "
		i=1
		for f in "${BACKUPPATH}"/[0-9_.]*; do
			echo -e "    \e[39;1m$i)\e[0m\t$f"
			i=$((i+1))
		done
		echo -en "Restore backup number: "

		# Read in user input
		read user_choice

		# Interpeting the input
		if [[ $user_choice =~ ^-?[0-9]+$ ]]; then
			n=1
			for f in "${BACKUPPATH}"/[0-9_.]*; do
				[[ ${n} -eq $user_choice ]] && FILE="$f"
				n=$((n+1))
			done
			if [[ -z $FILE ]]; then
				echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter the digit of the presented options."
				exit 5
			fi
		else
			echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter a valid digit for one of the presented options."
			exit 6
		fi
	elif [[ $# -eq 1 ]]; then
		# Check for the existance of the specified file
		if [[ -f "$1" ]]; then
			FILE="$1"
		else
			if [[ -f "${BACKUPPATH}"/"$1" ]]; then
				FILE="${BACKUPPATH}"/"$1"
			else
				echo -e "Sorry, but '$1', is \e[39;1mnot a valid file\e[0m, neither in your current directory nor in the backup folder."
				exit 4
			fi
		fi
	elif [[ $# -gt 1 ]]; then
		echo -e "\e[39;1mToo many arguments.\e[0m Please pass only the filename for the world data as an argument."
		echo "Or alternatively no arguments at all to chose from a list of available backups."
		exit 1
	fi

	echo "Restoring backup..."
	${SUDO_CMD} tar -xf "${FILE}" -C "${SERVER_ROOT}" 2>&1
	if [[ $? -eq 0 ]]; then
		echo -e "\e[39;1mRestoration completed\e[0m"
	else
		echo -e "\e[39;1mFailed to restore backup.\e[0m"
	fi
}

# Run the given comman at the minecraft server console
server_command() {
	if [[ $# -lt 1 ]]; then
		echo "No server command specified. Try 'help' for a list of commands."
		exit 1
	fi

	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		sleep 0.1s &
		sleep_pid=$!
		mc_command "$@" &
		tail -f --pid=${sleep_pid} -n 0 "${LOGPATH}/latest.log"
	else
		echo "There is no ${SESSION_NAME} session to connect to."
	fi
}

# Enter the screen minecraft session
server_console() {
	${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
	if [[ $? -eq 0 ]]; then
		${SUDO_CMD} screen -S "${SESSION_NAME}" -rx
	else
		echo "There is no ${SESSION_NAME} session to connect to."
	fi
}

# Help function, no arguments required
help() {
	cat <<-EOF
	This script was design to easily control any minecraft server. Quite every parameter for a given
	minecraft server derivative can be altered by editing the variables in the configuration file.

	Usage: $myname {start|stop|status|backup|restore|command <command>|console}
	    start                Start the minecraft server
	    stop                 Stop the minecraft server
	    restart              Restart the minecraft server
	    status               Print some status information
	    backup               Backup the world data
	    restore [filename]   Restore the world data from a backup
	    command <command>    Run the given comman at the minecraft server console
	    console              Enter the server console through a screen session

	Copyright (c) Gordian Edenhofer <gordian.edenhofer@gmail.com>
	EOF
}

case "$1" in
	start)
	server_start
	;;

	stop)
	server_stop
	;;

	status)
	server_status
	;;

	restart)
	server_restart
	;;

	console)
	server_console
	;;

	command)
	server_command "${@:2}"
	;;

	backup)
	backup_files
	;;

	restore)
	backup_restore "${@:2}"
	;;

	*|-h|--help)
	help
esac

exit 0