aboutsummaryrefslogtreecommitdiff
path: root/server.sh
blob: d1440530251712a5a991257b2699f58ad157862c (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/bash

if [ -e "serverconf.sh" ]
then
	source "serverconf.sh"
else
	echo No configuration found in PWD. Exiting.
	exit 1
fi

source "backends/tar.sh"
source "backends/bup.sh"
source "backends/borg.sh"

function backup_hook_example {
	bup -d $CUR_BACK_DIR ls -l $BACKUP_NAME/latest/var/minecraft
}

function send_cmd () {
	tmux -S $TMUX_SOCKET send -t $TMUX_WINDOW "$1" enter
}

function assert_running() {
	if server_running; 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 ! server_running; then
		echo "Server not running"
		exit 1
	fi
}

function server_start() {
	assert_running

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

	tmux -S $TMUX_SOCKET new-session -s $TMUX_WINDOW -d \
		$JRE_JAVA $JVM_ARGS -jar $JAR $JAR_ARGS
	pid=`tmux -S $TMUX_SOCKET list-panes -t $TMUX_WINDOW -F "#{pane_pid}"`
	echo $pid > $PIDFILE
	echo Started with PID $pid
	exit
}

function server_stop() {
	# Allow success even if server is not running
	#trap "exit 0" EXIT

	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
	tmux -S $TMUX_SOCKET attach -t $TMUX_WINDOW
	exit
}

function server_running() {
	if [ -f $PIDFILE ] && [ "$(cat $PIDFILE)" != "" ]; then
		ps -p $(cat $PIDFILE) > /dev/null
		return
	fi

	false
}

function server_status() {
	if server_running
	then
		echo "Server is running"
	else
		echo "Server is not running"
	fi
	exit
}

function players_online() {
	send_cmd "list"
	sleep 1
	while [ $(tail -n 3 "$LOGFILE" | grep -c "There are") -lt 1 ]
	do
		sleep 1
	done

	[ `tail -n 3 "$LOGFILE" | grep -c "There are 0"` -lt 1 ]
}

function init_backup() {
	for BACKUP_DIR in ${BACKUP_DIRS[*]}
	do
		if [[ $BACKUP_DIR == *:* ]]; then
			REMOTE="$(echo "$BACKUP_DIR" | cut -d: -f1)"
			REMOTE_DIR="$(echo "$BACKUP_DIR" | cut -d: -f2)"
			ssh "$REMOTE" "mkdir -p \"$REMOTE_DIR\""
		else
			mkdir -p "$BACKUP_DIR"
		fi
	done

	if [ $BACKUP_BACKEND = "bup" ]; then
		bup_init
	elif [ $BACKUP_BACKEND = "borg" ]; then
		borg_init
	else
		tar_init
	fi
}

function create_backup() {
	init_backup

	if [ $BACKUP_BACKEND = "bup" ]; then
		bup_create_backup
	elif [ $BACKUP_BACKEND = "borg" ]; then
		borg_create_backup
	else
		tar_create_backup
	fi
}

function server_backup_safe() {
	force=$1
	echo "Detected running server. Checking if players online..."
	if [ "$force" != "true" ] && ! players_online; then
		echo "Players are not online. Not backing up."
		return
	fi

	echo "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
	sleep 2
	echo "Done! starting backup..."

	create_backup

	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

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

function backup_running() {
	systemctl is-active --quiet mc-backup.service
}

function fbackup_running() {
	systemctl is-active --quiet mc-fbackup.service
}

function server_backup() {
	force=$1

	if [ "$force" = "true" ]; then
		if backup_running; then
			echo "A backup is running. Aborting..."
			return
		fi
	else
		if fbackup_running; then
			echo "A force backup is running. Aborting..."
			return
		fi
	fi

	if server_running; then
		server_backup_safe "$force"
	else
		server_backup_unsafe
	fi

	exit
}

function ls_backups() {
	if [ $BACKUP_BACKEND = "bup" ]; then
		bup_ls
	elif [ $BACKUP_BACKEND = "borg" ]; then
		borg_ls
	else
		tar_ls
	fi
}

# TODO: replace dmenu with terminal-only option
function server_restore() {
	REMOTES=$( IFS=$'\n'; echo "${BACKUP_DIRS[*]}" )
	REMOTE=$(echo "$REMOTES" | dmenu -l 30 -p "Select a remote to get snapshot from")
	if [[ ! "$REMOTES" == *"${REMOTE}"* ]] || [ ! "$REMOTE" ] ; then
		echo "No remote selected, abort"
		return 1
	fi

	SNAPSHOTS=$(
	if [ $BACKUP_BACKEND = "bup" ]; then
		bup_ls_remote "$REMOTE"
	elif [ $BACKUP_BACKEND = "borg" ]; then
		borg_ls_remote "$REMOTE"
	else
		tar_ls_remote "$REMOTE"
	fi
	)
	SNAPSHOT=$(echo "$SNAPSHOTS" | dmenu -l 30 -p "Select a snapshot to recover")
	if [[ ! "$SNAPSHOTS" == *"${SNAPSHOT}"* ]] || [ ! "$SNAPSHOT" ] ; then
		echo "No snapshot selected, abort"
		return 1
	fi

	echo "Restoring snapshot \"$SNAPSHOT\" from remote \"$REMOTE\""

	OLDWORLD_NAME=""
	if [[ -d "$WORLD_NAME" ]]; then
		echo "bup: Saving old world, just in case"
		OLDWORLD_NAME="${WORLD_NAME}.old.$(date +'%F_%H-%M-%S')"
		mv -v "$WORLD_NAME" "$OLDWORLD_NAME"
	fi

	if [ $BACKUP_BACKEND = "bup" ]; then
		bup_restore "$REMOTE" "$SNAPSHOT"
	elif [ $BACKUP_BACKEND = "borg" ]; then
		borg_restore "$REMOTE" "$SNAPSHOT"
	else
		tar_restore "$REMOTE" "$SNAPSHOT"
	fi

	if [ ! $? -eq 0 ]; then
		echo "Restore failed, reverting old world back"
		rm -r "$WORLD_NAME"
		mv -v "$OLDWORLD_NAME" "$WORLD_NAME"
	else
		echo "Restore finished"
	fi
}

#cd $(dirname $0)

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