/
debian-tmux
183 lines (163 loc) · 5.27 KB
/
debian-tmux
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
#!/bin/bash
# /etc/init.d/minecraft
# version 0.0.1 2014-03-18 (YYY-MM-DD)
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft Server
# Description: Starts the minecraft server
### END INIT INFO
# Variables
SRV_PATH='/srv/minecraft/' ## location of minecraft service folder, LEAVE THE / ON THE END
JAR_FILE='forge.jar' ## location of minecraft jar, in relation to SRV_PATH
JAR_OPTS='nogui' ## custom operators for java
SRV_NAME='ftb' ## name for service, used for tmux session
USER='minecraft' ## user to run service as
WORLD='world/' ## world dir, in relation to SRV_PATH
FULL_BACKUP_DIR='/srv/backup/minecraft.full' ## location of backup dir, leave blank to disable, supports rsync over ssh
DIFF_BACKUP_DIR='/srv/backup/minecraft' ## location of differential backup dir
CPUS=2 ## number of cores for java to use
MAX_RAM='6192M' ## maximum memory allowed for java
RES_RAM='1024M' ## starting ram for java
JAVA_OPTS="-Xmx${MAX_RAM} -Xms${RES_RAM} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=${CPUS}" ##all options for java
AS_USER="$(which sudo) -u ${USER}" ## save this for later, we're gonna be using it a lot
## Functions
check_running(){
if $(which pgrep) -u $USER -f $JAR_FILE > /dev/null ## check if process exists for jar file
then
return 0 ## when process is found
else
return 1 ## when process is not found, ie server not running
fi
}
send_command(){
$AS_USER $(which tmux) send -t $SRV_NAME "$1" ENTER ## run tmux as minecraft user, and send command followed by newline
}
attach_console(){
if check_running ## see if it's running
then
$AS_USER $(which tmux) attach -t $SRV_NAME ## attach to tmux session
else
echo "Connection failed, server is not running"
fi
}
disable_save(){
if check_running ## make sure the server is running
then
send_command "say Prepare your anuses, time for a backup!"
send_command "save-off" ## turn off auto saving, otherwise this is gonna break
send_command "save-all" ## Horse save current state
sync ## flush fs buffer, because reasons
sleep 2 ## wait a bit, give stuff time to catch up
fi
}
enable_save(){
if check_running ## make sure server is running first
then
send_command "save-on" ## turn auto saving back on
send_command "say And relax, backup complete!"
fi
}
start_server(){
echo -n "Starting ${SRV_NAME}: " ## self explanatory
if check_running ## is it already running?
then
echo "Failed, ${SRV_NAME} already running" ## it was already running.
else
cd $SRV_PATH ## get to where minecraft is
echo "i'm in" $SRV_PATH
$AS_USER $(which tmux) new -d -s ${SRV_NAME} "$(which java) ${JAVA_OPTS} -jar ${JAR_FILE} ${JAR_OPTS}" ## apparently, this runs the jar, looks like a mess to me...
echo "got here now"
sleep 5 ## give the server a chance to start, might wanna up this with lots of plugins
if check_running ## did it work?
then
echo "Success" ## yay, it worked!
else
echo "Failed, check the logs!" ## uh-oh, something went wrong, not our fault!
fi
fi
}
stop_server(){
echo -n "Stopping ${SRV_NAME}: " ## do i really need to explain this?
if check_running ## see if we can actually stop it
then
send_command "say Oh lordy, it's a shutdown!" ## warn everyone
sleep 3 ## let people see the message
send_command "stop" ## stop the server
sleep 5 ## minecraft is slow, wait for it to shut down
if check_running ## did we actually stop it?
then
echo "Failed, try killing the process yourself?" ## Well, this is bad...
else
echo "Success" ## Huzzah, we managed to stop it, aren't you proud?
fi
else
echo "Failed, ${SRV_NAME} not running" ## how can you make a phone call, if you cannot speak?
fi
}
backup_world(){
echo -n "Backing up World: "
if [ ! -z $BACKUP_DIR ] ## see if there is a backup dir set
then
if [ -d $BACKUP_DIR ]
then
disable_save ## turn of saving, otherwise chaos will ensue
$AS_USER $(which rsync) --archive --quiet ${SRV_PATH}${WORLD} $BACKUP_DIR ## rsync, without telling the user, sneaky sneaky
enable_save ## turn saving back on, we're done now
echo "Success"
else
echo "Failed - Backup directory doesn't exist!"
fi
else
echo "Failed, no backup dir set!" ## this was an option, if you remember
fi
}
diff_backup_world(){
echo -n "Performing differential backup: "
if [ ! -z $DIFF_BACKUP_DIR ]
then
if [ -d $DIFF_BACKUP_DIR ]
then
disable_save
$AS_USER $(which rdiff-backup) ${SRV_PATH}${WORLD} $DIFF_BACKUP_DIR
enable_save
echo "Success"
else
echo "Failed - Differential backup dir does not exist!"
fi
else
echo "Failed, no differential backup dir set!"
fi
}
case "$1" in
start)
start_server ;; ## start the server, if arg1 was start
stop)
stop_server ;; ## come on, this is obvious
restart)
stop_server
sleep 1
start_server ;; ## do you really need help with this?
backup)
backup_world ;;
diff_backup)
diff_backup_world ;;
console)
attach_console ;;
status)
if check_running
then
echo "${SRV_NAME} is running"
else
echo "${SRV_NAME} is not running"
fi ;;
*)
echo "Usage: /etc/init.d/minecraft {start|stop|restart|backup|diff_backup|console|status}"
exit 1 ;;
esac
exit 0