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

INSTALL_DIR="${INSTALL_DIR:-/opt/secondary-school-grade-archive}"
BACKUP_DIR="${BACKUP_DIR:-${INSTALL_DIR}/backups}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

cd "${INSTALL_DIR}"
# shellcheck disable=SC1090
source .env
mkdir -p "${BACKUP_DIR}"

echo "[INFO] 备份数据库…"
PGPASSWORD="${POSTGRES_PASSWORD}" pg_dump -h 127.0.0.1 -U "${POSTGRES_USER}" "${POSTGRES_DB}" \
  > "${BACKUP_DIR}/db_${TIMESTAMP}.sql"

echo "[INFO] 备份 uploads…"
tar -czf "${BACKUP_DIR}/uploads_${TIMESTAMP}.tar.gz" -C "${INSTALL_DIR}" uploads/

echo "[INFO] 完成:"
echo "  ${BACKUP_DIR}/db_${TIMESTAMP}.sql"
echo "  ${BACKUP_DIR}/uploads_${TIMESTAMP}.tar.gz"
