Repository Public
1#!/bin/zsh2# Restore an Agent OS backup into a database.3# deploy/restore.sh <archive.tar.gz.enc> <target DATABASE_URL> [--check-artifacts]4# The target database must exist and be empty (use a scratch database for restore drills):5# createdb -O agent_os agent_os_restore && psql -d agent_os_restore -c 'create extension if not exists pgcrypto'6# The archive holds the database and an INDEX of the artifact store, not the artifact bytes. With7# --check-artifacts the index is compared with $AOS_DATA_DIR/artifacts (default ~/agent-os-data) and the8# artifacts missing on disk are counted; nothing is written outside the target database.9# Passphrase: $PASS_FILE (default ~/.config/agent-os/backup.pass).10set -euo pipefail11export PATH=/opt/homebrew/bin:/opt/homebrew/opt/postgresql@17/bin:/usr/bin:/bin:/usr/sbin:/sbin12ARCHIVE="${1:?archive}"; TARGET="${2:?target DATABASE_URL}"; OPT="${3:-}"13APP=agent-os14if [[ -n "$OPT" && "$OPT" != "--check-artifacts" ]]; then15 echo "unknown option: $OPT (only --check-artifacts)" >&2; exit 216fi17PASS_FILE="${PASS_FILE:-$HOME/.config/$APP/backup.pass}"18umask 07719WORK=$(mktemp -d); trap 'rm -rf "$WORK"' EXIT20openssl enc -d -aes-256-cbc -pbkdf2 -iter 200000 -pass "file:$PASS_FILE" -in "$ARCHIVE" | tar -xzf - -C "$WORK"21echo "archive created at: $(cat "$WORK/created_at")"22[[ -f "$WORK/artifacts-summary" ]] && echo "artifact index: $(cat "$WORK/artifacts-summary")"23pg_restore --no-owner --no-privileges --exit-on-error -d "$TARGET" "$WORK/db.dump"24psql "$TARGET" -tAc "select 'users='||count(*) from users union all select 'projects='||count(*) from projects union all select 'missions='||count(*) from missions union all select 'runs='||count(*) from runs union all select 'artifacts='||count(*) from artifacts union all select 'audit_events='||count(*) from audit_events"25if [[ "$OPT" == "--check-artifacts" ]]; then26 STORE="${AOS_DATA_DIR:-$HOME/agent-os-data}/artifacts"27 missing=0; total=028 while IFS=' ' read -r _size rel; do29 [[ -n "$rel" ]] || continue30 total=$((total + 1))31 [[ -f "$STORE/$rel" ]] || missing=$((missing + 1))32 done < "$WORK/artifacts-index.tsv"33 echo "artifact store $STORE: $missing of $total indexed artifacts missing on disk"34fi35echo "restore ok"36