Files
AI-Profile-Router/restore.sh
T

52 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Restore Athena's non-reproducible Docker state from the latest /data backup.
set -Eeuo pipefail
umask 077
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ARCHIVE=${1:-/data/docker-backups/athena-latest.tar.gz}
die() { printf 'FEHLER: %s\n' "$*" >&2; exit 1; }
[[ $EUID -eq 0 ]] || die "Bitte als root ausführen."
[[ -s $ARCHIVE ]] || die "Backup fehlt: $ARCHIVE"
command -v docker >/dev/null || die "Docker fehlt. Zuerst ./install.sh ausführen."
work=$(mktemp -d /tmp/athena-restore.XXXXXX)
trap 'rm -rf "$work"' EXIT
# Refuse absolute paths and parent traversal before extracting as root.
if tar -tzf "$ARCHIVE" | grep -Eq '(^/|(^|/)\.\.(/|$))'; then
die "Unsichere Pfade im Backup."
fi
tar -xzf "$ARCHIVE" -C "$work"
[[ -d $work/etc-mike-ai ]] || die "Backup enthält etc-mike-ai nicht."
[[ -d $work/volumes ]] || die "Backup enthält keine Docker-Volumes."
# Stop only users of the restored volumes. WireGuard, SSH and networking stay up.
for container in mike-ai-router mike-ai-profile-controller mike-ai-piper; do
if [[ $(docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null || true) == true ]]; then
docker stop "$container" >/dev/null
fi
done
install -d -m 0700 /etc/mike-ai
rsync -a --delete "$work/etc-mike-ai/" /etc/mike-ai/
restore_volume() {
local volume=$1 source=$2 mountpoint
[[ -d $source ]] || return 0
docker volume create "$volume" >/dev/null
mountpoint=$(docker volume inspect -f '{{.Mountpoint}}' "$volume")
[[ -n $mountpoint && -d $mountpoint ]] || die "Volume nicht zugreifbar: $volume"
rsync -a --delete "$source/" "$mountpoint/"
}
restore_volume mike-ai_piper-data "$work/volumes/piper-data"
restore_volume mike-ai_router-state "$work/volumes/router-state"
restore_volume mike-ai_router-images "$work/volumes/router-images"
cd "$ROOT_DIR"
./manage.sh deploy core
printf 'ATHENA_RESTORE_OK %s\n' "$ARCHIVE"