#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
mkdir -p backups
umask 077
stamp=$(date -u +%Y%m%dT%H%M%SZ)
# Python's SQLite online backup safely snapshots the app during writes.
python3 - "$stamp" <<'PY2'
import sqlite3,sys
stamp=sys.argv[1]
source=sqlite3.connect('storage/app.sqlite')
target=sqlite3.connect('backups/app-'+stamp+'.sqlite')
source.backup(target)
target.close();source.close()
PY2
cp storage/config.php "backups/config-$stamp.php"
docker compose exec -T db sh -c 'exec mariadb-dump --single-transaction -u root -p"$MARIADB_ROOT_PASSWORD" radius' > "backups/radius-$stamp.sql"
printf 'Backups written for %s. Encrypt and copy offsite; test restoring before production.
' "$stamp"
