-
Notifications
You must be signed in to change notification settings - Fork 6
/
ts3_updater.sh
307 lines (256 loc) · 9.39 KB
/
ts3_updater.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
299
300
301
302
303
304
305
306
307
#!/bin/bash
# DEBUG MODE
DEBUG="OFF"
# Author: Ulrich Block <ulrich.block@easy-wi.com>,
# Alexander Doerwald <alexander.doerwald@easy-wi.com>
#
# This file is part of Easy-WI.
#
# Easy-WI is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Easy-WI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Easy-WI. If not, see <http://www.gnu.org/licenses/>.
#
# Diese Datei ist Teil von Easy-WI.
#
# Easy-WI ist Freie Software: Sie koennen es unter den Bedingungen
# der GNU General Public License, wie von der Free Software Foundation,
# Version 3 der Lizenz oder (nach Ihrer Wahl) jeder spaeteren
# veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
#
# Easy-WI wird in der Hoffnung, dass es nuetzlich sein wird, aber
# OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
# Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
# Siehe die GNU General Public License fuer weitere Details.
#
# Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
# Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
# Backup Path/Folder
# if this Variable empty, then will all Backups saved in /home
BACKUP_PATH=""
##################################
##### Do not edit from here! #####
##################################
if [ "$DEBUG" = "ON" ]; then
set -x
fi
FINISHED() {
sleep 2
echo
echo
yellowMessage "Thanks for using this script and have a nice Day."
echo
if [ "$DEBUG" = "ON" ]; then
set +x
fi
exit 0
}
yellowMessage() {
echo -e "\\033[33;1m${@}\033[0m"
}
redMessage() {
echo -e "\\033[31;1m${@}\033[0m"
}
greenMessage() {
echo -e "\\033[32;1m${@}\033[0m"
}
cyanMessage() {
echo -e "\\033[36;1m${@}\033[0m"
}
CURRENT_SCRIPT_VERSION="1.6"
TMP_PATH="/tmp/teamspeak_backup"
BACKUP_FILES=("licensekey.dat" "serverkey.dat" "ts3server.sqlitedb" "query_ip_blacklist.txt" "query_ip_whitelist.txt" "ts3db_mariadb.ini" "ts3db_mysql.ini" "ts3server.ini" "ts3server_startscript.sh" ".bash_history" ".bash_logout" ".bashrc" ".profile")
BACKUP_DIR=("backup" "Backup" "backups" "logs" "files" ".ssh" ".config")
MACHINE=$(uname -m)
TS_MASTER_PATH_TMP=$(find /home -type f -name 'ts3server')
TS_USER=$(ls -la "$TS_MASTER_PATH_TMP" | awk '{print $3}')
TS_GROUP=$(ls -la "$TS_MASTER_PATH_TMP" | awk '{print $4}')
TS_MASTER_PATH=$(echo "$TS_MASTER_PATH_TMP" | sed 's/\/ts3server//')
if [ "$BACKUP_PATH" == "" ]; then
BACKUP_PATH="$TS_MASTER_PATH_TMP/Backup"
fi
if [ "$TS_MASTER_PATH_TMP" == "" ]; then
echo
redMessage "No teamspeak 3 server found!"
FINISHED
fi
yellowMessage "Checking for the latest Updater Script"
LATEST_SCRIPT_VERSION=$(curl -q https://raw.githubusercontent.com/easy-wi/server/master/ts3_updater.sh | egrep "CURRENT_SCRIPT_VERSION" | grep -Po '([0-9]\.[0-9])')
if [ "$LATEST_SCRIPT_VERSION" != "" ]; then
if [ $(printf "${LATEST_SCRIPT_VERSION}\n${CURRENT_SCRIPT_VERSION}" | sort -V | tail -n 1) != "$CURRENT_SCRIPT_VERSION" ]; then
echo
redMessage "You are using a old TS3 Updater Script Version ${CURRENT_SCRIPT_VERSION}."
redMessage "Please Upgrade to Version ${LATEST_SCRIPT_VERSION} and retry."
redMessage "Download Link: https://raw.githubusercontent.com/easy-wi/server/master/ts3_updater.sh"
FINISHED
else
greenMessage "You are using a Up-to-Date Script Version ${CURRENT_SCRIPT_VERSION}"
sleep 2
fi
else
redMessage "Could not detect last Script Version!"
FINISHED
fi
echo
yellowMessage "Checking for the latest TS3 Server Version"
if [ "$MACHINE" == "x86_64" ]; then
ARCH="amd64"
elif [ "$MACHINE" == "i386" ] || [ "$MACHINE" == "i686" ]; then
ARCH="x86"
else
echo "$MACHINE is not supported!"
fi
for LASTEST_TS3_VERSION in $(curl -s "https://www.teamspeak.com/versions/server.json" | grep -Po '[0-9]+(\.[0-9]+){2,3}' | sort -Vr); do
DOWNLOAD_URL_VERSION="https://files.teamspeak-services.com/releases/server/$LASTEST_TS3_VERSION/teamspeak3-server_linux_$ARCH-$LASTEST_TS3_VERSION.tar.bz2"
STATUS=$(curl -I $DOWNLOAD_URL_VERSION 2>&1 | grep "HTTP/" | awk '{print $2}')
if [ "$STATUS" == "200" ]; then
DOWNLOAD_URL=$DOWNLOAD_URL_VERSION
break
fi
done
LOCAL_TS3_VERSION=$(if [ -f "$TS_MASTER_PATH"/version ]; then cat "$TS_MASTER_PATH"/version; fi)
if [ "$LASTEST_TS3_VERSION" != "" ]; then
if [ "$LOCAL_TS3_VERSION" != "$LASTEST_TS3_VERSION" ]; then
redMessage "Your TS3 Server Version is deprecated."
redMessage "Start Update Process"
sleep 2
else
greenMessage "Your TS3 Server Version is Up-to-Date"
FINISHED
fi
else
redMessage "Could not detect latest TS3 Server Version!"
FINISHED
fi
echo
if [ "$TS_USER" != "" ]; then
USER_CHECK=$(cut -d: -f6,7 /etc/passwd | grep "$TS_USER" | head -n1)
if ([ "$USER_CHECK" != "/home/$TS_USER:/bin/bash" -a "$USER_CHECK" != "/home/$TS_USER/:/bin/bash" ]); then
redMessage "User $TS_USER not found or wrong shell rights!"
redMessage "Please check the TS_USER inside this Script or the user shell rights."
FINISHED
else
yellowMessage "Stop Server for Update..."
su - -c "$TS_MASTER_PATH/ts3server_startscript.sh stop" "$TS_USER" 2>&1 >/dev/null
sleep 10
ps -u "$TS_USER" | grep ts3server | awk '{print $1}' | while read PID; do
kill $PID
done
sleep 5
greenMessage "Done"
sleep 3
echo
yellowMessage "Make Backup..."
if [ ! -d "$TMP_PATH" ]; then
mkdir "$TMP_PATH"
else
rm -rf "$TMP_PATH"
mkdir "$TMP_PATH"
fi
for tmp_dir in ${BACKUP_DIR[@]}; do
if [ -d "$TS_MASTER_PATH"/"$tmp_dir" ]; then
cp "$TS_MASTER_PATH"/"$tmp_dir" -R "$TMP_PATH" 2>&1 >/dev/null
fi
done
for tmp_file in ${BACKUP_FILES[@]}; do
if [ -f "$TS_MASTER_PATH"/"$tmp_file" ]; then
cp "$TS_MASTER_PATH"/"$tmp_file" -R "$TMP_PATH"/ 2>&1 >/dev/null
fi
done
cd /tmp
DIR_SIZE=$(du --max-depth=0 "$TMP_PATH"/ | awk '{ print $1 }')
if [ "$DIR_SIZE" -ge "999000000" ]; then
tar cpvz ./teamspeak_backup | split -b1024m - Teamspeak_Backup.$(date -I).tar.gz.split.
if [ "$BACKUP_PATH" != "" -a -d "$BACKUP_PATH" ]; then
mv Teamspeak_Backup.*.tar.gz.split.* "$BACKUP_PATH"
else
mv Teamspeak_Backup.*.tar.gz.split.* /home
fi
else
tar cfvz Teamspeak_Backup.$(date -I).tar.gz ./teamspeak_backup
if [ "$BACKUP_PATH" != "" -a -d "$BACKUP_PATH" ]; then
mv Teamspeak_Backup.$(date -I).tar.gz "$BACKUP_PATH"
else
mv Teamspeak_Backup.$(date -I).tar.gz /home
fi
fi
sleep 2
greenMessage "Done"
sleep 3
echo
yellowMessage "Downloading TS3 Server Files..."
echo
wget --timeout=60 -P /tmp/ "$DOWNLOAD_URL"
if [ -f /tmp/teamspeak3-server_linux_"$ARCH"-"$LASTEST_TS3_VERSION".tar.bz2 ]; then
cd /tmp
tar xfj /tmp/teamspeak3-server_linux_"$ARCH"-"$LASTEST_TS3_VERSION".tar.bz2
rm -rf "$TS_MASTER_PATH"/*
mv /tmp/teamspeak3-server_linux_"$ARCH"/* "$TS_MASTER_PATH"
echo "$LASTEST_TS3_VERSION" >> "$TS_MASTER_PATH"/version
echo
sleep 3
yellowMessage "Restore TS3 Server Files..."
for tmp_dir in ${BACKUP_DIR[@]}; do
if [ -d "$TMP_PATH"/"$tmp_dir" ]; then
cp "$TMP_PATH"/"$tmp_dir" -R "$TS_MASTER_PATH"/
fi
done
if [ ! -d "$TS_MASTER_PATH"/logs ]; then
mkdir "$TS_MASTER_PATH"/logs
fi
for tmp_file in ${BACKUP_FILES[@]}; do
if [ -f "$TMP_PATH"/"$tmp_file" ]; then
rm -rf "$TS_MASTER_PATH"/"$tmp_file"
mv "$TMP_PATH"/"$tmp_file" "$TS_MASTER_PATH"/
fi
done
chown -cR "$TS_USER":"$TS_GROUP" "$TS_MASTER_PATH" 2>&1 >/dev/null
rm -rf /tmp/teamspeak3-server_linux_"$ARCH"-"$LASTEST_TS3_VERSION".tar.bz2
rm -rf /tmp/teamspeak3-server_linux_"$ARCH"
rm -rf "$TMP_PATH"
sleep 3
greenMessage "Done"
sleep 3
echo
yellowMessage "starting TS3 Server with ts3server_minimal_runscript.sh to Update Database..."
yellowMessage "Please do not cancel!"
echo
CHECK_MARIADB=$(if [ -f "$TS_MASTER_PATH"/ts3db_mariadb.ini ]; then grep "username=" "$TS_MASTER_PATH"/ts3db_mariadb.ini | sed 's/username=//'; fi)
CHECK_MSQL=$(if [ -f "$TS_MASTER_PATH"/ts3db_mysql.ini ]; then grep "username=" "$TS_MASTER_PATH"/ts3db_mysql.ini | sed 's/username=//'; fi)
if [ "$CHECK_MARIADB" != "" -o "$CHECK_MSQL" != "" ]; then
su - -c "ln -s $TS_MASTER_PATH/redist/libmariadb.so.2 $TS_MASTER_PATH/libmariadb.so.2" "$TS_USER"
su - -c "$TS_MASTER_PATH/ts3server_minimal_runscript.sh inifile=ts3server.ini 2>&1 | tee $TS_MASTER_PATH/logs/ts3server_minimal_start_$(date +%d-%m-%Y).log" "$TS_USER" &
else
su - -c "$TS_MASTER_PATH/ts3server_minimal_runscript.sh | tee $TS_MASTER_PATH/logs/ts3server_minimal_start_$(date +%d-%m-%Y).log" "$TS_USER" &
fi
sleep 80
ps -u "$TS_USER" | grep ts3server | awk '{print $1}' | while read PID; do
kill "$PID"
done
sleep 5
greenMessage "Done"
sleep 5
echo
yellowMessage "Start TS3 Server"
su - -c "$TS_MASTER_PATH/ts3server_startscript.sh start" "$TS_USER" 2>&1 >/dev/null
sleep 2
greenMessage "Done"
echo
greenMessage "Backups saved in $BACKUP_PATH"
else
redMessage "Download the last TS3 Files failed!"
FINISHED
fi
fi
else
redMessage 'Variable "TS_USER" are empty!'
FINISHED
fi