SPB Git

Repository Public

restore.sh 2.6 KB 44 lines shellscript

1#!/bin/zsh2# Restore a Claude Remote backup into a database.3#   deploy/restore.sh <archive.tar.gz.enc> <target DATABASE_URL> [--with-claude-config]4# The target database must exist and be empty (use a scratch database for restore drills).5#6# Codex Remote instance: run with CR_APP_NAME=codex-remote (passphrase ~/.config/codex-remote/backup.pass) and7# use --with-codex-config, which extracts config.toml, AGENTS.md and prompts/ into $CODEX_HOME (default8# ~/.codex). CR_APP_NAME / CR_AGENT follow the same rules as in backup.sh; --with-agent-config means "the9# configuration of this instance's agent". With nothing set this is the Claude Remote restore, unchanged.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="${CR_APP_NAME:-claude-remote}"14[[ "$APP" =~ '^[a-z0-9][a-z0-9-]*$' ]] || { echo "invalid CR_APP_NAME: $APP" >&2; exit 2; }15AGENT="${CR_AGENT:-}"16if [[ -z "$AGENT" ]]; then17  if [[ "$APP" == "codex-remote" ]]; then AGENT=codex; else AGENT=claude; fi18fi19[[ "$AGENT" == "claude" || "$AGENT" == "codex" ]] || { echo "invalid CR_AGENT: $AGENT (claude | codex)" >&2; exit 2; }20if [[ -n "$OPT" && "$OPT" != "--with-agent-config" && "$OPT" != "--with-$AGENT-config" ]]; then21  # Never extract one agent's configuration into the other's instance by mistake.22  if [[ "$OPT" == "--with-claude-config" || "$OPT" == "--with-codex-config" ]]; then23    echo "$OPT does not match this instance ($APP, agent $AGENT); use --with-$AGENT-config" >&2; exit 224  fi25fi26PASS_FILE="${PASS_FILE:-$HOME/.config/$APP/backup.pass}"27WORK=$(mktemp -d); trap 'rm -rf "$WORK"' EXIT28openssl enc -d -aes-256-cbc -pbkdf2 -iter 200000 -pass "file:$PASS_FILE" -in "$ARCHIVE" | tar -xzf - -C "$WORK"29echo "archive created at: $(cat "$WORK/created_at")"30pg_restore --no-owner --no-privileges --exit-on-error -d "$TARGET" "$WORK/db.dump"31psql "$TARGET" -tAc "select 'users='||count(*) from users union all select 'projects='||count(*) from projects union all select 'terminal_sessions='||count(*) from terminal_sessions union all select 'audit_events='||count(*) from audit_events"32if [[ "$OPT" == "--with-agent-config" || "$OPT" == "--with-$AGENT-config" ]]; then33  if [[ "$AGENT" == "codex" ]]; then34    CODEX_DIR="${CODEX_HOME:-$HOME/.codex}"35    mkdir -p "$CODEX_DIR"36    tar -xf "$WORK/codex-config.tar" -C "$CODEX_DIR"37    echo "codex configuration restored into $CODEX_DIR"38  else39    tar -xf "$WORK/claude-config.tar" -C "$HOME"40    echo "claude configuration restored into $HOME/.claude"41  fi42fi43echo "restore ok"44
Files main
Menu

Theme