#!/usr/bin/env bash
set -euo pipefail

BACKUP_DIR="/mnt/s3bucket/nc-bk"

echo "🛑 Stopping all containers..."
docker stop $(docker ps -q) || true

echo "🧹 Clearing old backup..."
rm -rf "$BACKUP_DIR"
mkdir -p "$BACKUP_DIR"

for vol in nextcloud_aio_apache \
           nextcloud_aio_database \
           nextcloud_aio_database_dump \
           nextcloud_aio_mastercontainer \
           nextcloud_aio_nextcloud \
           nextcloud_aio_nextcloud_data \
           nextcloud_aio_onlyoffice \
           nextcloud_aio_redis; do
    echo "📦 Backing up $vol..."
    docker run --rm \
      -v ${vol}:/volume \
      -v "$BACKUP_DIR":/backup \
      alpine tar -czf /backup/${vol}.tar.gz -C /volume .
done

echo "✅ Backup complete! Files saved in: $BACKUP_DIR"
ls -lh "$BACKUP_DIR"
