From 918c32c623b1d04b70ab646e14715406f3ade918 Mon Sep 17 00:00:00 2001 From: Guillaume Barreau Date: Thu, 25 Jan 2024 07:09:30 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Add=20variables=20Env=20for?= =?UTF-8?q?=20cron=20feature?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 7 ++++--- README.md | 34 ++++++++++++++++++++++------------ docker-compose.yml | 8 ++++++-- scripts/backup.sh | 13 +++++++++---- scripts/init.sh | 2 -- scripts/start.sh | 17 +++++++++++++++-- 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/Dockerfile b/Dockerfile index f8e52f9..6d64723 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ @@ -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 diff --git a/README.md b/README.md index f7b8587..a0215e5 100644 --- a/README.md +++ b/README.md @@ -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 + +**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. + +**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 cron /etc/cron.d/backups-cron - +DAYS_TO_KEEP=7 +BACKUP_CRON_EXPRESSION="0 2 * * *" +BACKUP_ENABLED=true ``` -Additionally, to stop the cron process within the Docker container, you can use the following command: - -```bash -docker exec palworld-server service cron stop -``` - -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: `/Pal/Saved/Config/LinuxServer/PalWorldSettings.ini` diff --git a/docker-compose.yml b/docker-compose.yml index 7dc7136..0665c1e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: @@ -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" diff --git a/scripts/backup.sh b/scripts/backup.sh index 29be637..712a0db 100644 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -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 @@ -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 {} \; \ No newline at end of file +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 + + diff --git a/scripts/init.sh b/scripts/init.sh index 32a7840..f1b5bf2 100644 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -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 diff --git a/scripts/start.sh b/scripts/start.sh index 411e522..8e0cc1b 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,7 +1,5 @@ #!/bin/bash -printf "\e[0;34m***** RUNNING SCRIPTS start.sh *****\e[0m\n" - STARTCOMMAND="./PalServer.sh" if [ -n "${PORT}" ]; then @@ -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 <