-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
start.sh
230 lines (201 loc) · 9.68 KB
/
start.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
#!/bin/bash
#Minecraft Bedrock Docker server startup script using screen
#Author: James A. Chambers - https://jamesachambers.com/legendary-minecraft-bedrock-container/
#GitHub Repository: https://github.com/TheRemote/Legendary-Bedrock-Container
echo "Minecraft Bedrock Server Docker script by James A. Chambers"
echo "Latest version always at https://github.com/TheRemote/Legendary-Bedrock-Container"
echo "Don't forget to set up port forwarding on your router! The default port is 19132"
if [ ! -d '/minecraft' ]; then
echo "ERROR: A named volume was not specified for the minecraft server data. Please create one with: docker volume create yourvolumename"
echo "Please pass the new volume to docker like this: docker run -it -v yourvolumename:/minecraft"
exit 1
fi
Terminal=$(readlink /proc/self/fd/0)
if [ -z "$Terminal" ] || [ "$Terminal" != "/dev/pts/0" ]; then
echo "An interactive terminal is required (you don't have to attach). Please run with docker -it or docker -itd (detached interactive terminal)."
sleep 10
exit 1
fi
# Randomizer for user agent
RandNum=$(echo $((1 + $RANDOM % 5000)))
if [ -z "$PortIPV4" ]; then
PortIPV4="19132"
fi
if [ -z "$PortIPV6" ]; then
PortIPV6="19133"
fi
echo "Ports used - IPV4: $PortIPV4 - IPV6: $PortIPV6"
# Check if server is already started
ScreenWipe=$(screen -wipe 2>&1)
if screen -list | grep -q "\.minecraftbe"; then
echo "Server is already started! Press screen -r minecraftbe to open it"
exit 1
fi
# Change directory to server directory
cd /minecraft
# If clean switch is specified then remove downloads and version_installed.txt
if [ -n "$Clean" ]; then
echo "Cleaning enabled, removing downloads and version_installed.txt..."
rm -rf /minecraft/downloads
rm -f /minecraft/version_installed.txt
fi
# Create logs/backups/downloads folder if it doesn't exist
if [ ! -d "/minecraft/logs" ]; then
mkdir -p /minecraft/logs
fi
if [ ! -d "/minecraft/downloads" ]; then
mkdir -p /minecraft/downloads
fi
if [ ! -d "/minecraft/backups" ]; then
mkdir -p /minecraft/backups
fi
# Check if network interfaces are up
NetworkChecks=0
if [ -e '/sbin/route' ]; then
DefaultRoute=$(/sbin/route -n | awk '$4 == "UG" {print $2}')
else
DefaultRoute=$(route -n | awk '$4 == "UG" {print $2}')
fi
while [ -z "$DefaultRoute" ]; do
echo "Network interface not up, will try again in 1 second"
sleep 1
if [ -e '/sbin/route' ]; then
DefaultRoute=$(/sbin/route -n | awk '$4 == "UG" {print $2}')
else
DefaultRoute=$(route -n | awk '$4 == "UG" {print $2}')
fi
NetworkChecks=$((NetworkChecks + 1))
if [ $NetworkChecks -gt 20 ]; then
echo "Waiting for network interface to come up timed out - starting server without network connection ..."
break
fi
done
# Take ownership of server files and set correct permissions
if [ -z "$NoPermCheck" ]; then
echo "Taking ownership of all server files/folders in /minecraft..."
sudo -n chown -R $(whoami) /minecraft >/dev/null 2>&1
echo "Complete"
else
echo "Skipping permissions check due to NoPermCheck flag"
fi
# Create backup
if [ -d "worlds" ]; then
echo "Backing up server (to minecraftbe/backups folder)"
if [ -n "$(which pigz)" ]; then
echo "Backing up server (multiple cores) to minecraftbe/backups folder"
tar -I pigz -pvcf backups/$(date +%Y.%m.%d.%H.%M.%S).tar.gz worlds
else
echo "Backing up server (single cored) to minecraftbe/backups folder"
tar -pzvcf backups/$(date +%Y.%m.%d.%H.%M.%S).tar.gz worlds
fi
fi
# Rotate backups -- keep most recent 10
if [ -e /minecraft/backups ]; then
Rotate=$(
pushd /minecraft/backups
ls -1tr | head -n -$BackupCount | xargs -d '\n' rm -f --
popd
)
fi
# Retrieve latest version of Minecraft Bedrock dedicated server
echo "Checking for the latest version of Minecraft Bedrock server ..."
# Test internet connectivity first
if [ -z "$QuietCurl" ]; then
curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -s https://www.minecraft.net/ -o /dev/null
else
curl --no-progress-meter -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -s https://www.minecraft.net/ -o /dev/null
fi
if [ "$?" != 0 ]; then
echo "Unable to connect to update website (internet connection may be down). Skipping update ..."
else
# Download server index.html to check latest version
if [ -z "$QuietCurl" ]; then
curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -o /minecraft/downloads/version.html https://www.minecraft.net/en-us/download/server/bedrock
else
curl --no-progress-meter -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -o /minecraft/downloads/version.html https://www.minecraft.net/en-us/download/server/bedrock
fi
LatestURL=$(grep -o 'https://www.minecraft.net/bedrockdedicatedserver/bin-linux/[^"]*' downloads/version.html)
LatestFile=$(echo "$LatestURL" | sed 's#.*/##')
echo "Latest version online is $LatestFile"
if [ -n "$Version" ]; then
PinFile="bedrock-server-$Version.zip"
echo "Override version, using version specified: $Version as $PinFile"
fi
if [ -e version_installed.txt ]; then
InstalledFile=$(cat version_installed.txt)
echo "Current install is: $InstalledFile"
fi
if [[ "$PinFile" == *"zip" ]] && [[ "$InstalledFile" == "$PinFile" ]]; then
echo "Requested version $PinFile is already installed"
elif [ ! -z "$PinFile" ]; then
echo "Installing $PinFile"
DownloadFile=$PinFile
DownloadURL="https://www.minecraft.net/bedrockdedicatedserver/bin-linux/$PinFile"
# Download version of Minecraft Bedrock dedicated server if it's not already local
if [ ! -f "downloads/$DownloadFile" ]; then
if [ -z "$QuietCurl" ]; then
curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -o "downloads/$DownloadFile" "$DownloadURL"
else
curl --no-progress-meter -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -o "downloads/$DownloadFile" "$DownloadURL"
fi
fi
# Install version of Minecraft requested
if [ ! -z "$DownloadFile" ]; then
unzip -o "downloads/$DownloadFile" -x "*server.properties*" "*permissions.json*" "*whitelist.json*" "*valid_known_packs.json*" "*allowlist.json*"
Permissions=$(chmod u+x /minecraft/bedrock_server >/dev/null)
echo "$DownloadFile" >/minecraft/version_installed.txt
fi
elif [[ "$InstalledFile" == "$LatestFile" ]]; then
echo "Latest version $LatestFile is already installed"
else
echo "Installing $LatestFile"
DownloadFile=$LatestFile
DownloadURL=$LatestURL
# Download version of Minecraft Bedrock dedicated server if it's not already local
if [ ! -f "downloads/$DownloadFile" ]; then
if [ -z "$QuietCurl" ]; then
curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -o "downloads/$DownloadFile" "$DownloadURL"
else
curl --no-progress-meter -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.$RandNum.212 Safari/537.36" -o "downloads/$DownloadFile" "$DownloadURL"
fi
fi
# Install version of Minecraft requested
if [ ! -z "$DownloadFile" ]; then
unzip -o "downloads/$DownloadFile" -x "*server.properties*" "*permissions.json*" "*whitelist.json*" "*valid_known_packs.json*" "*allowlist.json*"
Permissions=$(chmod u+x /minecraft/bedrock_server >/dev/null)
echo "$DownloadFile" >/minecraft/version_installed.txt
fi
fi
fi
if [ ! -e /minecraft/server.properties ]; then
cp -rf /scripts/server.properties /minecraft/server.properties
fi
if [ ! -e /minecraft/allowlist.json ]; then
cp -rf /scripts/allowlist.json /minecraft/allowlist.json
fi
if [ ! -e /minecraft/permissions.json ]; then
cp -rf /scripts/permissions.json /minecraft/permissions.json
fi
# Change ports in server.properties
sed -i "/server-port=/c\server-port=$PortIPV4" /minecraft/server.properties
sed -i "/server-portv6=/c\server-portv6=$PortIPV6" /minecraft/server.properties
# Start server
echo "Starting Minecraft server..."
CPUArch=$(uname -m)
if [[ "$CPUArch" == *"x86_64"* ]]; then
BASH_CMD="./bedrock_server"
elif [[ "$CPUArch" == *"aarch64"* ]]; then
if [ -z "$UseQEMU" ]; then
BASH_CMD="box64 bedrock_server"
else
BASH_CMD="qemu-x86_64-static bedrock_server"
fi
else
BASH_CMD="qemu-x86_64-static bedrock_server"
fi
if command -v gawk &>/dev/null; then
BASH_CMD+=$' | gawk \'{ print strftime(\"[%Y-%m-%d %H:%M:%S]\"), $0 }\''
else
echo "gawk application was not found -- timestamps will not be available in the logs"
fi
exec /bin/bash -c "${BASH_CMD}"