This commit is contained in:
Adrian Amaglio 2023-02-03 19:31:24 +01:00
parent 7ed4949630
commit f65caf2b6b
4 changed files with 72 additions and 2 deletions

16
Dockerfile Normal file
View File

@ -0,0 +1,16 @@
FROM alpine
RUN apk add --no-cache curl sqlite
RUN rm /etc/crontabs/root
RUN mkdir /usr/local/app
RUN adduser -D -h /usr/local/app backup
WORKDIR /usr/local/app
COPY entrypoint.sh backup.sh reload.sh initdb.sql /usr/local/app/
ENV USER=backup
ENTRYPOINT /usr/local/app/entrypoint.sh

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
set -euo pipefail
@ -14,7 +14,7 @@ function extract() {
usage "$0 <key>"
exit 1
fi
echo "select $1 from files where id=$id" | sqlite3 db.sqlite
echo "select $1 from files where id=$id" | sqlite3 db/db.sqlite
}
# Extract variables from database

51
entrypoint.sh Executable file
View File

@ -0,0 +1,51 @@
#!/bin/sh
set -u
# This script run as root (as crond needs to be root).
# But the crontab user is different so the work is done by user $USER
echo "The cronjobs will be run as user '$USER'"
while true ; do
# Database init if needed
if [ ! -e "db/db.sqlite" ] ; then
mkdir db
touch db/db.sqlite
chown "$USER:$USER" db -R
su "$USER" -c "sqlite3 db/db.sqlite" < initdb.sql
fi
cronfile="/etc/crontabs/$USER"
echo -n '' > "$cronfile"
tmp="$(mktemp)"
echo 'select id,crontab_time from files' | sqlite3 db/db.sqlite > "$tmp"
while read res ; do
id="$(echo "$res" | cut -d '|' -f 1)"
cron_raw="$(echo "$res" | cut -d '|' -f 2)"
# Trim
cron_raw="$(echo "$cron_raw" | sed -e 's/^[[:space:]]\+//' -e 's/[[:space:]]\+$//')"
# only get valid cron times
cron="$(echo "$cron_raw " | grep -oE '^(([[:digit:]]+|\*)[[:space:]]+){3}$')"
if [ -z "$cron" ] ; then
echo "Invalid cron times for id=$id : '$cron_raw'"
continue
fi
echo "$id 5 $cron backup.sh $id" >> "$cronfile"
done <"$tmp"
rm "$tmp"
# Reglementary empty line at the end
echo '' >> "$cronfile"
# run cron and log
date >> /var/log/cron.log
crond -L /var/log/cron.log &
tail -f /var/log/cron.log
# Clean
killall crond
done

3
reload.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
killall tail