#!/usr/bin/env bash
set -euo pipefail

# Immutable, point-in-time safety copy immediately before the PostgreSQL cutover.
# The FilyX production environment is read from its protected .env; no credential
# is echoed by this script.
destination='/srv/filyx/backups/postgres-cutover-20261015'
snapshot="$destination/filyx-before-postgresql-cutover.sql"
checksum="$snapshot.sha256"

install -d -m 0750 "$destination"
set -a
. /srv/filyx/.env
set +a

umask 027
mariadb-dump \
  --host="$DB_HOST" --port="$DB_PORT" --user="$DB_USER" --password="$DB_PASS" \
  --single-transaction --quick --routines --events --triggers --skip-lock-tables \
  --default-character-set=utf8mb4 "$DB_NAME" > "$snapshot"
sha256sum "$snapshot" > "$checksum"
sha256sum -c "$checksum"
find "$destination" -maxdepth 1 -type f -printf '%f %s bytes\n'
