tracksolid_timescale_grafan.../backup/entrypoint.sh

66 lines
2 KiB
Bash
Raw Permalink Normal View History

#!/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