#!/bin/bash
set -e

BACKUP_DIR="./backups"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
FILENAME="backup_${TIMESTAMP}.sql"

mkdir -p $BACKUP_DIR

echo "💾 Делаем бэкап базы..."
docker compose exec db mysqldump -u"$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "${BACKUP_DIR}/${FILENAME}"

echo "✅ Бэкап сохранён: ${BACKUP_DIR}/${FILENAME}"
echo "📦 Архивируем бэкап..."
tar -czf "${BACKUP_DIR}/${FILENAME}.tar.gz" -C "$BACKUP_DIR" "$FILENAME"
echo "✅ Бэкап архивирован: ${BACKUP_DIR}/${FILENAME}.tar.gz"