#!/bin/bash
# backup-critical-data.sh
# Backs up critical databases and data from the homelab

set -e

BACKUP_DIR="/Users/timothy.bendt/developer/cloud-compose/backups"
CONTROLLER="ubuntu@192.168.2.130"
NAS="tim@192.168.2.18"
DATE=$(date +%Y-%m-%d)
RETENTION_DAYS=30

echo "💾 Starting critical data backup..."
echo "Date: $DATE"
echo ""

mkdir -p "$BACKUP_DIR"

# Function to backup a PostgreSQL database
backup_postgres() {
    local service=$1
    local db_name=$2
    local output_file="$BACKUP_DIR/${service}-${db_name}-${DATE}.sql"
    
    echo "  📊 Backing up PostgreSQL: $service/$db_name..."
    
    # Find the container
    container=$(ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker ps -q -f name=$service" | head -1)
    
    if [ ! -z "$container" ]; then
        ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker exec $container pg_dump -U postgres $db_name" > "$output_file"
        echo "    ✅ Saved to: $output_file"
    else
        echo "    ❌ Container not found: $service"
    fi
}

# Function to backup a MariaDB database
backup_mariadb() {
    local service=$1
    local db_name=$2
    local output_file="$BACKUP_DIR/${service}-${db_name}-${DATE}.sql"
    
    echo "  📊 Backing up MariaDB: $service/$db_name..."
    
    container=$(ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker ps -q -f name=$service" | head -1)
    
    if [ ! -z "$container" ]; then
        ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker exec $container mariadb-dump -u root -p'3QU5eA&U^Y&3DQm6' $db_name" > "$output_file" 2>/dev/null || \
        echo "    ⚠️  Could not backup (check credentials)"
    else
        echo "    ❌ Container not found: $service"
    fi
}

# Function to backup volume data
backup_volume() {
    local service=$1
    local volume=$2
    local output_file="$BACKUP_DIR/${service}-data-${DATE}.tar.gz"
    
    echo "  💿 Backing up volume: $volume..."
    
    ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker run --rm -v $volume:/data -v /tmp:/backup alpine tar czf /backup/${service}-backup.tar.gz -C /data ."
    scp -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER:/tmp/${service}-backup.tar.gz "$output_file"
    ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "rm -f /tmp/${service}-backup.tar.gz"
    
    echo "    ✅ Saved to: $output_file"
}

echo "📦 Backing up databases..."

# Dokploy database
backup_postgres "dokploy-postgres" "dokploy"

# Immich database
backup_postgres "immich3-compose-ubyhe9-immich-database" "immich"

# BewCloud database  
backup_postgres "bewcloud-postgres-in40hh" "bewcloud"

# Pancake database
backup_mariadb "bendtstudio-pancake-bzgfpc" "pancake"

echo ""
echo "📦 Backing up application data..."

# Memos data
backup_volume "bewcloud-memos-ssogxn-memos" "bewcloud-memos-ssogxn_memos_data"

# Gitea data
echo "  💿 Backing up Gitea repositories..."
container=$(ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker ps -q -f name=gitea" | head -1)
if [ ! -z "$container" ]; then
    ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker exec $container tar czf /tmp/gitea-backup.tar.gz -C /data ."
    scp -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER:/tmp/gitea-backup.tar.gz "$BACKUP_DIR/gitea-data-${DATE}.tar.gz"
    ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "rm -f /tmp/gitea-backup.tar.gz"
    echo "    ✅ Saved to: $BACKUP_DIR/gitea-data-${DATE}.tar.gz"
fi

echo ""
echo "🧹 Cleaning up old backups (older than $RETENTION_DAYS days)..."
find "$BACKUP_DIR" -type f -mtime +$RETENTION_DAYS -delete
echo "  ✅ Cleanup complete"

echo ""
echo "📄 Creating backup report..."
cat > "$BACKUP_DIR/BACKUP_REPORT-${DATE}.md" << EOF
# Backup Report - $DATE

## Backup Location
$BACKUP_DIR

## Files Backed Up
\`\`\`
$(ls -lh $BACKUP_DIR/*-${DATE}* 2>/dev/null || echo "No files found")
\`\`\`

## Services Status
$(ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker service ls --format 'table {{.Name}}\t{{.Replicas}}\t{{.Image}}'" 2>/dev/null || echo "Unable to fetch status")

## Next Steps
1. Verify backup files are valid
2. Copy backups to offsite storage (MinIO, external drive, etc.)
3. Test restore procedure periodically

## Retention Policy
Backups older than $RETENTION_DAYS days are automatically deleted.

---
Generated: $(date)
EOF

echo ""
echo "✅ Backup complete!"
echo "📁 Location: $BACKUP_DIR/"
echo "📄 Report: $BACKUP_DIR/BACKUP_REPORT-${DATE}.md"
echo ""
echo "⚠️  Remember to copy backups to offsite storage!"
