#!/usr/bin/env bash
# Backup SQLite database — копирует .db файл с timestamp в директорию бэкапов.
# Вызывается из systemd timer или cron.
#
# Использование: ./scripts/backup-db.sh [path-to-db] [backup-dir] [retention-days]
# По умолчанию: db=server/prisma/prod.db, backup=server/backups, retention=30

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

DB_PATH="${1:-$ROOT/server/prisma/prod.db}"
BACKUP_DIR="${2:-$ROOT/server/backups}"
RETENTION_DAYS="${3:-30}"

if [[ ! -f "$DB_PATH" ]]; then
  echo "[$(date -Iseconds)] DB not found: $DB_PATH" >&2
  exit 1
fi

mkdir -p "$BACKUP_DIR"

TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/craftshop_${TIMESTAMP}.db"

# SQLite-safe copy: use .backup to avoid copying a file mid-write
if command -v sqlite3 &>/dev/null; then
  sqlite3 "$DB_PATH" ".backup '$BACKUP_FILE'"
else
  # Fallback: plain copy (risk of inconsistent state if DB is being written)
  cp "$DB_PATH" "$BACKUP_FILE"
fi

# Compress
gzip -f "$BACKUP_FILE"

# Remove old backups
find "$BACKUP_DIR" -name 'craftshop_*.db.gz' -mtime +"$RETENTION_DAYS" -delete

echo "[$(date -Iseconds)] Backup created: ${BACKUP_FILE}.gz"
