tracksolid_timescale_grafan.../backup/entrypoint.sh
David Kiania 85cb408dea
Some checks failed
Static Analysis / static (push) Has been cancelled
Tests / test (push) Has been cancelled
Static Analysis / static (pull_request) Has been cancelled
Tests / test (pull_request) Has been cancelled
feat(backup): timestamp and schedule in Africa/Nairobi local time
- Default TZ=Africa/Nairobi baked into the sidecar image; override via
  compose TZ env var if another region is ever needed.
- Rename BACKUP_TIMES_UTC → BACKUP_TIMES (legacy var still honored for
  back-compat). Times are now interpreted in the container's local TZ,
  so "02:30" means 02:30 EAT, not UTC.
- Log timestamps and dump filenames use %FT%T%z / %Y%m%d_%H%M%S_%Z
  (e.g. tracksolid_db_20260424_115729_EAT.sql.gz) so the TZ is visible
  on every artifact.
- Prune cutoff computed in local time; YYYYMMDD regex unchanged so it
  still matches legacy UTC filenames during the transition.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 11:30:20 +03:00

65 lines
2 KiB
Bash
Executable file

#!/bin/sh
# Runs backup_db.sh at each time in BACKUP_TIMES (comma-separated HH:MM list).
# Times are interpreted in the container's local TZ (set via TZ env; default
# Africa/Nairobi in the Dockerfile). Defaults: 02:30, 08:30, 14:30, 20:30 local.
#
# Back-compat: legacy BACKUP_TIMES_UTC or BACKUP_HOUR/BACKUP_MINUTE are still honored.
set -eu
if [ -n "${BACKUP_TIMES:-}" ]; then
TIMES="$BACKUP_TIMES"
elif [ -n "${BACKUP_TIMES_UTC:-}" ]; then
TIMES="$BACKUP_TIMES_UTC"
elif [ -n "${BACKUP_HOUR:-}" ] || [ -n "${BACKUP_MINUTE:-}" ]; then
TIMES="$(printf '%02d:%02d' "${BACKUP_HOUR:-2}" "${BACKUP_MINUTE:-30}")"
else
TIMES="02:30,08:30,14:30,20:30"
fi
TS_FMT='+%FT%T%z'
echo "[$(date "$TS_FMT")] TZ=${TZ:-UTC} backup schedule: ${TIMES}"
if [ "${BACKUP_RUN_ON_START:-0}" = "1" ]; then
echo "[$(date "$TS_FMT")] BACKUP_RUN_ON_START=1 — running backup immediately"
/app/backup_db.sh || echo "[$(date "$TS_FMT")] initial backup failed (continuing)"
fi
# Compute epoch for "today HH:MM local" on both GNU and BSD date.
slot_to_epoch_today() {
HM="$1"
date -d "today ${HM}:00" +%s 2>/dev/null \
|| date -j -f "%H:%M:%S" "${HM}:00" +%s
}
while true; do
NOW_EPOCH=$(date +%s)
NEXT=""
# Find the smallest TARGET > NOW across all configured slots (rolling to tomorrow if needed).
OLDIFS="$IFS"
IFS=','
for HM in $TIMES; do
HM="$(echo "$HM" | tr -d ' ')"
[ -z "$HM" ] && continue
T=$(slot_to_epoch_today "$HM")
if [ "$T" -le "$NOW_EPOCH" ]; then
T=$((T + 86400))
fi
if [ -z "$NEXT" ] || [ "$T" -lt "$NEXT" ]; then
NEXT="$T"
fi
done
IFS="$OLDIFS"
if [ -z "$NEXT" ]; then
echo "[$(date "$TS_FMT")] no valid times in BACKUP_TIMES='${TIMES}'; sleeping 1h"
sleep 3600
continue
fi
SLEEP=$((NEXT - NOW_EPOCH))
NEXT_ISO=$(date -d "@${NEXT}" "$TS_FMT" 2>/dev/null || date -r "${NEXT}" "$TS_FMT")
echo "[$(date "$TS_FMT")] next backup in ${SLEEP}s (at ${NEXT_ISO})"
sleep "$SLEEP"
/app/backup_db.sh || echo "[$(date "$TS_FMT")] backup failed (will retry at next slot)"
done