Skip to content

Commit

Permalink
feat: ✨ Add variables Env for cron feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Barreau committed Jan 25, 2024
1 parent 735ae33 commit 918c32c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 22 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ RUN mv rcon-cli /usr/bin/rcon-cli
RUN rm -f /var/run/crond.pid
COPY ./scripts/backup.sh /usr/local/bin/backup
RUN chmod +x /usr/local/bin/backup
RUN echo "0 3 * * * root bash /usr/local/bin/backup" > /etc/cron.d/backups-cron
RUN chmod 0644 /etc/cron.d/backups-cron

ENV PORT= \
PUID=1000 \
Expand All @@ -32,7 +30,10 @@ ENV PORT= \
UPDATE_ON_BOOT=true \
RCON_ENABLED=true \
RCON_PORT=25575 \
QUERY_PORT=27015
QUERY_PORT=27015 \
BACKUP_ENABLED=true \
DAYS_TO_KEEP=7 \
BACKUP_CRON_EXPRESSION="0 0 * * *"

COPY ./scripts/* /home/steam/server/
RUN chmod +x /home/steam/server/init.sh /home/steam/server/start.sh /home/steam/server/backup.sh
Expand Down
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,33 @@ This will create a backup at `/palworld/backups/`

## Setting Up Automatic Backups with Cron

This section guides you through the process of configuring automatic backups using Cron. Use the provided command to set up the necessary Cron job for your Palworld server within a Docker environment.
Setting Up Automatic Backups with Cron

The configured backup system automatically deletes backups older than 7 days to manage storage efficiently.
### Environment Variables

```bash
docker exec palworld-server cron /etc/cron.d/backups-cron
**DAYS_TO_KEEP:**

```
Description: This environment variable represents the number of days to retain backup files. It is used in the cleanup process to remove backup files older than the specified duration.
Example Usage: If set to 7, backup files older than 7 days will be deleted during the cleanup.

**BACKUP_CRON_EXPRESSION:**

Description: This environment variable defines the cron expression for scheduling automatic backups. It determines when the backup script should run at specific intervals.
Example Usage: If set to 0 2 * * *, the backup script will run every day at 2:00 AM.

Additionally, to stop the cron process within the Docker container, you can use the following command:
**BACKUP_ENABLED:**

Description: This environment variable indicates whether automatic backups are enabled or not. If set to a non-empty value, automatic backups will be enabled; otherwise, they will be disabled.
Example Usage: If set to any non-empty string (e.g., true or 1), the backup script will be configured and executed as per the specified cron schedule.

Example:

```bash
docker exec palworld-server service cron stop
DAYS_TO_KEEP=7
BACKUP_CRON_EXPRESSION="0 2 * * *"
BACKUP_ENABLED=true
```

This command stops the cron service within the specified container, halting the scheduled cron job.

## Editing Server Settings

When the server starts, a `PalWorldSettings.ini` file will be created in the following location: `<mount_folder>/Pal/Saved/Config/LinuxServer/PalWorldSettings.ini`
Expand Down
8 changes: 6 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
palworld:
image: thijsvanloef/palworld-server-docker:latest
image: test/palworld-server-docker:latest
build: .
restart: unless-stopped
container_name: palworld-server
ports:
Expand All @@ -15,7 +16,10 @@ services:
- RCON_ENABLED=true
- RCON_PORT=25575
- ADMIN_PASSWORD="adminPasswordHere"
- COMMUNITY=false # Enable this if you want your server to show up in the community servers tab, USE WITH SERVER_PASSWORD!
- COMMUNITY=false
- BACKUP_ENABLED=true
- DAYS_TO_KEEP=7
- BACKUP_CRON_EXPRESSION=0 0 * * *
# Enable the environment variables below if you have COMMUNITY=true
# - SERVER_PASSWORD="worldofpals"
# - SERVER_NAME="World of Pals"
Expand Down
13 changes: 9 additions & 4 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/bin/bash

printf "\e[0;34m***** RUNNING SCRIPTS backup.sh *****\e[0m\n"

DATE=$(date +"%Y-%m-%d_%H-%M-%S")
DESTINATION_PATH="/palworld/backups"
FILE_PATH="${DESTINATION_PATH}/backup_palworld_${DATE}.tar.gz"

if [ ! -f ${FILE_PATH} ]; then
printf "\e[0;32m***** CREATING BACKUPS FOLDER *****\e[0m\n"
echo "\e[0;32m***** CREATING BACKUPS FOLDER *****\e[0m\n"
mkdir -p "${DESTINATION_PATH}"
fi

Expand All @@ -16,4 +14,11 @@ cd /palworld/Pal/ || exit
tar -zcf "$FILE_PATH" "Saved/"
echo "backup created at $FILE_PATH"

find "${DESTINATION_PATH}" -type f -name "backup_palworld_*.tar.gz" -ctime +7 -exec rm -f {} \;
if [[ -n "${DAYS_TO_KEEP}" && "${DAYS_TO_KEEP}" =~ ^[0-9]+$ ]]; then
echo "DAYS_TO_KEEP=${DAYS_TO_KEEP}"
find "${DESTINATION_PATH}" -type f -mtime +"${DAYS_TO_KEEP}" -exec rm {} \;
else
echo "DAYS_TO_KEEP is not a valid number."
fi


2 changes: 0 additions & 2 deletions scripts/init.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash

printf "\e[0;34m***** RUNNING SCRIPTS init.sh *****\e[0m\n"

if [[ ! "${PUID}" -eq 0 ]] && [[ ! "${PGID}" -eq 0 ]]; then
printf "\e[0;32m*****EXECUTING USERMOD*****\e[0m\n"
usermod -o -u "${PUID}" steam
Expand Down
17 changes: 15 additions & 2 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash

printf "\e[0;34m***** RUNNING SCRIPTS start.sh *****\e[0m\n"

STARTCOMMAND="./PalServer.sh"

if [ -n "${PORT}" ]; then
Expand Down Expand Up @@ -69,6 +67,21 @@ if [ -n "${RCON_PORT}" ]; then
sed -i "s/RCONPort=[0-9]*/RCONPort=$RCON_PORT/" /palworld/Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
fi

if [[ -n "${BACKUP_ENABLED}" ]]; then
echo "BACKUP_ENABLED=${BACKUP_ENABLED}"

if [[ -z "${BACKUP_CRON_EXPRESSION}" ]]; then
printf "\e[0;31m***** BACKUP_CRON_EXPRESSION NOT SET *****\e[0m\n"
exit 1
else
echo "BACKUP_CRON_EXPRESSION=${BACKUP_CRON_EXPRESSION}"
fi

echo "${BACKUP_CRON_EXPRESSION} root bash /usr/local/bin/backup" > /etc/cron.d/backups-cron
chmod 0644 /etc/cron.d/backups-cron
cron /etc/cron.d/backups-cron
fi

# Configure RCON settings
cat >~/.rcon-cli.yaml <<EOL
host: localhost
Expand Down

0 comments on commit 918c32c

Please sign in to comment.