forked from TheRemote/MinecraftBedrockServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupMinecraft.sh
executable file
·298 lines (260 loc) · 11.6 KB
/
SetupMinecraft.sh
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
#!/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/truaddict/MinecraftBedrockServer/master/SetupMinecraft.sh
# chmod +x SetupMinecraft.sh
# ./SetupMinecraft.sh
#
# GitHub Repository: https://github.com/truaddict/MinecraftBedrockServer
echo "Forked version of Minecraft Bedrock Server installation script by James Chambers - July 24th 2019"
echo "Latest version always at https://github.com/truaddict/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
}
# 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
sudo apt-get install libcurl4 -y
sudo apt-get install openssl -y
# Check to see if Minecraft server main directory already exists
cd ~
if [ ! -d "mcsrv" ]; then
mkdir mcsrv
cd mcsrv
fi
# Server name configuration
echo "Enter a short one word label for a new or existing server..."
echo "It will be used in the folder name and service name..."
read_with_prompt ServerName "Server Label"
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 mcsrv/$ServerName already exists! Updating scripts and configuring service ..."
# Get Home directory path and username
DirName=$(readlink -e ~)
UserName=$(whoami)
cd ~
cd mcsrv
cd $ServerName
echo "Server directory is: $DirName/mcsrv/$ServerName"
# Remove existing scripts
rm start.sh stop.sh restart.sh
# Download start.sh from repository
echo "Grabbing start.sh from repository..."
wget -O start.sh https://raw.githubusercontent.com/truaddict/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/truaddict/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/truaddict/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 watchdog.sh from repository
echo "Grabbing watchdog.sh from repository..."
wget -O watchdog.sh https://raw.githubusercontent.com/truaddict/MinecraftBedrockServer/master/watchdog.sh
chmod +x watchdog.sh
sed -i "s:dirname:$DirName:g" watchdog.sh
sed -i "s:servername:$ServerName:g" watchdog.sh
# Update minecraft server service
echo "Configuring $ServerName service..."
sudo wget -O /etc/systemd/system/$ServerName.service https://raw.githubusercontent.com/truaddict/MinecraftBedrockServer/master/minecraftbe.service
sudo chmod +x /etc/systemd/system/$ServerName.service
sudo sed -i "s/replace/$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/mcsrv/$ServerName/restart.sh backup"
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 (~/mcsrv/$ServerName)..."
cd ~
cd mcsrv
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"
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. Downloading alternative..."
if [[ "$CPUArch" == *"armv7"* || "$CPUArch" == *"armhf"* ]]; then
wget http://ftp.us.debian.org/debian/pool/main/q/qemu/qemu-user-static_3.1+dfsg-8_armhf.deb
wget http://ftp.us.debian.org/debian/pool/main/b/binfmt-support/binfmt-support_2.2.0-2_armhf.deb
sudo dpkg --install binfmt*.deb
sudo dpkg --install qemu-user*.deb
elif [[ "$CPUArch" == *"aarch64"* || "$CPUArch" == *"arm64"* ]]; then
wget http://ftp.us.debian.org/debian/pool/main/q/qemu/qemu-user-static_3.1+dfsg-8_arm64.deb
wget http://ftp.us.debian.org/debian/pool/main/b/binfmt-support/binfmt-support_2.2.0-2_arm64.deb
sudo dpkg --install binfmt*.deb
sudo dpkg --install qemu-user*.deb
fi
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/truaddict/MinecraftBedrockServer/master/depends.zip
unzip depends.zip
sudo mkdir /lib64
# Create soft link ld-linux-x86-64.so.2 mapped to ld-2.28.so
sudo ln -s ~/mcsrv/$ServerName/ld-2.28.so /lib64/ld-linux-x86-64.so.2
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/truaddict/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/truaddict/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/truaddict/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 watchdog.sh from repository
echo "Grabbing watchdog.sh from repository..."
wget -O watchdog.sh https://raw.githubusercontent.com/truaddict/MinecraftBedrockServer/master/watchdog.sh
chmod +x watchdog.sh
sed -i "s:dirname:$DirName:g" watchdog.sh
sed -i "s:servername:$ServerName:g" watchdog.sh
# Service configuration
echo "Configuring Minecraft $ServerName service..."
sudo wget -O /etc/systemd/system/$ServerName.service https://raw.githubusercontent.com/truaddict/MinecraftBedrockServer/master/minecraftbe.service
sudo chmod +x /etc/systemd/system/$ServerName.service
sudo sed -i "s/replace/$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/mcsrv/$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