#!/bin/bash
# pre-backup.sh — esh-vm-db.
# Runs as root from resticprofile's `run-before`.
#
# Produces consistent DB dumps in /var/lib/restic/stage/. Two DBs here:
#   - Postgres 15 (port 5432, local) — pg_dumpall all databases
#   - MongoDB (port 27017, local) — mongodump all databases
#
# Peer-data DBs (paperless-ng on Postgres) are the primary consumers;
# raw volume capture isn't in the restic source list so these dumps
# are the ONLY way restic sees DB data.
#
# Errors in individual blocks log a WARN; whole script doesn't abort.

set -euo pipefail

STAGE=/var/lib/restic/stage
install -d -o root -g root -m 0700 "$STAGE"

log() { printf '%s pre-backup(esh-vm-db): %s\n' "$(date -Is)" "$*"; }
warn() { printf '%s pre-backup(esh-vm-db): WARN: %s\n' "$(date -Is)" "$*" >&2; }

# ---- Postgres ----------------------------------------------------------
PG_DUMP="$STAGE/pg_dumpall.sql.gz"
if sudo -u postgres pg_isready -h localhost -p 5432 > /dev/null 2>&1; then
  log "pg_dumpall starting → $PG_DUMP"
  if sudo -u postgres pg_dumpall -h localhost -p 5432 | gzip > "$PG_DUMP.tmp"; then
    mv "$PG_DUMP.tmp" "$PG_DUMP"
    log "pg_dumpall done ($(du -h "$PG_DUMP" | cut -f1))"
  else
    warn "pg_dumpall failed (exit $?); keeping previous dump if any"
    rm -f "$PG_DUMP.tmp"
  fi
else
  warn "postgres not ready on :5432 — skipping pg_dumpall"
fi

# ---- MongoDB -----------------------------------------------------------
MONGO_DIR="$STAGE/mongodump"
if mongosh --quiet --eval 'db.adminCommand({ping: 1}).ok' | grep -q '^1$'; then
  log "mongodump starting → $MONGO_DIR"
  rm -rf "$MONGO_DIR.tmp"
  if mongodump --out "$MONGO_DIR.tmp" > /dev/null 2>&1; then
    rm -rf "$MONGO_DIR"
    mv "$MONGO_DIR.tmp" "$MONGO_DIR"
    log "mongodump done ($(du -sh "$MONGO_DIR" | cut -f1))"
  else
    warn "mongodump failed (exit $?); keeping previous dump if any"
    rm -rf "$MONGO_DIR.tmp"
  fi
else
  warn "mongo not reachable via mongosh — skipping mongodump"
fi

# ---- Retention on stage dir --------------------------------------------
# restic dedupes identical dumps at the chunk level, so we can safely keep
# overwriting the same files. No explicit rotation needed here.

log "pre-backup complete"
