-
Notifications
You must be signed in to change notification settings - Fork 0
/
enshrd_backupgrade
executable file
·132 lines (119 loc) · 4.93 KB
/
enshrd_backupgrade
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
#!/bin/bash
# Upgrade Server on new release (https://api.steamcmd.net/v1/info/2278520)
# Backup savegame (after shutdown)
#
### v0.1 - enshrouded server update/reboot script
#
# crontab @4AM every day
# 0 4 * * * /usr/local/bin/enshrd_backupgrade
_USERDIR="/home/enshrouded" # Enshrouded User directory
_STEAMCMD="${_USERDIR}/steamcmd/steamcmd.sh" # SteamCMD script location
_WKGDIR="${_USERDIR}/enshrd-monitor" # Monitoring Directory used by this toolset
_SAVEBKPDIR="${_USERDIR}/enshrd-bak" # Backup Folder for Savegames
_GAMEDIR="${_USERDIR}/enshroudedserver" # Enshrouded Server Game Directory
_LOGFILE="${_WKGDIR}/update.log" # Log file for this script
_LOCKFILE="/tmp/.enshrd_updater.lock" # lockfile location
_SERVICEFILE="enshrd.service" # Enshrouded Service name
# Optional Proton Update with "proton-update" script
_PROTON_UPDATE="" # Set to 'true" if Proton is used
### PRE
_LOGTHIS(){
local _LEVEL="$1"
local _MESSAGE="$2"
local _TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
printf "[${_TIMESTAMP}] [${_LEVEL}] ${_MESSAGE}\n" >>$_LOGFILE
}
[ -x "$_STEAMCMD" ] || { _LOGTHIS FAIL "SteamCMD not Found.."; exit 1; }
[ -d "${_GAMEDIR}/savegame" ] || { _LOGTHIS FAIL "Enshrouded 'savegame' dir not found.."; exit 1; }
[ -d "$_WKGDIR" ] || mkdir -p $_WKGDIR
[ -d "$_SAVEBKPDIR" ] || mkdir -p $_SAVEBKPDIR
# lockfile
if [ -e "$_LOCKFILE" ]; then
_PID=$(cat ${_LOCKFILE})
ps -p $_PID -o pid= >/dev/null 2>&1
if [ $? -eq 0 ]; then
_LOGTHIS FAIL "Process is already running.."
exit
fi
fi
echo $$ >$_LOCKFILE
trap 'rm -f "$_LOCKFILE"' EXIT
# check query script presence
type enshrd_query >/dev/null || { _LOGTHIS FAIL "Script 'enshrd_query' not found in PATH"; exit 1; }
# SteamCMD update function
_ENSHRDUPDATE(){
_LOGTHIS INFO "Starting SteamCMD Game Update"
_LOGTHIS INFO "SteamCMD Log: ${_WKGDIR}/steamcmd-last-update.log"
$_STEAMCMD +@sSteamCmdForcePlatformType windows +force_install_dir $_GAMEDIR +login anonymous +app_update 2278520 +quit > ${_WKGDIR}/steamcmd-last-update.log && \
_LOGTHIS SUCCESS "SteamCMD Update Success!" || \
_LOGTHIS FAIL "SteamCMD Update Failed.."
}
### Base Informations collect
#_LASTGAMEUPDATE=$(date -d @$(curl -s https://api.steamcmd.net/v1/info/2278520 | jq '.data."2278520".depots.branches.public.timeupdated' |tr -d '"'))
# loop to get SteamCMD last update date
_MAX_RETRIES=5
_RETRY_INTERVAL=5 # seconds
attempt=0
while [ $attempt -lt $_MAX_RETRIES ]; do
response=$(curl -s https://api.steamcmd.net/v1/info/2278520)
if [ $? = 0 ]; then
timeupdated=$(echo "$response" | jq '.data."2278520".depots.branches.public.timeupdated' | tr -d '"')
if [ -n "$timeupdated" ]; then
_LASTGAMEUPDATE=$(date -d @$timeupdated)
break
fi
fi
attempt=$(($attempt+1))
sleep $_RETRY_INTERVAL
done
if [ $attempt -eq $_MAX_RETRIES ]; then
_LASTGAMEUPDATE=$(date +'%s' --date="-1 week")
fi
_CURRENTVERSION=$(grep SVN ${_GAMEDIR}/logs/enshrouded_server.log | awk '{print $NF}')
# Main
_LOGTHIS START "Enshrouded server Backup / Update started"
_LOGTHIS INFO "Current version: $_CURRENTVERSION"
# Abort if player are connected
if [ "$(enshrd_query -l -n)" = "0" -o "$(enshrd_query -n -l)" = "" ]; then
_LOGTHIS INFO "Active User(s) detected: '0'"
else
_LOGTHIS WARN "Active User(s) detected: $(cat ${_WKGDIR}/connected-users), abort.."
exit 1
fi
# stop service and backup map
_LOGTHIS INFO "Stopping service: $_SERVICEFILE"
sudo systemctl stop $_SERVICEFILE
sleep 5
# Clean logs and monitoring files
>${_WKGDIR}/last_check
>${_WKGDIR}/log_news
>${_WKGDIR}/connected-users
>${_GAMEDIR}/logs/enshrouded_server.log
# Backup Savegame folder
_LOGTHIS INFO "Backup Map files: ${_SAVEBKPDIR}/Enshrouded_Map-$(date +%d%m%Y-%H%M).zip"
zip -q -r ${_SAVEBKPDIR}/Enshrouded-map_$(date +%d%m%Y-%H%M).zip ${_GAMEDIR}/savegame/*
# Update Core Game files only if steam game repo have been updated within last 24h (as this script is meant to run every day)
_LOGTHIS INFO "Steam Core Game Repo last update: $_LASTGAMEUPDATE"
if [ "$(date -d "$_LASTGAMEUPDATE" +'%s')" -ge "$(date +'%s' --date="-1 day")" ]; then
_ENSHRDUPDATE
else
_LOGTHIS INFO "Steam Repo Check: Update not needed"
fi
# Proton Update if set
if [ "$_PROTON_UPDATE" = "true" ]; then
if type proton-update >/dev/null 2>&1; then
proton-update || { _LOGTHIS END_FAIL "Proton check failed, Server inconsistency.." && exit 1; }
else
_LOGTHIS WARN "Script 'proton-update' not found: skip.."
fi
fi
# wait for server start
_LOGTHIS INFO "Restarting service: $_SERVICEFILE"
sudo systemctl start $_SERVICEFILE
counter="0"
while ! grep -q SVN ${_GAMEDIR}/logs/enshrouded_server.log; do
sleep 3
counter=$((counter+1))
[ "$counter" = 40 ] && _LOGTHIS END_FAIL "Service did not restart properly" && exit 1
done
_LOGTHIS END_SUCCESS "Enshrouded Server [Version:$(grep SVN ${_GAMEDIR}/logs/enshrouded_server.log | awk '{print $NF}')] - Backup/Update Complete! "