#!/bin/bash
set -e

BACKUP_DIR="/var/backups/pdimaker"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/pdimaker_$DATE.sql.gz"

mkdir -p $BACKUP_DIR

echo "🗄️  Fazendo backup do banco de dados..."
docker-compose exec -T postgres pg_dump -U pdimaker pdimaker_prod | gzip > $BACKUP_FILE

echo "✅ Backup salvo em: $BACKUP_FILE"

# Manter apenas últimos 7 backups
find $BACKUP_DIR -name "*.sql.gz" -mtime +7 -delete
