#!/bin/bash
# backup-db.sh
set -euo pipefail
source /Users/rxf/Projekte/wetterstation/.env

BACKUP_DIR="/Users/rxf/Projekte/wetterstation/backups"
mkdir -p "$BACKUP_DIR"
FILENAME="wetterstation_$(date +%Y%m%d_%H%M).dump"

docker exec wetterstation_db pg_dump -U "$DB_USER" -d "$DB_NAME" -F c -f /tmp/backup.dump
docker cp wetterstation_db:/tmp/backup.dump "$BACKUP_DIR/$FILENAME"
docker exec wetterstation_db rm /tmp/backup.dump

# Alte Backups löschen (älter als 30 Tage)
find "$BACKUP_DIR" -name "*.dump" -mtime +30 -delete

echo "Backup gespeichert: $BACKUP_DIR/$FILENAME"
