Add infrastructure management: backup scripts and documentation

This commit is contained in:
Bendt
2026-02-09 10:58:30 -05:00
parent fab217c259
commit 61236e864c
3 changed files with 407 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
#!/bin/bash
# backup-compose-files.sh
# Backs up all compose files from the cluster to local repo
set -e
BACKUP_DIR="/Users/timothy.bendt/developer/cloud-compose/infrastructure"
CONTROLLER="ubuntu@192.168.2.130"
DATE=$(date +%Y-%m-%d)
echo "📦 Backing up compose files from cluster..."
echo "Date: $DATE"
echo ""
# Backup Dokploy compose files
echo "📁 Backing up Dokploy compose files..."
ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "find /etc/dokploy/compose -name 'docker-compose.yml' -type f" | while read file; do
# Get the project name from the path
project=$(echo $file | grep -oP 'compose/\K[^/]+' | head -1)
if [ ! -z "$project" ]; then
echo " - $project"
mkdir -p "$BACKUP_DIR/compose/$project"
scp -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER:"$file" "$BACKUP_DIR/compose/$project/"
fi
done
# Backup Traefik configuration
echo "📁 Backing up Traefik configuration..."
mkdir -p "$BACKUP_DIR/traefik"
scp -r -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER:/etc/dokploy/traefik/* "$BACKUP_DIR/traefik/" 2>/dev/null || echo " (No Traefik config or permission denied)"
# Backup local compose files from home directory
echo "📁 Backing up local compose files..."
scp -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER:~/minio-stack.yml "$BACKUP_DIR/stacks/" 2>/dev/null || true
# Create manifest
echo "📄 Creating backup manifest..."
cat > "$BACKUP_DIR/BACKUP_MANIFEST.md" << EOF
# Infrastructure Backup Manifest
**Backup Date:** $DATE
**Source:** $CONTROLLER
## Contents
### Compose Files
All Docker Compose files from Dokploy-managed projects:
\`\`\`
$(ls -1 $BACKUP_DIR/compose/)
\`\`\`
### Stack Files
Standalone stack definitions:
\`\`\`
$(ls -1 $BACKUP_DIR/stacks/ 2>/dev/null || echo "None")
\`\`\`
### Traefik Configuration
Reverse proxy configuration backed up to ./traefik/
## Restoration
To restore these configurations:
1. Copy compose files to /etc/dokploy/compose/<project>/code/
2. Copy stack files to ~/ on the controller
3. Copy Traefik config to /etc/dokploy/traefik/
4. Redeploy via Dokploy UI or docker stack deploy
## Services Backed Up
$(ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 $CONTROLLER "docker service ls --format '- {{.Name}}'" 2>/dev/null || echo "Unable to fetch service list")
---
Generated: $(date)
EOF
echo ""
echo "✅ Backup complete!"
echo "📁 Location: $BACKUP_DIR/"
echo "📄 Manifest: $BACKUP_DIR/BACKUP_MANIFEST.md"

View File

@@ -0,0 +1,139 @@
#!/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!"