interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / ops / mac / forge-backup-pull.sh
| 1 | #!/bin/sh |
| 2 | # infra/ops/mac/forge-backup-pull.sh: copy the Spark's encrypted backup spools to this Mac. |
| 3 | # Modelled on com.aien.vault-backup-pull (rsync over ssh into ~/Backups/<name>, 0700, files older |
| 4 | # than 120 days deleted), but driven by change instead of a clock: after each pull it holds one |
| 5 | # ssh session open to the Spark running `inotifywait` on the spool dirs, which returns when a new |
| 6 | # archive is renamed into place (both backup scripts write .partial, then mv) or after MAX_GAP |
| 7 | # seconds. launchd keeps it alive (KeepAlive), so a sleeping or offline Mac pulls on wake. The |
| 8 | # archives are age-encrypted on the Spark; the recovery identity |
| 9 | # stays on this Mac and is never read here. |
| 10 | # |
| 11 | # Usage: forge-backup-pull.sh loop forever (what launchd runs) |
| 12 | # forge-backup-pull.sh --once one pull, then exit |
| 13 | # forge-backup-pull.sh --dry-run rsync -n of every pair, no wait, nothing written |
| 14 | # Env: PULL_HOST (default spark) |
| 15 | # PULL_PAIRS "remote_dir:local_name ..." (default below; remote dirs are relative to the |
| 16 | # Spark home) |
| 17 | # PULL_DEST (default ~/Backups) |
| 18 | # PULL_KEEP_DAYS (default 120), PULL_MAX_GAP (default 86400) |
| 19 | # Host tools: OpenSSH, rsync (macOS /usr/bin/rsync), inotifywait on the Spark, find. Omega |
| 20 | # replacement: an Omega sync client once seeded networking exists. |
| 21 | set -u |
| 22 | HOST=${PULL_HOST:-spark} |
| 23 | PAIRS=${PULL_PAIRS:-forge-backups:forge-spark atlas-forgejo-backups:forgejo-spark} |
| 24 | DEST=${PULL_DEST:-$HOME/Backups} |
| 25 | KEEP_DAYS=${PULL_KEEP_DAYS:-120} |
| 26 | MAX_GAP=${PULL_MAX_GAP:-86400} |
| 27 | SSH='ssh -o BatchMode=yes -o ConnectTimeout=20 -o ServerAliveInterval=60 -o ServerAliveCountMax=3' |
| 28 | mode=${1:-loop} |
| 29 | |
| 30 | pull() { # $1 = -n for a dry run |
| 31 | rc=0 |
| 32 | for pair in $PAIRS; do |
| 33 | rdir=${pair%%:*}; lname=${pair#*:} |
| 34 | if [ "${1:-}" != -n ]; then mkdir -p "$DEST/$lname" && chmod 700 "$DEST/$lname" || { rc=1; continue; }; fi |
| 35 | # Only finished archives: never the .partial being written, never the lock or state files. |
| 36 | if /usr/bin/rsync -a ${1:-} --include='*.age' --include='*.age.sha256' --include='latest.json' \ |
| 37 | --exclude='*' -e "$SSH" "$HOST:$rdir/" "$DEST/$lname/"; then |
| 38 | [ "${1:-}" = -n ] || find "$DEST/$lname" -name '*.age*' -mtime +"$KEEP_DAYS" -delete |
| 39 | echo "$(date '+%F %T') pulled $HOST:$rdir -> $DEST/$lname${1:+ (dry run)}" |
| 40 | else |
| 41 | echo "$(date '+%F %T') FAIL $HOST:$rdir" >&2; rc=1 |
| 42 | fi |
| 43 | done |
| 44 | return $rc |
| 45 | } |
| 46 | |
| 47 | case $mode in |
| 48 | --dry-run) pull -n; exit $? ;; |
| 49 | --once) pull; exit $? ;; |
| 50 | loop) ;; |
| 51 | *) echo "usage: forge-backup-pull.sh [--once|--dry-run]" >&2; exit 2 ;; |
| 52 | esac |
| 53 | |
| 54 | dirs='' |
| 55 | for pair in $PAIRS; do dirs="$dirs ${pair%%:*}"; done |
| 56 | while :; do |
| 57 | pull || sleep 60 |
| 58 | # Block on the Spark until a new archive is renamed into place, or MAX_GAP passes. A missing |
| 59 | # dir or a dropped connection returns at once; the sleep keeps that from spinning. |
| 60 | start=$(date +%s) |
| 61 | $SSH "$HOST" "inotifywait -qq -t $MAX_GAP -e moved_to $dirs" 2>/dev/null |
| 62 | [ $(( $(date +%s) - start )) -lt 5 ] && sleep 60 |
| 63 | done |