interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / ops / forge-backup.sh
| 1 | #!/usr/bin/env bash |
| 2 | # infra/ops/forge-backup.sh: one age-encrypted tar of the forge's own state per change burst. |
| 3 | # Members are named by their source dir's basename; MANIFEST.txt inside the tar says where each |
| 4 | # came from and what was left out: |
| 5 | # forge-data/ ~/forge-data without tmp/ logs/ archives/ collab/ search/ *.pid and social.key |
| 6 | # (archives/ and collab/ are caches forged rebuilds from Radicle; search/ holds |
| 7 | # derived search shards (NN.out) [INFERENCE: no writer on edge/spark-front-door]; |
| 8 | # logs/ is the request log; social.key is the session HMAC key, which is not |
| 9 | # copied anywhere: a restore writes a new one, see infra/RUNBOOK.md) |
| 10 | # nginx/ /etc/nginx as far as this user can read it; certs/ is root-only and its source |
| 11 | # of record is the hub Pi (acme.sh), copied by edge-certs.sh |
| 12 | # storage/ ~/.radicle/storage/<rid>/refs and <rid>/packed-refs only: no objects, and never |
| 13 | # anything from ~/.radicle/keys |
| 14 | # Output: $SPOOL/forge-backup-<utc>.tar.gz.age (gzip: the refs are mostly 41-byte files, so the |
| 15 | # plain tar is ~30x larger), its .sha256 (sha256 of the ciphertext, written before the rename) |
| 16 | # and latest.json (name, sha256, bytes, time), replaced atomically after the rename. |
| 17 | # A run whose content (names, modes, bytes; mtimes ignored) equals the previous run writes |
| 18 | # nothing. Keeps the newest $KEEP archives. |
| 19 | # FAILS CLOSED: without a valid age recipient it exits 3 before reading or writing anything; when a |
| 20 | # secret path (social.key, keys/, .radicle/keys) would enter the archive, run and --dry-run exit 4. |
| 21 | # The unit (infra/ops/aien-run-forge-backup.service) runs it through aien-onchange. |
| 22 | # |
| 23 | # Usage: forge-backup.sh [run] make one archive |
| 24 | # forge-backup.sh --dry-run list what would go in; write nothing |
| 25 | # (exit 3 when no recipient is set) |
| 26 | # forge-backup.sh restore ARCHIVE IDENTITY DIR check .sha256, decrypt, unpack into an empty DIR |
| 27 | # Env: FORGE_BACKUP_RECIPIENT age1... public key (required for run) |
| 28 | # FORGE_BACKUP_SPOOL default ~/forge-backups |
| 29 | # FORGE_BACKUP_KEEP default 60 |
| 30 | # FORGE_BACKUP_FORGE_DATA default ~/forge-data |
| 31 | # FORGE_BACKUP_NGINX default /etc/nginx |
| 32 | # FORGE_BACKUP_RAD_STORAGE default ~/.radicle/storage |
| 33 | # Host tools: GNU tar, gzip, age 1.x, sha256sum, flock. Omega replacement: an Omega archiver once the |
| 34 | # seeded runtime has one; encryption stays on age (vetted crypto, Drake's rule). |
| 35 | set -euo pipefail |
| 36 | umask 077 |
| 37 | export LC_ALL=C |
| 38 | SPOOL=${FORGE_BACKUP_SPOOL:-$HOME/forge-backups} |
| 39 | KEEP=${FORGE_BACKUP_KEEP:-60} |
| 40 | FD=${FORGE_BACKUP_FORGE_DATA:-$HOME/forge-data} |
| 41 | NGX=${FORGE_BACKUP_NGINX:-/etc/nginx} |
| 42 | RAD=${FORGE_BACKUP_RAD_STORAGE:-$HOME/.radicle/storage} |
| 43 | RECIPIENT=${FORGE_BACKUP_RECIPIENT:-} |
| 44 | |
| 45 | die() { echo "forge-backup: $1" >&2; exit "${2:-1}"; } |
| 46 | recipient_ok() { [[ $RECIPIENT =~ ^age1[02-9ac-hj-np-z]{58}$ ]]; } |
| 47 | |
| 48 | # tar argument list: -C <parent> <member> per source, excludes before members. |
| 49 | build_args() { |
| 50 | ARGS=(--sort=name --numeric-owner --ignore-failed-read --warning=no-file-changed --anchored) |
| 51 | local fdb ngb radp radb rid |
| 52 | fdb=$(basename "$FD"); ngb=$(basename "$NGX"); radp=$(dirname "$RAD"); radb=$(basename "$RAD") |
| 53 | for x in tmp logs archives collab search social.key '*.pid'; do ARGS+=("--exclude=$fdb/$x"); done |
| 54 | ARGS+=("--exclude=$ngb/certs") |
| 55 | ARGS+=(-C "$WORK" MANIFEST.txt) |
| 56 | [ -d "$FD" ] && ARGS+=(-C "$(dirname "$FD")" "$fdb") |
| 57 | [ -d "$NGX" ] && ARGS+=(-C "$(dirname "$NGX")" "$ngb") |
| 58 | RADM=() |
| 59 | if [ -d "$RAD" ]; then |
| 60 | for rid in "$RAD"/*/; do |
| 61 | rid=$(basename "$rid") |
| 62 | [ -d "$RAD/$rid/refs" ] && RADM+=("$radb/$rid/refs") |
| 63 | [ -f "$RAD/$rid/packed-refs" ] && RADM+=("$radb/$rid/packed-refs") |
| 64 | done |
| 65 | [ ${#RADM[@]} -gt 0 ] && ARGS+=(-C "$radp" "${RADM[@]}") |
| 66 | fi |
| 67 | { |
| 68 | echo "forge-backup manifest (format 1)" |
| 69 | echo "host: $(hostname)" |
| 70 | echo "$fdb/ <- $FD (excluded: tmp/ logs/ archives/ collab/ search/ *.pid social.key)" |
| 71 | echo "$ngb/ <- $NGX (excluded: certs/; unreadable files skipped)" |
| 72 | echo "$radb/<rid>/refs, $radb/<rid>/packed-refs <- $RAD (${#RADM[@]} members; no objects, no keys)" |
| 73 | } >"$WORK/MANIFEST.txt" |
| 74 | touch -d @0 "$WORK/MANIFEST.txt" |
| 75 | } |
| 76 | |
| 77 | with_work() { WORK=$(mktemp -d); trap 'rm -rf -- "$WORK"' EXIT; } |
| 78 | |
| 79 | # Member list (tar -tv) on stdin; exit 4 when a secret path is in it. Used by --dry-run and run. |
| 80 | refuse_secrets() { |
| 81 | if grep -E '(^| )([^ ]*/)?(social\.key|keys/|\.radicle/keys)' >/dev/null; then |
| 82 | die "${1}a secret path would be included" 4 |
| 83 | fi |
| 84 | } |
| 85 | |
| 86 | do_dry() { |
| 87 | with_work; build_args |
| 88 | echo "dry-run: nothing written; spool would be $SPOOL" |
| 89 | local list |
| 90 | list=$(tar -c -f - "${ARGS[@]}" 2>/dev/null | tar -t -v -f - ) || true |
| 91 | echo "members: $(printf '%s\n' "$list" | grep -c .) file bytes before tar and gzip: $(printf '%s\n' "$list" | awk '{s += $3} END {print s + 0}')" |
| 92 | # Members per top-level dir (a symlink's " -> target" is cut first). |
| 93 | printf '%s\n' "$list" | sed 's/ -> .*//' | awk 'NF {print $NF}' | awk -F/ '{print $1}' | sort | uniq -c |
| 94 | printf '%s\n' "$list" | refuse_secrets "dry-run: " |
| 95 | echo "secret paths: none (social.key, keys/, objects/ not in the list: $(printf '%s\n' "$list" | grep -c '/objects/') objects entries)" |
| 96 | recipient_ok || die "dry-run: no valid FORGE_BACKUP_RECIPIENT; a run would refuse (fail closed)" 3 |
| 97 | echo "recipient: $RECIPIENT" |
| 98 | } |
| 99 | |
| 100 | do_run() { |
| 101 | recipient_ok || die "refusing: no valid FORGE_BACKUP_RECIPIENT (age1...); nothing read or written" 3 |
| 102 | [ -L "$SPOOL" ] && die "spool must not be a symlink: $SPOOL" |
| 103 | mkdir -p -m 700 "$SPOOL" |
| 104 | exec 9>>"$SPOOL/.lock" |
| 105 | flock -n 9 || die "another run holds $SPOOL/.lock" |
| 106 | with_work; build_args |
| 107 | # Content digest: same members with every mtime set to 0, so touched-but-equal files do not count. |
| 108 | local digest prev stamp final rc |
| 109 | digest=$(tar -c -f - --mtime=@0 "${ARGS[@]}" 2>/dev/null | sha256sum | cut -d' ' -f1) |
| 110 | prev=$(cat "$SPOOL/.last-content" 2>/dev/null || true) |
| 111 | if [ "$digest" = "$prev" ]; then echo "forge-backup: unchanged since the last archive"; return 0; fi |
| 112 | # Same secret-path guard as --dry-run, before anything is written to the spool. |
| 113 | { tar -c -f - "${ARGS[@]}" 2>/dev/null | tar -t -v -f - >"$WORK/list" |
| 114 | rc=("${PIPESTATUS[@]}"); } || true |
| 115 | [ "${rc[0]}" -le 1 ] && [ "${rc[1]}" -eq 0 ] || die "member listing failed (tar=${rc[0]})" |
| 116 | refuse_secrets "refusing: " <"$WORK/list" |
| 117 | stamp=$(date -u +%Y%m%dT%H%M%SZ) |
| 118 | final=$SPOOL/forge-backup-$stamp.tar.gz.age |
| 119 | [ -e "$final" ] && die "exists: $final" |
| 120 | # Inside `{ } || true` errexit is off, so every exit code reaches the check below. |
| 121 | { tar -c -f - "${ARGS[@]}" 2>"$WORK/tar.err" | gzip -n | age -r "$RECIPIENT" -o "$final.partial" |
| 122 | rc=("${PIPESTATUS[@]}"); } || true |
| 123 | # tar 1 = a file changed while read (still a valid archive); anything else is a failure. |
| 124 | if [ "${rc[0]}" -gt 1 ] || [ "${rc[1]}" -ne 0 ] || [ "${rc[2]}" -ne 0 ]; then |
| 125 | rm -f "$final.partial"; sed 's/^/ tar: /' "$WORK/tar.err" >&2 |
| 126 | die "archive failed (tar=${rc[0]} gzip=${rc[1]} age=${rc[2]})" |
| 127 | fi |
| 128 | sync "$final.partial" |
| 129 | (cd "$SPOOL" && sha256sum "$(basename "$final.partial")" | sed 's/\.partial$//' >"$final.sha256") |
| 130 | mv "$final.partial" "$final" |
| 131 | printf '{"file": "%s", "sha256": "%s", "bytes": %s, "created_utc": "%s", "format": "tar+gzip+age"}\n' \ |
| 132 | "$(basename "$final")" "$(cut -d' ' -f1 "$final.sha256")" "$(stat -c %s "$final")" "$stamp" >"$SPOOL/.latest.json.tmp" |
| 133 | mv -f "$SPOOL/.latest.json.tmp" "$SPOOL/latest.json" |
| 134 | printf '%s\n' "$digest" >"$SPOOL/.last-content" |
| 135 | { ls -1 "$SPOOL" | grep -E '^forge-backup-[0-9TZ]+\.tar\.gz\.age$' | sort | head -n "-$KEEP" | |
| 136 | while IFS= read -r old; do rm -f -- "$SPOOL/$old" "$SPOOL/$old.sha256"; done; } || true |
| 137 | echo "forge-backup: wrote $(basename "$final") $(stat -c %s "$final") bytes (tar rc ${rc[0]})" |
| 138 | } |
| 139 | |
| 140 | do_restore() { |
| 141 | local archive=${1:-} identity=${2:-} dir=${3:-} |
| 142 | [ -f "$archive" ] && [ -f "$identity" ] && [ -n "$dir" ] || die "usage: forge-backup.sh restore ARCHIVE IDENTITY DIR" 2 |
| 143 | if [ -e "$dir" ] && [ -n "$(ls -A "$dir")" ]; then die "restore dir is not empty: $dir"; fi |
| 144 | if [ -f "$archive.sha256" ]; then |
| 145 | (cd "$(dirname "$archive")" && sha256sum -c --quiet "$(basename "$archive").sha256") || die "checksum MISMATCH: $archive" |
| 146 | echo "checksum: ok" |
| 147 | else |
| 148 | echo "checksum: no .sha256 next to the archive, skipped" |
| 149 | fi |
| 150 | mkdir -p -m 700 "$dir" |
| 151 | local rc |
| 152 | { age -d -i "$identity" "$archive" | tar -x -z -f - -C "$dir" --no-same-owner |
| 153 | rc=("${PIPESTATUS[@]}"); } || true |
| 154 | [ "${rc[0]}" = 0 ] || die "decryption FAILED (wrong identity or corrupt archive)" |
| 155 | [ "${rc[1]}" = 0 ] || die "unpack FAILED" |
| 156 | cat "$dir/MANIFEST.txt" |
| 157 | echo "restored into $dir: $(find "$dir" -type f | wc -l) files" |
| 158 | } |
| 159 | |
| 160 | case ${1:-run} in |
| 161 | run) do_run ;; |
| 162 | --dry-run) do_dry ;; |
| 163 | restore) shift; do_restore "$@" ;; |
| 164 | *) die "usage: forge-backup.sh [run | --dry-run | restore ARCHIVE IDENTITY DIR]" 2 ;; |
| 165 | esac |