# Kvitt backup job image (SPEC §10.3, §10.9): alpine with sqlite, gzip, tar, rclone and backup.sh on a daily schedule.
# Versions resolved 2026-11-13 (D-215): alpine 3.24.4 (Docker Hub official image, multi-arch index digest below);
# packages from Alpine v3.24 main/community: sqlite 3.53.4, tar 1.35, zlib 1.4, tzdata 2026f, supercronic 0.2.49.
# rclone 1.76.0 is the official release binary, not Alpine's package: Alpine (v3.24 and edge) still ships 1.75.2,
# whose bundled github.com/andybalholm/brotli v1.2.2 has CVE-2026-111497 (exploited; Coolify's image scan blocks it).
# rclone 1.76.0 is built with brotli v1.2.7, golang.org/x/net v0.62.0, klauspost/compress v1.20.3. The SHA-256 of
# each zip is hard-coded below from rclone's SHA256SUMS for v1.76.0 (downloads.rclone.org, PGP-signed by Nick
# Craig-Wood, key FBF737ECE9F8AB18604BD2AC93935E02FF3B54FA; identical to the GitHub release's SHA256SUMS).
# zlib is lifted to 1.4 from v3.24 main (the base image carries 1.3.2).
# The scheduler is supercronic, not busybox crond: the container never runs as root (the owner's uid on the Pi,
# 10001 elsewhere; README "Container uid"), and busybox crond cannot run jobs as a non-root user (checked
# 2026-10-18). See D-104.
FROM docker.io/library/alpine:3.24.4@sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf AS rclone

ARG RCLONE_VERSION=v1.76.0
ARG RCLONE_SHA256_AMD64=b00f00b3266c3764413afe599d3ec316f6f43b3e0ea8a0bc1d186d2133e46df7
ARG RCLONE_SHA256_ARM64=ee169016675e23a69d77240565133ff7dbc9b33d16fa8f7053d4af39f1eaeb78
# Set by BuildKit, buildx and Podman; derived from the image's own architecture when a builder leaves it empty.
ARG TARGETARCH
RUN set -eu; \
    arch="${TARGETARCH:-}"; \
    if [ -z "$arch" ]; then \
      case "$(apk --print-arch)" in x86_64) arch=amd64 ;; aarch64) arch=arm64 ;; *) arch=unknown ;; esac; \
    fi; \
    case "$arch" in \
      amd64) sum="$RCLONE_SHA256_AMD64" ;; \
      arm64) sum="$RCLONE_SHA256_ARM64" ;; \
      *) echo "no pinned rclone for architecture $arch" >&2; exit 1 ;; \
    esac; \
    zip="rclone-$RCLONE_VERSION-linux-$arch.zip"; \
    wget -q -O "/tmp/$zip" "https://downloads.rclone.org/$RCLONE_VERSION/$zip"; \
    echo "$sum  /tmp/$zip" | sha256sum -c -; \
    unzip -q "/tmp/$zip" -d /tmp/rclone; \
    install -m 0755 "/tmp/rclone/rclone-$RCLONE_VERSION-linux-$arch/rclone" /usr/local/bin/rclone; \
    /usr/local/bin/rclone version | head -n 1

FROM docker.io/library/alpine:3.24.4@sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf

RUN apk add --no-cache \
      "sqlite~3.53" \
      "tar~1.35" \
      "supercronic~0.2" \
      "zlib~1.4" \
      gzip \
      tzdata \
 && adduser -D -u 10001 -g "Kvitt backup" kvitt \
 && mkdir -p /backups /data \
 && chown 10001:10001 /backups /data

COPY --from=rclone /usr/local/bin/rclone /usr/local/bin/rclone
COPY backup.sh healthcheck.sh /
RUN chmod 0755 /backup.sh /healthcheck.sh \
 && echo '30 3 * * * /backup.sh' > /etc/kvitt-backup.crontab

ENV TZ=Europe/Stockholm
# Runs as uid/gid 10001 by default, the server image's uid, so sqlite3 .backup can open the WAL files the server owns
# (D-105) where compose sets no user: (the Coolify stack, D-214). The Pi and development stacks set user: themselves.
USER 10001:10001
# Daily at 03:30 Europe/Stockholm; /tmp/kvitt-started feeds the healthcheck's "no run yet since start".
# The development stack runs it on demand instead (`docker compose run --rm backup /backup.sh`, see infra/README.md).
CMD ["sh", "-c", "date +%s > /tmp/kvitt-started && exec supercronic -quiet /etc/kvitt-backup.crontab"]
