Feat: Voll-Zustands-Backup + getesteter Restore + taeglicher Timer

Behebt 3 von 4 Backup-Luecken (Schicht 1, lokal):
- backup.sh sichert jetzt den ECHTEN Zustand als ein Tarball mc2-state-<ts>.tar.gz:
  mem0 (Chroma+history.db), ~/.hermes (config.yaml, .env, plugins/), llama-swap config.
  Vorher wurde nur die alte/leere mc2-memory.db gesichert. chmod 600 (enthaelt .env).
- restore.sh: --list / --dry-run / [--yes] <datei|latest>; macht VOR dem Zurueckspielen
  ein Sicherheits-Backup, stoppt/startet Dienste, Health-Check. Live round-trip verifiziert.
- mc2-backup.timer/.service: taegliches Backup ~03:30 (vorher gab es KEINE Automatik).
- backend/services/backup.py delegiert an backup.sh (eine Quelle der Wahrheit); UI-Button
  + /api/system/backups zeigen die Tarballs.
- docs/BACKUP.md: Backup/Restore-Anleitung.

Offen (Schicht 2): Off-Box-Spiegel (Box ist Bare Metal -> PBS-Client oder rsync in LXC).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Hitonabi
2026-06-27 21:25:39 +02:00
parent 9fb321e45e
commit 8554e7b29c
13 changed files with 251 additions and 48 deletions
+40 -11
View File
@@ -1,18 +1,47 @@
#!/usr/bin/env bash
# Backup der Shared-Memory-SQLite + Configs (cron-bar auf der Box).
# Behält die letzten 7 Snapshots. Identisch zur /api/system/backup-Logik.
# Voll-Zustands-Backup der AI-Box: alles, was NICHT aus Git oder per Re-Download
# zurückkommt. Erzeugt EIN Tarball mc2-state-<ts>.tar.gz, behält die letzten N.
# Läuft per systemd-Timer (täglich), manuell, oder über den UI-Snapshot-Button.
#
# Inhalt: mem0 (Chroma + history.db) · ~/.hermes (config.yaml, .env, plugins/) ·
# /etc/llama-swap/config.yaml
# NICHT enthalten (bewusst): GGUF-Modelle (riesig, neu ladbar), MC2-Code (Git), venvs.
#
# ACHTUNG: das Tarball enthält ~/.hermes/.env (Secrets) → chmod 600, nicht in Git.
set -euo pipefail
MODELS_DIR="${MC_MODELS_DIR:-/srv/models}"
MEM_DB="${MC_MEMORY_DB:-$MODELS_DIR/mc2-memory.db}"
DEST_DIR="$MODELS_DIR/mc2-backups"
RETAIN="${MC_BACKUP_RETAIN:-14}"
MEM0_DIR="${MC_MEM0_DIR:-/srv/models/mem0}"
LSWAP="${MC_CONFIG_PATH:-/etc/llama-swap/config.yaml}"
DEST="$MODELS_DIR/mc2-backups/$(date +%Y%m%d-%H%M%S)"
HERMES="${HERMES_HOME:-$HOME/.hermes}"
TS="$(date +%Y%m%d-%H%M%S)"
mkdir -p "$DEST"
for f in "$MEM_DB" "$MEM_DB-wal" "$MEM_DB-shm" "$LSWAP"; do
[ -f "$f" ] && cp -p "$f" "$DEST/" || true
done
STAGE="$(mktemp -d)"
trap 'rm -rf "$STAGE"' EXIT
mkdir -p "$STAGE/mem0" "$STAGE/hermes" "$STAGE/llama-swap"
# nur die letzten 7 behalten
ls -1dt "$MODELS_DIR"/mc2-backups/*/ 2>/dev/null | tail -n +8 | xargs -r rm -rf
echo "OK — Backup unter $DEST"
[ -d "$MEM0_DIR" ] && cp -a "$MEM0_DIR/." "$STAGE/mem0/" || true
[ -f "$HERMES/config.yaml" ] && cp -a "$HERMES/config.yaml" "$STAGE/hermes/" || true
[ -f "$HERMES/.env" ] && cp -a "$HERMES/.env" "$STAGE/hermes/" || true
[ -d "$HERMES/plugins" ] && cp -a "$HERMES/plugins" "$STAGE/hermes/plugins" || true
[ -f "$LSWAP" ] && cp -a "$LSWAP" "$STAGE/llama-swap/" || true
cat > "$STAGE/MANIFEST.txt" <<EOF
mc2-state backup
created : $TS
host : $(hostname)
inhalt : mem0 (chroma + history.db), hermes (config.yaml, .env, plugins/), llama-swap (config.yaml)
restore : bash ~/mission-control-v2/deploy/restore.sh mc2-state-$TS.tar.gz
EOF
mkdir -p "$DEST_DIR"
OUT="$DEST_DIR/mc2-state-$TS.tar.gz"
tar -czf "$OUT" -C "$STAGE" .
chmod 600 "$OUT" # enthält .env (Secrets)
# Retention: nur die letzten N behalten.
ls -1t "$DEST_DIR"/mc2-state-*.tar.gz 2>/dev/null | tail -n +$((RETAIN + 1)) | xargs -r rm -f
echo "OK — Backup: $OUT ($(du -h "$OUT" | cut -f1))"