#!/usr/bin/env bash
# backend/scripts/backup.sh — SQLite 락 안전 백업(.backup) → gzip
set -euo pipefail
DB="${DATABASE_URL:-sqlite:////data/ari.db}"
DB_PATH="${DB#sqlite:///}"
OUT="${BACKUP_DIR:-/backups}"
TS="$(date +%Y%m%d-%H%M%S)"
mkdir -p "$OUT"
if command -v sqlite3 >/dev/null 2>&1 && [ -f "$DB_PATH" ]; then
  sqlite3 "$DB_PATH" ".backup '$OUT/ari-$TS.db'"
  gzip -f "$OUT/ari-$TS.db"
  echo "backup → $OUT/ari-$TS.db.gz"
else
  echo "sqlite3 미설치 또는 Postgres — pg_dump 사용 권장" >&2
  exit 1
fi
