27 lines
959 B
Bash
27 lines
959 B
Bash
|
|
#!/bin/sh
|
||
|
|
# Loops forever: sleeps until the next BACKUP_HOUR:BACKUP_MINUTE UTC, then runs backup_db.sh.
|
||
|
|
# Defaults: 02:30 UTC nightly.
|
||
|
|
|
||
|
|
set -eu
|
||
|
|
|
||
|
|
HOUR="${BACKUP_HOUR:-2}"
|
||
|
|
MINUTE="${BACKUP_MINUTE:-30}"
|
||
|
|
|
||
|
|
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)"
|
||
|
|
fi
|
||
|
|
|
||
|
|
while true; do
|
||
|
|
NOW_EPOCH=$(date -u +%s)
|
||
|
|
TARGET=$(date -u -d "today ${HOUR}:${MINUTE}:00" +%s 2>/dev/null \
|
||
|
|
|| date -u -j -f "%H:%M:%S" "${HOUR}:${MINUTE}:00" +%s)
|
||
|
|
if [ "$TARGET" -le "$NOW_EPOCH" ]; then
|
||
|
|
TARGET=$((TARGET + 86400))
|
||
|
|
fi
|
||
|
|
SLEEP=$((TARGET - NOW_EPOCH))
|
||
|
|
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))"
|
||
|
|
sleep "$SLEEP"
|
||
|
|
/app/backup_db.sh || echo "[$(date -u +%FT%TZ)] backup failed (will retry tomorrow)"
|
||
|
|
done
|