#!/bin/bash
set -euo pipefail

# ── PostgreSQL Automated Backup Script ──
# Runs daily via cron inside the pg-backup container.
# Dumps the database and manages retention.

BACKUP_DIR="${BACKUP_DIR:-/backups}"
RETENTION_DAYS="${RETENTION_DAYS:-7}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/goodgo_${TIMESTAMP}.sql.gz"

echo "[backup] Starting PostgreSQL backup at $(date -Iseconds)"

# Ensure backup directory exists
mkdir -p "${BACKUP_DIR}"

# Run pg_dump with compression
pg_dump \
  -h "${PGHOST:-postgres}" \
  -p "${PGPORT:-5432}" \
  -U "${PGUSER:-goodgo}" \
  -d "${PGDATABASE:-goodgo}" \
  --no-owner \
  --no-privileges \
  --format=custom \
  --compress=6 \
  -f "${BACKUP_FILE}"

FILESIZE=$(du -h "${BACKUP_FILE}" | cut -f1)
echo "[backup] Backup completed: ${BACKUP_FILE} (${FILESIZE})"

# Prune old backups beyond retention period
echo "[backup] Pruning backups older than ${RETENTION_DAYS} days..."
PRUNED=$(find "${BACKUP_DIR}" -name "goodgo_*.sql.gz" -type f -mtime "+${RETENTION_DAYS}" -print -delete | wc -l)
echo "[backup] Pruned ${PRUNED} old backup(s)"

# List current backups
echo "[backup] Current backups:"
ls -lh "${BACKUP_DIR}"/goodgo_*.sql.gz 2>/dev/null || echo "  (none)"

echo "[backup] Done at $(date -Iseconds)"
