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

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
DB_PATH="${DB_PATH:-$ROOT_DIR/data/okr-performance.db}"

if [[ $# -ne 1 ]]; then
  echo "用法: $0 /path/to/okr-performance-backup.db" >&2
  exit 1
fi

BACKUP_PATH="$1"
if [[ ! -f "$BACKUP_PATH" ]]; then
  echo "备份文件不存在: $BACKUP_PATH" >&2
  exit 1
fi

mkdir -p "$(dirname "$DB_PATH")"
if [[ -f "$DB_PATH" ]]; then
  SAFETY_COPY="$DB_PATH.before-restore-$(date +%Y%m%d-%H%M%S)"
  cp "$DB_PATH" "$SAFETY_COPY"
  echo "已创建恢复前安全备份: $SAFETY_COPY"
fi

cp "$BACKUP_PATH" "$DB_PATH"
echo "已恢复数据库: $DB_PATH"
