#!/bin/bash

set -e

DATE=$(date +"%Y-%m-%d_%H-%M")
BACKUP_DIR="/backup"
DATA_SRC="/data-uploads"
DUMP_DIR="$BACKUP_DIR/mongo_$DATE"
ARCHIVE="$BACKUP_DIR/backup_$DATE.tar.gz"

echo "1. Realizando backup de MongoDB..."
mongodump \
  --host mongodb \
  --username txuser \
  --password hdR2102Frx8us2WkuN21quAn12 \
  --authenticationDatabase turnosxpress \
  --db turnosxpress \
  --out "$DUMP_DIR"

echo "2. Comprimendo datos..."
tar -czf "$ARCHIVE" "$DATA_SRC" "$DUMP_DIR"

echo "3. Subiendo a Mega con rclone..."
rclone copy "$ARCHIVE" hdrdevs-mega:

echo "4. Eliminando archivos temporales..."
rm -rf "$DUMP_DIR"

echo "✅ Backup finalizado correctamente: $ARCHIVE"