#!/bin/bash
# Backup Docker volumes and system configs to NAS using Restic
# Can be run manually or via systemd timer

set -e

# Configuration
BACKUP_REPO="/mnt/nas/backups/homelab-restic"
PASSWORD_FILE="/home/hoborg/creds/restic-password.txt"
LOG_FILE="/var/log/homelab-backup.log"
DOCKER_BACKUP_DIR="/tmp/docker-backup-$$"

# Export password
export RESTIC_PASSWORD_FILE="$PASSWORD_FILE"

# Logging function
log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

log "========================================"
log "Starting Homelab Backup"
log "========================================"

# Check if repository exists
if ! restic -r "$BACKUP_REPO" snapshots &>/dev/null; then
    log "ERROR: Backup repository not initialized. Run backup-init.sh first."
    exit 1
fi

# Create temporary directory for Docker volume exports
mkdir -p "$DOCKER_BACKUP_DIR"
log "Created temporary backup directory: $DOCKER_BACKUP_DIR"

# Export Docker volumes
log "Exporting Docker volumes..."

# Gitea
log "  - Exporting Gitea data..."
docker run --rm -v gitea_gitea:/data -v "$DOCKER_BACKUP_DIR":/backup alpine \
    tar czf /backup/gitea-data.tar.gz -C /data .

# Jellyfin
log "  - Exporting Jellyfin config..."
docker run --rm -v jellyfin_jellyfin_config:/data -v "$DOCKER_BACKUP_DIR":/backup alpine \
    tar czf /backup/jellyfin-config.tar.gz -C /data .

# Nextcloud data
log "  - Exporting Nextcloud data..."
docker run --rm -v nextcloud_nextcloud_data:/data -v "$DOCKER_BACKUP_DIR":/backup alpine \
    tar czf /backup/nextcloud-data.tar.gz -C /data .

# Nextcloud database
log "  - Exporting Nextcloud database..."
docker run --rm -v nextcloud_nextcloud_db:/data -v "$DOCKER_BACKUP_DIR":/backup alpine \
    tar czf /backup/nextcloud-db.tar.gz -C /data .

# Portainer
log "  - Exporting Portainer data..."
docker run --rm -v portainer_data:/data -v "$DOCKER_BACKUP_DIR":/backup alpine \
    tar czf /backup/portainer-data.tar.gz -C /data .

log "✓ Docker volumes exported"

# Run backup
log "Running Restic backup..."
restic -r "$BACKUP_REPO" backup \
    --verbose \
    --tag homelab-docker \
    --tag homelab-configs \
    /etc/nginx/sites-available/homelab \
    /etc/systemd/system/copyparty.service \
    /home/hoborg/.config/copyparty/ \
    "$DOCKER_BACKUP_DIR"

# Cleanup temporary files
log "Cleaning up temporary files..."
rm -rf "$DOCKER_BACKUP_DIR"

# Prune old backups (keep: 7 daily, 4 weekly, 6 monthly, 2 yearly)
log "Pruning old backups..."
restic -r "$BACKUP_REPO" forget \
    --keep-daily 7 \
    --keep-weekly 4 \
    --keep-monthly 6 \
    --keep-yearly 2 \
    --prune

# Show backup stats
log "Backup statistics:"
restic -r "$BACKUP_REPO" stats --mode restore-size

log "========================================"
log "Backup completed successfully!"
log "========================================"
