forked from TheRemote/MinecraftBedrockServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupMinecraft.sh
More file actions
executable file
·356 lines (311 loc) · 13.8 KB
/
SetupMinecraft.sh
File metadata and controls
executable file
·356 lines (311 loc) · 13.8 KB
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
#!/bin/bash
# Minecraft Server Installation Script - James A. Chambers - https://jamesachambers.com
#
# Instructions: https://jamesachambers.com/minecraft-bedrock-edition-ubuntu-dedicated-server-guide/
# To run the setup script use:
# wget https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/SetupMinecraft.sh
# chmod +x SetupMinecraft.sh
# ./SetupMinecraft.sh
#
# GitHub Repository: https://github.com/TheRemote/MinecraftBedrockServer
echo "Minecraft Bedrock Server installation script by James Chambers"
echo "Latest version always at https://github.com/TheRemote/MinecraftBedrockServer"
echo "Don't forget to set up port forwarding on your router! The default port is 19132"
# Function to read input from user with a prompt
function read_with_prompt {
variable_name="$1"
prompt="$2"
default="${3-}"
unset $variable_name
while [[ ! -n ${!variable_name} ]]; do
read -p "$prompt: " $variable_name < /dev/tty
if [ ! -n "`which xargs`" ]; then
declare -g $variable_name=$(echo "${!variable_name}" | xargs)
fi
declare -g $variable_name=$(echo "${!variable_name}" | head -n1 | awk '{print $1;}')
if [[ -z ${!variable_name} ]] && [[ -n "$default" ]] ; then
declare -g $variable_name=$default
fi
echo -n "$prompt : ${!variable_name} -- accept (y/n)?"
read answer < /dev/tty
if [ "$answer" == "${answer#[Yy]}" ]; then
unset $variable_name
else
echo "$prompt: ${!variable_name}"
fi
done
}
# Check to make sure we aren't being ran as root
if [ $(id -u) = 0 ]; then
echo "This script is not meant to be ran as root or sudo. Please run normally with ./SetupMinecraft.sh. If you know what you are doing and want to override this edit this check out of SetupMinecraft.sh. Exiting..."
exit 1
fi
# Install dependencies required to run Minecraft server in the background
echo "Installing screen, unzip, sudo, net-tools, wget.."
if [ ! -n "`which sudo`" ]; then
apt-get update && apt-get install sudo -y
fi
sudo apt-get update
sudo apt-get install screen unzip wget -y
sudo apt-get install net-tools -y
echo "Installing curl and libcurl.."
sudo apt-get install curl -y
sudo apt-get install libcurl4 -y
# Install libcurl3 for backwards compatibility in case libcurl4 isn't available
sudo apt-get install libcurl3 -y
echo "Installing openssl, libc6 and libcrypt1.."
sudo apt-get install openssl -y
sudo apt-get install libc6 -y
sudo apt-get install libcrypt1 -y
# Check to see if Minecraft server main directory already exists
cd ~
if [ ! -d "minecraftbe" ]; then
mkdir minecraftbe
cd minecraftbe
else
cd minecraftbe
if [ -f "bedrock_server" ]; then
echo "Migrating old Bedrock server to minecraftbe/old"
cd ~
mv minecraftbe old
mkdir minecraftbe
mv old minecraftbe/old
cd minecraftbe
echo "Migration complete to minecraftbe/old"
fi
fi
# Server name configuration
echo "Enter a short one word label for a new or existing server (don't use minecraftbe)..."
echo "It will be used in the folder name and service name..."
read_with_prompt ServerName "Server Label"
if [[ "$ServerName" == *"minecraftbe"* ]]; then
echo "Server label of minecraftbe is not allowed. Please choose a different server label!"
exit 1
fi
echo "Enter server IPV4 port (default 19132): "
read_with_prompt PortIPV4 "Server IPV4 Port" 19132
echo "Enter server IPV6 port (default 19133): "
read_with_prompt PortIPV6 "Server IPV6 Port" 19133
if [ -d "$ServerName" ]; then
echo "Directory minecraftbe/$ServerName already exists! Updating scripts and configuring service ..."
# Get Home directory path and username
DirName=$(readlink -e ~)
UserName=$(whoami)
cd ~
cd minecraftbe
cd $ServerName
echo "Server directory is: $DirName/minecraftbe/$ServerName"
# Remove existing scripts
rm start.sh stop.sh restart.sh fixpermissions.sh
# Download start.sh from repository
echo "Grabbing start.sh from repository..."
wget -O start.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/start.sh
chmod +x start.sh
sed -i "s:dirname:$DirName:g" start.sh
sed -i "s:servername:$ServerName:g" start.sh
# Download stop.sh from repository
echo "Grabbing stop.sh from repository..."
wget -O stop.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/stop.sh
chmod +x stop.sh
sed -i "s:dirname:$DirName:g" stop.sh
sed -i "s:servername:$ServerName:g" stop.sh
# Download restart.sh from repository
echo "Grabbing restart.sh from repository..."
wget -O restart.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/restart.sh
chmod +x restart.sh
sed -i "s:dirname:$DirName:g" restart.sh
sed -i "s:servername:$ServerName:g" restart.sh
# Download fixpermissions.sh from repository
echo "Grabbing fixpermissions.sh from repository..."
wget -O fixpermissions.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/fixpermissions.sh
chmod +x fixpermissions.sh
sed -i "s:dirname:$DirName:g" fixpermissions.sh
sed -i "s:servername:$ServerName:g" fixpermissions.sh
sed -i "s:userxname:$UserName:g" fixpermissions.sh
# Update minecraft server service
echo "Configuring Minecraft $ServerName service..."
sudo wget -O /etc/systemd/system/$ServerName.service https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/minecraftbe.service
sudo chmod +x /etc/systemd/system/$ServerName.service
sudo sed -i "s:userxname:$UserName:g" /etc/systemd/system/$ServerName.service
sudo sed -i "s:dirname:$DirName:g" /etc/systemd/system/$ServerName.service
sudo sed -i "s:servername:$ServerName:g" /etc/systemd/system/$ServerName.service
sed -i "/server-port=/c\server-port=$PortIPV4" server.properties
sed -i "/server-portv6=/c\server-portv6=$PortIPV6" server.properties
sudo systemctl daemon-reload
echo -n "Start Minecraft server at startup automatically (y/n)?"
read answer < /dev/tty
if [ "$answer" != "${answer#[Yy]}" ]; then
sudo systemctl enable $ServerName.service
# Automatic reboot at 4am configuration
echo -n "Automatically restart and backup server at 4am daily (y/n)?"
read answer < /dev/tty
if [ "$answer" != "${answer#[Yy]}" ]; then
croncmd="$DirName/minecraftbe/$ServerName/restart.sh"
cronjob="0 4 * * * $croncmd"
( crontab -l | grep -v -F "$croncmd" ; echo "$cronjob" ) | crontab -
echo "Daily restart scheduled. To change time or remove automatic restart type crontab -e"
fi
fi
# Setup completed
echo "Setup is complete. Starting Minecraft $ServerName server..."
sudo systemctl start $ServerName.service
# Sleep for 4 seconds to give the server time to start
sleep 4s
screen -r $ServerName
exit 0
fi
# Create server directory
echo "Creating minecraft server directory (~/minecraftbe/$ServerName)..."
cd ~
cd minecraftbe
mkdir $ServerName
cd $ServerName
mkdir downloads
mkdir backups
mkdir logs
# Check CPU archtecture to see if we need to do anything special for the platform the server is running on
echo "Getting system CPU architecture..."
CPUArch=$(uname -m)
echo "System Architecture: $CPUArch"
# Check for ARM architecture
if [[ "$CPUArch" == *"aarch"* || "$CPUArch" == *"arm"* ]]; then
# ARM architecture detected -- download QEMU and dependency libraries
echo "ARM platform detected -- installing dependencies..."
# Check if latest available QEMU version is at least 3.0 or higher
QEMUVer=$(apt-cache show qemu-user-static | grep Version | awk 'NR==1{ print $2 }' | cut -c3-3)
if [[ "$QEMUVer" -lt "3" ]]; then
echo "Available QEMU version is not high enough to emulate x86_64. Please update your QEMU version."
exit
else
sudo apt-get install qemu-user-static binfmt-support -y
fi
if [ -n "`which qemu-x86_64-static`" ]; then
echo "QEMU-x86_64-static installed successfully"
else
echo "QEMU-x86_64-static did not install successfully -- please check the above output to see what went wrong."
exit 1
fi
# Retrieve depends.zip from GitHub repository
wget -O depends.zip https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/depends.zip
unzip depends.zip
sudo mkdir /lib64
# Create soft link ld-linux-x86-64.so.2 mapped to ld-2.31.so
sudo ln -s ~/minecraftbe/$ServerName/ld-2.31.so /lib64/ld-linux-x86-64.so.2
fi
# Check for x86 (32 bit) architecture
if [[ "$CPUArch" == *"i386"* || "$CPUArch" == *"i686"* ]]; then
# ARM architecture detected -- download QEMU and dependency libraries
#echo "32 bit platform detected -- installing dependencies..."
# Check if latest available QEMU version is at least 3.0 or higher
#QEMUVer=$(apt-cache show qemu-user-static | grep Version | awk 'NR==1{ print $2 }' | cut -c3-3)
#if [[ "$QEMUVer" -lt "3" ]]; then
# echo "Available QEMU version is not high enough to emulate x86_64. Please update your QEMU version."
# exit
#else
# sudo apt-get install qemu-user-static binfmt-support -y
#fi
#if [ -n "`which qemu-x86_64-static`" ]; then
# echo "QEMU-x86_64-static installed successfully"
#else
# echo "QEMU-x86_64-static did not install successfully -- please check the above output to see what went wrong."
# exit 1
#fi
# Retrieve depends.zip from GitHub repository
#wget -O depends.zip https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/depends.zip
#unzip depends.zip
#sudo mkdir /lib64
# Create soft link ld-linux-x86-64.so.2 mapped to ld-2.31.so
#sudo ln -s ~/minecraftbe/$ServerName/ld-2.31.so /lib64/ld-linux-x86-64.so.2
# 32 bit attempts have not been successful -- notify user to install 64 bit OS
echo "You are running a 32 bit operating system (i386 or i686) and the Bedrock Dedicated Server has only been released for 64 bit (x86_64). If you have a 64 bit processor please install a 64 bit operating system to run the Bedrock dedicated server!"
exit 1
fi
# Retrieve latest version of Minecraft Bedrock dedicated server
echo "Checking for the latest version of Minecraft Bedrock server..."
wget -O downloads/version.html https://minecraft.net/en-us/download/server/bedrock/
DownloadURL=$(grep -o 'https://minecraft.azureedge.net/bin-linux/[^"]*' downloads/version.html)
DownloadFile=$(echo "$DownloadURL" | sed 's#.*/##')
echo "$DownloadURL"
echo "$DownloadFile"
# Download latest version of Minecraft Bedrock dedicated server
echo "Downloading the latest version of Minecraft Bedrock server..."
UserName=$(whoami)
DirName=$(readlink -e ~)
wget -O "downloads/$DownloadFile" "$DownloadURL"
unzip -o "downloads/$DownloadFile"
# Download start.sh from repository
echo "Grabbing start.sh from repository..."
wget -O start.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/start.sh
chmod +x start.sh
sed -i "s:dirname:$DirName:g" start.sh
sed -i "s:servername:$ServerName:g" start.sh
sed -i "s:userxname:$UserName:g" start.sh
# Download stop.sh from repository
echo "Grabbing stop.sh from repository..."
wget -O stop.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/stop.sh
chmod +x stop.sh
sed -i "s:dirname:$DirName:g" stop.sh
sed -i "s:servername:$ServerName:g" stop.sh
sed -i "s:userxname:$UserName:g" stop.sh
# Download restart.sh from repository
echo "Grabbing restart.sh from repository..."
wget -O restart.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/restart.sh
chmod +x restart.sh
sed -i "s:dirname:$DirName:g" restart.sh
sed -i "s:servername:$ServerName:g" restart.sh
sed -i "s:userxname:$UserName:g" restart.sh
# Download fixpermissions.sh from repository
echo "Grabbing fixpermissions.sh from repository..."
wget -O fixpermissions.sh https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/fixpermissions.sh
chmod +x fixpermissions.sh
sed -i "s:dirname:$DirName:g" fixpermissions.sh
sed -i "s:servername:$ServerName:g" fixpermissions.sh
sed -i "s:userxname:$UserName:g" fixpermissions.sh
# Service configuration
echo "Configuring Minecraft $ServerName service..."
sudo wget -O /etc/systemd/system/$ServerName.service https://raw.githubusercontent.com/TheRemote/MinecraftBedrockServer/master/minecraftbe.service
sudo chmod +x /etc/systemd/system/$ServerName.service
sudo sed -i "s:userxname:$UserName:g" /etc/systemd/system/$ServerName.service
sudo sed -i "s:dirname:$DirName:g" /etc/systemd/system/$ServerName.service
sudo sed -i "s:servername:$ServerName:g" /etc/systemd/system/$ServerName.service
sed -i "/server-port=/c\server-port=$PortIPV4" server.properties
sed -i "/server-portv6=/c\server-portv6=$PortIPV6" server.properties
sudo systemctl daemon-reload
echo -n "Start Minecraft server at startup automatically (y/n)?"
read answer < /dev/tty
if [ "$answer" != "${answer#[Yy]}" ]; then
sudo systemctl enable $ServerName.service
# Automatic reboot at 4am configuration
TimeZone=$(cat /etc/timezone)
CurrentTime=$(date)
echo "Your time zone is currently set to $TimeZone. Current system time: $CurrentTime"
echo "You can adjust/remove the selected reboot time later by typing crontab -e or running SetupMinecraft.sh again."
echo -n "Automatically restart and backup server at 4am daily (y/n)?"
read answer < /dev/tty
if [ "$answer" != "${answer#[Yy]}" ]; then
croncmd="$DirName/minecraftbe/$ServerName/restart.sh"
cronjob="0 4 * * * $croncmd"
( crontab -l | grep -v -F "$croncmd" ; echo "$cronjob" ) | crontab -
echo "Daily restart scheduled. To change time or remove automatic restart type crontab -e"
fi
fi
# Finished!
echo "Setup is complete. Starting Minecraft server..."
sudo systemctl start $ServerName.service
# Wait up to 20 seconds for server to start
StartChecks=0
while [ $StartChecks -lt 20 ]; do
if screen -list | grep -q "\.$ServerName"; then
break
fi
sleep 1;
StartChecks=$((StartChecks+1))
done
# Force quit if server is still open
if ! screen -list | grep -q "\.$ServerName"; then
echo "Minecraft server failed to start after 20 seconds."
else
echo "Minecraft server has started. Type screen -r $ServerName to view the running server!"
fi
# Attach to screen
screen -r $ServerName