Compare commits
No commits in common. "c585e674821b0c0340071bccb8f51361119ea6fe" and "fa110f4313485a34ae6cc9ead165fbb7ad1050a7" have entirely different histories.
c585e67482
...
fa110f4313
2 changed files with 15 additions and 54 deletions
|
|
@ -1,62 +1,26 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# Runs backup_db.sh at each time in BACKUP_TIMES_UTC (comma-separated HH:MM list).
|
# Loops forever: sleeps until the next BACKUP_HOUR:BACKUP_MINUTE UTC, then runs backup_db.sh.
|
||||||
# Defaults: 02:30, 08:30, 14:30, 20:30 UTC — four backups per day.
|
# Defaults: 02:30 UTC nightly.
|
||||||
#
|
|
||||||
# Back-compat: if BACKUP_TIMES_UTC is unset but legacy BACKUP_HOUR/BACKUP_MINUTE are,
|
|
||||||
# those are honored as a single slot.
|
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
if [ -n "${BACKUP_TIMES_UTC:-}" ]; then
|
HOUR="${BACKUP_HOUR:-2}"
|
||||||
TIMES="$BACKUP_TIMES_UTC"
|
MINUTE="${BACKUP_MINUTE:-30}"
|
||||||
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
|
|
||||||
|
|
||||||
echo "[$(date -u +%FT%TZ)] backup schedule (UTC): ${TIMES}"
|
|
||||||
|
|
||||||
if [ "${BACKUP_RUN_ON_START:-0}" = "1" ]; then
|
if [ "${BACKUP_RUN_ON_START:-0}" = "1" ]; then
|
||||||
echo "[$(date -u +%FT%TZ)] BACKUP_RUN_ON_START=1 — running backup immediately"
|
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)"
|
/app/backup_db.sh || echo "[$(date -u +%FT%TZ)] initial backup failed (continuing)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Compute epoch for "today HH:MM UTC" 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
|
|
||||||
}
|
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
NOW_EPOCH=$(date -u +%s)
|
NOW_EPOCH=$(date -u +%s)
|
||||||
NEXT=""
|
TARGET=$(date -u -d "today ${HOUR}:${MINUTE}:00" +%s 2>/dev/null \
|
||||||
# Find the smallest TARGET > NOW across all configured slots (rolling to tomorrow if needed).
|
|| date -u -j -f "%H:%M:%S" "${HOUR}:${MINUTE}:00" +%s)
|
||||||
OLDIFS="$IFS"
|
if [ "$TARGET" -le "$NOW_EPOCH" ]; then
|
||||||
IFS=','
|
TARGET=$((TARGET + 86400))
|
||||||
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
|
fi
|
||||||
if [ -z "$NEXT" ] || [ "$T" -lt "$NEXT" ]; then
|
SLEEP=$((TARGET - NOW_EPOCH))
|
||||||
NEXT="$T"
|
echo "[$(date -u +%FT%TZ)] next backup in ${SLEEP}s (at $(date -u -d "@${TARGET}" +%FT%TZ 2>/dev/null || date -u -r "${TARGET}" +%FT%TZ))"
|
||||||
fi
|
|
||||||
done
|
|
||||||
IFS="$OLDIFS"
|
|
||||||
|
|
||||||
if [ -z "$NEXT" ]; then
|
|
||||||
echo "[$(date -u +%FT%TZ)] no valid times in BACKUP_TIMES_UTC='${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})"
|
|
||||||
sleep "$SLEEP"
|
sleep "$SLEEP"
|
||||||
/app/backup_db.sh || echo "[$(date -u +%FT%TZ)] backup failed (will retry at next slot)"
|
/app/backup_db.sh || echo "[$(date -u +%FT%TZ)] backup failed (will retry tomorrow)"
|
||||||
done
|
done
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,10 @@ services:
|
||||||
- POSTGRES_DB=${POSTGRES_DB}
|
- POSTGRES_DB=${POSTGRES_DB}
|
||||||
- POSTGRES_USER=${POSTGRES_USER}
|
- POSTGRES_USER=${POSTGRES_USER}
|
||||||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
|
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
|
||||||
# HA image's PGDATA is /home/postgres/pgdata/data, not /var/lib/postgresql/data.
|
|
||||||
# Mount the named volume there so data survives container rebuilds.
|
|
||||||
- PGDATA=/home/postgres/pgdata/data
|
|
||||||
ports:
|
ports:
|
||||||
- "5433:5432"
|
- "5433:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- timescale-data:/home/postgres/pgdata
|
- timescale-data:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
|
|
@ -89,9 +86,9 @@ services:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
env_file: .env
|
env_file: .env
|
||||||
environment:
|
environment:
|
||||||
# pg_dump → rustfs. Credentials from .env (RUSTFS_*).
|
# Nightly pg_dump → rustfs. Credentials from .env (RUSTFS_*).
|
||||||
# BACKUP_TIMES_UTC: comma-separated HH:MM list. Default: 4×/day.
|
- BACKUP_HOUR=${BACKUP_HOUR:-2}
|
||||||
- BACKUP_TIMES_UTC=${BACKUP_TIMES_UTC:-02:30,08:30,14:30,20:30}
|
- BACKUP_MINUTE=${BACKUP_MINUTE:-30}
|
||||||
- BACKUP_KEEP_DAYS=${BACKUP_KEEP_DAYS:-30}
|
- BACKUP_KEEP_DAYS=${BACKUP_KEEP_DAYS:-30}
|
||||||
- BACKUP_RUN_ON_START=${BACKUP_RUN_ON_START:-0}
|
- BACKUP_RUN_ON_START=${BACKUP_RUN_ON_START:-0}
|
||||||
- RUSTFS_ENDPOINT=${RUSTFS_ENDPOINT}
|
- RUSTFS_ENDPOINT=${RUSTFS_ENDPOINT}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue