use go-cron

Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
This commit is contained in:
Shakar Bakr 2025-10-29 16:56:47 +03:00
parent 7db86e3d72
commit 3c9af7f6c5
No known key found for this signature in database
GPG Key ID: DA55A26823AE3C28
2 changed files with 13 additions and 16 deletions

View File

@ -21,6 +21,15 @@ RUN curl -O https://download.microsoft.com/download/b/9/f/b9f3cce4-3925-46d4-9f4
apk add --allow-untrusted mssql-tools18_18.1.1.1-1_amd64.apk && \ apk add --allow-untrusted mssql-tools18_18.1.1.1-1_amd64.apk && \
rm msodbcsql18_18.1.1.1-1_amd64.apk mssql-tools18_18.1.1.1-1_amd64.apk rm msodbcsql18_18.1.1.1-1_amd64.apk mssql-tools18_18.1.1.1-1_amd64.apk
# Install go-cron for scheduled backups
# Source: https://github.com/ivoronin/go-cron
ARG TARGETARCH
RUN curl -L https://github.com/ivoronin/go-cron/releases/download/v0.0.5/go-cron_0.0.5_linux_${TARGETARCH}.tar.gz -O && \
tar xvf go-cron_0.0.5_linux_${TARGETARCH}.tar.gz && \
rm go-cron_0.0.5_linux_${TARGETARCH}.tar.gz && \
mv go-cron /usr/local/bin/go-cron && \
chmod +x /usr/local/bin/go-cron
RUN rm -rf /var/cache/apk/* RUN rm -rf /var/cache/apk/*
ENV PATH="${PATH}:/opt/mssql-tools18/bin" ENV PATH="${PATH}:/opt/mssql-tools18/bin"

View File

@ -11,22 +11,10 @@ fi
if [ -z "$SCHEDULE" ]; then if [ -z "$SCHEDULE" ]; then
sh backup.sh sh backup.sh
else else
# For non-root users, use a writable directory for crontabs
# busybox crond supports -c option to specify crontab directory
CRON_USER=$(id -u)
CRON_DIR="${HOME}/crontabs"
# Create crontab directory
mkdir -p "$CRON_DIR"
# Write crontab entry
echo "$SCHEDULE /bin/sh $(pwd)/backup.sh" > "$CRON_DIR/$CRON_USER"
chmod 600 "$CRON_DIR/$CRON_USER"
echo "Backup schedule configured: $SCHEDULE" echo "Backup schedule configured: $SCHEDULE"
echo "Crontab file: $CRON_DIR/$CRON_USER" echo "Starting go-cron..."
echo "Starting crond..."
# Start crond in foreground mode with custom crontab directory # Use go-cron to run backup.sh on the specified schedule
exec crond -f -d 8 -c "$CRON_DIR" # go-cron takes schedule and command as arguments
exec go-cron "$SCHEDULE" /bin/sh "$(pwd)/backup.sh"
fi fi