Skip to content

Commit

Permalink
Merge pull request #209 from Luatan/cron-update
Browse files Browse the repository at this point in the history
Cron update
  • Loading branch information
thijsvanloef authored Jan 31, 2024
2 parents 5e3472e + 9ed95d3 commit 124a8d4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ ENV PORT= \
TZ=UTC \
SERVER_DESCRIPTION= \
BACKUP_ENABLED=true \
BACKUP_CRON_EXPRESSION="0 0 * * *"
BACKUP_CRON_EXPRESSION="0 0 * * *" \
AUTO_UPDATE_ENABLED=false \
AUTO_UPDATE_CRON_EXPRESSION="0 * * * *" \
AUTO_UPDATE_WARN_MINUTES=30

COPY ./scripts/* /home/steam/server/
RUN chmod +x /home/steam/server/init.sh /home/steam/server/start.sh /home/steam/server/backup.sh && \
mv /home/steam/server/backup.sh /usr/local/bin/backup
RUN chmod +x /home/steam/server/init.sh /home/steam/server/start.sh /home/steam/server/backup.sh /home/steam/server/update.sh && \
mv /home/steam/server/backup.sh /usr/local/bin/backup && \
mv /home/steam/server/update.sh /usr/local/bin/update

WORKDIR /home/steam/server

Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ It is highly recommended you set the following environment values before startin
| BACKUP_ENABLED | Enables automatic backups | true | true/false |
| DELETE_OLD_BACKUPS | Delete backups after a certain number of days | false | true/false |
| OLD_BACKUP_DAYS | How many days to keep backups | 30 | any positive integer |
| AUTO_UPDATE_CRON_EXPRESSION | Setting affects frequency of automatic updates. | 0 \* \* \* \* | Needs a Cron-Expression - See [Configuring Automatic Backups with Cron](#configuring-automatic-backups-with-cron) |
| AUTO_UPDATE_ENABLED | Enables automatic updates | false | true/false |
| AUTO_UPDATE_WARN_MINUTES | How long to wait to update the server, after the player were informed. | 30 | !0 |

*highly recommended to set

Expand Down Expand Up @@ -261,6 +264,32 @@ BACKUP_CRON_EXPRESSION is a cron expression, in a Cron-Expression you define an
Set BACKUP_CRON_EXPRESSION to change the default schedule.
Example Usage: If BACKUP_CRON_EXPRESSION to `0 2 * * *`, the backup script will run every day at 2:00 AM.

## Configuring Automatic Updates with Cron

To be able to use automatic Updates with this Server the following environment variables **have** to be set to `true`:

* RCON_ENABLED
* UPDATE_ON_BOOT

> [!IMPORTANT]
>
> If docker restart is not set to policy `always` or `unless-stopped` then the server will shutdown and will need to be
> manually restarted.
>
> The example docker run command and docker compose file in [How to Use](#how-to-use) already use the needed policy
Set AUTO_UPDATE_ENABLED enable or disable automatic backups (Default is disabled)

AUTO_UPDATE_CRON_EXPRESSION is a cron expression, in a Cron-Expression you define an interval for when to run jobs.

> [!TIP]
> This image uses Supercronic for crons
> see [supercronic](https://github.com/aptible/supercronic#crontab-format)
> or
> [Crontab Generat](https://crontab-generator.org).
Set AUTO_UPDATE_CRON_EXPRESSION to change the default schedule.

## Editing Server Settings

### With Environment Variables
Expand Down
11 changes: 10 additions & 1 deletion scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,19 @@ if [ -n "${RCON_PORT}" ]; then
sed -i "s/RCONPort=[0-9]*/RCONPort=$RCON_PORT/" /palworld/Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
fi

rm -f "/home/steam/server/crontab"
if [ "${BACKUP_ENABLED}" = true ]; then
echo "BACKUP_ENABLED=${BACKUP_ENABLED}"

echo "$BACKUP_CRON_EXPRESSION bash /usr/local/bin/backup" > "/home/steam/server/crontab"
echo "$BACKUP_CRON_EXPRESSION bash /usr/local/bin/backup" >> "/home/steam/server/crontab"
fi

if [ "${AUTO_UPDATE_ENABLED}" = true ] && [ "${UPDATE_ON_BOOT}" = true ]; then
echo "AUTO_UPDATE_ENABLED=${AUTO_UPDATE_ENABLED}"
echo "$AUTO_UPDATE_CRON_EXPRESSION bash /usr/local/bin/update" >> "/home/steam/server/crontab"
fi

if { [ "${AUTO_UPDATE_ENABLED}" = true ] && [ "${UPDATE_ON_BOOT}" = true ]; } || [ "${BACKUP_ENABLED}" = true ]; then
supercronic "/home/steam/server/crontab" &
fi

Expand Down
38 changes: 38 additions & 0 deletions scripts/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

if [ "${UPDATE_ON_BOOT}" = false ]; then
echo "Update on Boot needs to be enabled for auto updating"
exit 0
fi

temp_file=$(mktemp)
http_code=$(curl https://api.steamcmd.net/v1/info/2394010 --output "$temp_file" --silent --location --write-out "%{http_code}")

CURRENTBUILD=$(awk '/buildid/{ print $2 }' < /palworld/steamapps/appmanifest_2394010.acf)
TARGETBUILD=$(grep -P '"public": {"buildid": "\d+"' -o <"$temp_file" | sed -r 's/.*("[0-9]+")$/\1/')
rm "$temp_file"

if [ "$http_code" -ne 200 ]; then
echo "There was a problem reaching the Steam api. Unable to check for updates!"
exit 1
fi

if [ -z "$TARGETBUILD" ]; then
echo "The server response does not contain the expected BuildID. Unable to check for updates!"
exit 1
fi

if [ "$CURRENTBUILD" != "$TARGETBUILD" ]; then
echo "New Build was found. Updating the server from $CURRENTBUILD to $TARGETBUILD."
if [ "${RCON_ENABLED}" = true ]; then
rm /palworld/steamapps/appmanifest_2394010.acf
rcon-cli -c /home/steam/server/rcon.yaml "broadcast The_Server_will_update_in_${AUTO_UPDATE_WARN_MINUTES}_Minutes"
sleep "${AUTO_UPDATE_WARN_MINUTES}m"
backup
rcon-cli -c /home/steam/server/rcon.yaml "shutdown 1"
else
echo "An update is available however auto updating without rcon is not supported"
fi
else
echo "The Server is up to date!"
fi

0 comments on commit 124a8d4

Please sign in to comment.