Compare commits

...

2 commits

Author SHA1 Message Date
4b738fbcb7 Merge PR #11: backup TZ → Africa/Nairobi
Some checks failed
Tests / test (push) Has been cancelled
Static Analysis / static (push) Has been cancelled
2026-04-24 08:31:24 +00:00
David Kiania
85cb408dea feat(backup): timestamp and schedule in Africa/Nairobi local time
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
- 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
4 changed files with 32 additions and 25 deletions

View file

@ -8,6 +8,9 @@ RUN apk add --no-cache \
bash \
coreutils
# Default TZ for timestamps and schedule interpretation. Override via compose env.
ENV TZ=Africa/Nairobi
WORKDIR /app
COPY backup_db.sh /app/backup_db.sh
COPY entrypoint.sh /app/entrypoint.sh

View file

@ -18,7 +18,7 @@ PGHOST="${PGHOST:-timescale_db}"
PGPORT="${PGPORT:-5432}"
KEEP_DAYS="${BACKUP_KEEP_DAYS:-30}"
TS="$(date -u +%Y%m%d_%H%M%SZ)"
TS="$(date +%Y%m%d_%H%M%S_%Z)"
FILE="${POSTGRES_DB}_${TS}.sql.gz"
TMP="/tmp/${FILE}"
@ -27,22 +27,22 @@ export AWS_SECRET_ACCESS_KEY="$RUSTFS_SECRET_KEY"
export AWS_DEFAULT_REGION="${RUSTFS_REGION:-us-east-1}"
export PGPASSWORD="$POSTGRES_PASSWORD"
echo "[$(date -u +%FT%TZ)] pg_dump ${POSTGRES_DB}@${PGHOST} -> ${FILE}"
echo "[$(date +%FT%T%z)] pg_dump ${POSTGRES_DB}@${PGHOST} -> ${FILE}"
pg_dump -h "$PGHOST" -p "$PGPORT" -U "$POSTGRES_USER" -d "$POSTGRES_DB" \
--no-owner --no-privileges --format=plain \
| gzip -9 > "$TMP"
SIZE=$(wc -c < "$TMP")
echo "[$(date -u +%FT%TZ)] dump size: ${SIZE} bytes"
echo "[$(date +%FT%T%z)] dump size: ${SIZE} bytes"
KEY="daily/${FILE}"
echo "[$(date -u +%FT%TZ)] uploading s3://${RUSTFS_BUCKET}/${KEY}"
echo "[$(date +%FT%T%z)] uploading s3://${RUSTFS_BUCKET}/${KEY}"
aws --endpoint-url "$RUSTFS_ENDPOINT" s3 cp "$TMP" "s3://${RUSTFS_BUCKET}/${KEY}"
rm -f "$TMP"
# Prune anything older than KEEP_DAYS.
CUTOFF="$(date -u -d "-${KEEP_DAYS} days" +%Y%m%d 2>/dev/null || date -u -v -"${KEEP_DAYS}"d +%Y%m%d)"
CUTOFF="$(date -d "-${KEEP_DAYS} days" +%Y%m%d 2>/dev/null || date -v -"${KEEP_DAYS}"d +%Y%m%d)"
aws --endpoint-url "$RUSTFS_ENDPOINT" s3 ls "s3://${RUSTFS_BUCKET}/daily/" \
| awk '{print $4}' \
| while read -r OBJ; do
@ -50,9 +50,9 @@ aws --endpoint-url "$RUSTFS_ENDPOINT" s3 ls "s3://${RUSTFS_BUCKET}/daily/" \
OBJ_DATE=$(echo "$OBJ" | sed -n 's/.*_\([0-9]\{8\}\)_.*/\1/p')
[ -z "$OBJ_DATE" ] && continue
if [ "$OBJ_DATE" -lt "$CUTOFF" ]; then
echo "[$(date -u +%FT%TZ)] prune s3://${RUSTFS_BUCKET}/daily/${OBJ}"
echo "[$(date +%FT%T%z)] prune s3://${RUSTFS_BUCKET}/daily/${OBJ}"
aws --endpoint-url "$RUSTFS_ENDPOINT" s3 rm "s3://${RUSTFS_BUCKET}/daily/${OBJ}"
fi
done
echo "[$(date -u +%FT%TZ)] backup complete"
echo "[$(date +%FT%T%z)] backup complete"

View file

@ -1,13 +1,15 @@
#!/bin/sh
# Runs backup_db.sh at each time in BACKUP_TIMES_UTC (comma-separated HH:MM list).
# Defaults: 02:30, 08:30, 14:30, 20:30 UTC — four backups per day.
# 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: if BACKUP_TIMES_UTC is unset but legacy BACKUP_HOUR/BACKUP_MINUTE are,
# those are honored as a single slot.
# Back-compat: legacy BACKUP_TIMES_UTC or BACKUP_HOUR/BACKUP_MINUTE are still honored.
set -eu
if [ -n "${BACKUP_TIMES_UTC:-}" ]; then
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}")"
@ -15,22 +17,23 @@ else
TIMES="02:30,08:30,14:30,20:30"
fi
echo "[$(date -u +%FT%TZ)] backup schedule (UTC): ${TIMES}"
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 -u +%FT%TZ)] BACKUP_RUN_ON_START=1 — running backup immediately"
/app/backup_db.sh || echo "[$(date -u +%FT%TZ)] initial backup failed (continuing)"
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 UTC" on both GNU and BSD date.
# Compute epoch for "today HH:MM local" on both GNU and BSD date.
slot_to_epoch_today() {
HM="$1"
date -u -d "today ${HM}:00" +%s 2>/dev/null \
|| date -u -j -f "%H:%M:%S" "${HM}:00" +%s
date -d "today ${HM}:00" +%s 2>/dev/null \
|| date -j -f "%H:%M:%S" "${HM}:00" +%s
}
while true; do
NOW_EPOCH=$(date -u +%s)
NOW_EPOCH=$(date +%s)
NEXT=""
# Find the smallest TARGET > NOW across all configured slots (rolling to tomorrow if needed).
OLDIFS="$IFS"
@ -49,14 +52,14 @@ while true; do
IFS="$OLDIFS"
if [ -z "$NEXT" ]; then
echo "[$(date -u +%FT%TZ)] no valid times in BACKUP_TIMES_UTC='${TIMES}'; sleeping 1h"
echo "[$(date "$TS_FMT")] no valid times in BACKUP_TIMES='${TIMES}'; sleeping 1h"
sleep 3600
continue
fi
SLEEP=$((NEXT - NOW_EPOCH))
NEXT_ISO=$(date -u -d "@${NEXT}" +%FT%TZ 2>/dev/null || date -u -r "${NEXT}" +%FT%TZ)
echo "[$(date -u +%FT%TZ)] next backup in ${SLEEP}s (at ${NEXT_ISO})"
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 -u +%FT%TZ)] backup failed (will retry at next slot)"
/app/backup_db.sh || echo "[$(date "$TS_FMT")] backup failed (will retry at next slot)"
done

View file

@ -90,8 +90,9 @@ services:
env_file: .env
environment:
# pg_dump → rustfs. Credentials from .env (RUSTFS_*).
# BACKUP_TIMES_UTC: comma-separated HH:MM list. Default: 4×/day.
- BACKUP_TIMES_UTC=${BACKUP_TIMES_UTC:-02:30,08:30,14:30,20:30}
# BACKUP_TIMES: comma-separated HH:MM list in local TZ (default Africa/Nairobi).
- TZ=${TZ:-Africa/Nairobi}
- BACKUP_TIMES=${BACKUP_TIMES:-02:30,08:30,14:30,20:30}
- BACKUP_KEEP_DAYS=${BACKUP_KEEP_DAYS:-30}
- BACKUP_RUN_ON_START=${BACKUP_RUN_ON_START:-0}
- RUSTFS_ENDPOINT=${RUSTFS_ENDPOINT}