forked from TapeWerm/MCscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcbe_autoupdate.sh
executable file
·80 lines (74 loc) · 2.37 KB
/
mcbe_autoupdate.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
#!/usr/bin/env bash
# Exit if error
set -e
syntax='Usage: mcbe_autoupdate.sh [OPTION]... SERVER_DIR'
args=$(getopt -l help,service: -o hs: -- "$@")
eval set -- "$args"
while [ "$1" != -- ]; do
case $1 in
--help|-h)
echo "$syntax"
echo "If SERVER_DIR/version isn't the same as the ZIP in ~mc, back up, update, and restart service of Minecraft Bedrock Edition server. If there's no service, make and chown mc SERVER_DIR."
echo
echo Mandatory arguments to long options are mandatory for short options too.
echo '-s, --service=SERVICE service of Minecraft Bedrock Edition server'
exit
;;
--service|-s)
service=$2
shift 2
;;
esac
done
shift
if [ "$#" -lt 1 ]; then
>&2 echo Not enough arguments
>&2 echo "$syntax"
exit 1
elif [ "$#" -gt 1 ]; then
>&2 echo Too much arguments
>&2 echo "$syntax"
exit 1
fi
server_dir=$(realpath "$1")
# cat fails if there's no file $server_dir/version
installed_ver=$(cat "$server_dir/version" 2> /dev/null || true)
# There might be more than one ZIP in ~mc
# ls fails if there's no match
minecraft_zip=$(find ~mc/bedrock-server-*\.zip 2> /dev/null | xargs -0d '\n' ls -t | head -n 1 || true)
# Trim off $minecraft_zip after last .zip
current_ver=$(basename "${minecraft_zip%.zip}")
if [ -n "$service" ]; then
if ! systemctl is-active --quiet "$service"; then
>&2 echo "Service $service not active"
exit 1
fi
fi
# Trim off $service before last @
instance=${service##*@}
if [ -n "$service" ]; then
if [ "$installed_ver" = fail ]; then
echo Previous update failed, rm "$server_dir/version" and try again
exit 1
elif [ "$installed_ver" != "$current_ver" ]; then
trap 'echo fail > "$server_dir/version"' ERR
sudo systemctl start "mcbe-backup@$instance"
sudo systemctl stop "$service.socket"
trap 'sudo systemctl start "$service"' ERR
# mcbe_update.sh reads y asking if you stopped the server
sudo su mc -s /bin/bash -c "echo y | ~/mcbe_update.sh $server_dir $minecraft_zip"
sudo systemctl start "$service"
fi
else
if [ -d "$server_dir" ]; then
>&2 echo "Server directory $server_dir already exists"
exit 1
fi
# Test extracting $minecraft_zip partially quietly
unzip -tq "$minecraft_zip"
trap 'sudo rm -rf "$server_dir"' ERR
sudo unzip -q "$minecraft_zip" -d "$server_dir"
echo "$current_ver" > "$server_dir/version"
sudo chown -R mc:nogroup "$server_dir"
echo "@@@ Don't forget to edit $server_dir/server.properties @@@"
fi