#!/usr/bin/env bash
# 备份生产 Docker PostgreSQL 到 ./backups，输出 gzip 与 sha256 校验文件

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=scripts/deploy-lib.sh
source "$SCRIPT_DIR/deploy-lib.sh"

PREFIX="manual"

usage() {
  cat <<'EOF'
用法: scripts/backup-db.sh [选项]

选项:
  --prefix NAME   备份文件名前缀，默认 manual
  -h, --help      显示帮助

示例:
  ./scripts/backup-db.sh
  ./scripts/backup-db.sh --prefix pre-release
EOF
}

while [ $# -gt 0 ]; do
  case "$1" in
    --prefix)
      PREFIX="${2:?缺少 --prefix 参数值}"
      shift 2
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      die "未知参数: $1"
      ;;
  esac
done

cd "$ROOT"
require_docker
ensure_env_file || exit 1
start_infra
backup_database "$PREFIX"
prune_old_backups
