#!/bin/sh # Entry point for the `backup` sidecar container's periodic loop. Runs # db-backup.sh on a fixed interval (default: daily) -- a sleep loop instead of # a cron daemon, kept deliberately simple so it works in a bare # postgres:16-alpine image. # # Resolves db-backup.sh the same way entrypoint.sh resolves this file: prefer # the live bind-mounted copy at /scripts (so edits don't need a rebuild), fall # back to the copy baked into the image at build time if the mount is # missing, empty, or stale. Resolving fresh on every loop iteration also means # that if the mount comes back healthy later (e.g. someone fixes the host # directory) this container picks it up on the very next run, with no # restart needed. set -eu resolve() { # $1 = script filename, e.g. db-backup.sh if [ -f "/scripts/$1" ]; then echo "/scripts/$1" else echo "/app/scripts-default/$1" fi } INTERVAL="${BACKUP_INTERVAL_SECONDS:-86400}" # 86400 = once a day echo "[backup] sidecar started; interval=${INTERVAL}s, keep=${BACKUP_KEEP:-14}, dir=${BACKUP_DIR:-/backups}" # Take one backup shortly after start so a freshly-deployed stack has an # immediate restore point instead of waiting a whole interval. sleep 20 while true; do DB_BACKUP="$(resolve db-backup.sh)" sh "$DB_BACKUP" || echo "[backup] run failed; will retry next interval" >&2 sleep "$INTERVAL" done