#!/usr/bin/env bash # Daily pCloud-Backup (Gotti) - sichert kritische Hermes/System-Config # nach ~/pCloudDrive/backups/hermes/ (nur wenn gemountet). # Fail-safe: bricht sauber ab, wenn pCloudDrive nicht gemountet ist. set -u PCLOUD="$HOME/pCloudDrive" DEST="$PCLOUD/GotthardJohann_Backup/hermes" LOG="/home/nextlevel/dashboard/pcloud_backup_$(date '+%Y-%m-%d').log" exec > >(tee -a "$LOG") 2>&1 echo "=== pCloud-Backup gestartet: $(date) ===" # Mount-Check (robust: pruefe echtes FUSE-Mount via /proc/mounts) if ! grep -q " $PCLOUD " /proc/mounts 2>/dev/null; then echo "[!!] pCloudDrive NICHT gemountet - Backup uebersprungen" # WhatsApp-Warnung auch beim Skip (Plan C) - vor dem Exit! if [ -x /home/nextlevel/dashboard/notify_whatsapp.sh ]; then /home/nextlevel/dashboard/notify_whatsapp.sh "Gotti pCloud-Backup UEBERSPRUNGEN: pCloudDrive nicht gemountet - kein Snapshot angelegt" || true fi exit 0 fi mkdir -p "$DEST" STAMP="$(date '+%Y%m%d_%H%M%S')" SNAP="$DEST/$STAMP" mkdir -p "$SNAP" # 1) .hermes - NUR einzigartige Config, KEIN Code/Venv/Caches # (hermes-agent + tts_venv + bin sind reproduzierbar, wuerden 1,2GB+/Tag fressen) if [ -d "$HOME/.hermes" ]; then rsync -a \ --exclude='hermes-agent/' \ --exclude='tts_venv/' \ --exclude='bin/' \ --exclude='cache/' \ --exclude='logs/*.log' \ --exclude='state.db*' \ --exclude='*.pid' --exclude='*.lock' --exclude='gateway-starts.log' \ "$HOME/.hermes/" "$SNAP/hermes_home/" 2>/dev/null echo "[OK] ~/.hermes (config/creds/memory) gesichert" fi # 2) systemd service-file if [ -f "$HOME/.config/systemd/user/hermes-dashboard.service" ]; then mkdir -p "$SNAP/systemd_user" cp "$HOME/.config/systemd/user/hermes-dashboard.service" "$SNAP/systemd_user/" 2>/dev/null echo "[OK] hermes-dashboard.service gesichert" fi # 3) rclone.conf (chmod 600 erhalten) if [ -f "$HOME/.config/rclone/rclone.conf" ]; then mkdir -p "$SNAP/rclone" cp "$HOME/.config/rclone/rclone.conf" "$SNAP/rclone/" 2>/dev/null chmod 600 "$SNAP/rclone/rclone.conf" 2>/dev/null echo "[OK] rclone.conf gesichert" fi # 4) dashboard skripte if [ -d "$HOME/dashboard" ]; then mkdir -p "$SNAP/dashboard" cp "$HOME/dashboard"/*.sh "$SNAP/dashboard/" 2>/dev/null echo "[OK] dashboard-skripte gesichert" fi # 5) hermes config.yaml if [ -f "$HOME/.hermes/hermes-agent/config.yaml" ]; then mkdir -p "$SNAP/config" cp "$HOME/.hermes/hermes-agent/config.yaml" "$SNAP/config/" 2>/dev/null echo "[OK] hermes config.yaml gesichert" fi # Rotation: behalte nur die 3 NEUESTEN Snapshots, Rest weg # (verhindert, dass pCloud volllaeuft - User-Vorgabe: 3 reichen) KEEP=3 # alle snapshot-dirs chronologisch, neueste zuerst mapfile -t ALL_SNAPS < <(ls -1 "$DEST" 2>/dev/null | grep -E '^20[0-9]{6}_[0-9]{6}$' | sort -r) if [ "${#ALL_SNAPS[@]}" -gt "$KEEP" ]; then # ab Element (KEEP+1) alles loeschen for old in "${ALL_SNAPS[@]:$KEEP}"; do rm -rf "$DEST/$old" 2>/dev/null echo "[DEL] alter Snapshot entfernt: $old" done else echo "[OK] nur ${#ALL_SNAPS[@]} Snapshot(s) - nichts zu entfernen (KEEP=$KEEP)" fi echo "[OK] Rotation auf $KEEP neueste Snapshots begrenzt" echo "=== Snapshot: $SNAP ===" du -sh "$SNAP" 2>/dev/null echo "=== Backup beendet: $(date) ===" # WhatsApp-Notification (Plan C) - unabhaengig vom Agenten SUMMARY="pCloud-Backup OK: $(du -sh "$SNAP" 2>/dev/null | cut -f1) gesichert nach $SNAP" # Korrekte Mount-Pruefung (case-sensitiv fehlerhaft war: $PCLOUD/backups existiert nicht -> immer true) if ! grep -q " $PCLOUD " /proc/mounts 2>/dev/null; then SUMMARY="pCloud-Backup WARNUNG: pCloudDrive waehrend Lauf unmounted - Snapshot evtl. nur lokal" fi if [ -x /home/nextlevel/dashboard/notify_whatsapp.sh ]; then /home/nextlevel/dashboard/notify_whatsapp.sh "Gotti $SUMMARY" || true fi