Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/infra/spark/forgejo/forgejo-backup # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / infra / spark / forgejo / forgejo-backup` 276 lines ยท 13889 bytes Raw Blame History 1 #!/bin/sh 2 # forgejo-backup: consistent, encrypted Forgejo snapshot kept on the Spark. 3 # 4 # Shell replacement (2026-10-15) for backup-forgejo.py (2026-09-08). Same behaviour: 5 # - refuse a symlinked destination; create it private (0700); umask 077 6 # - one run at a time (non-blocking lock on DEST/.lock) 7 # - require the encrypted (gocryptfs) data mount and a healthy Forgejo 8 # - stop Forgejo, stream tar of the data dir (minus ./log and ./tmp) straight into 9 # age for the recovery recipient, then ALWAYS start Forgejo again 10 # Differences from the Python original: it stops BOTH Forgejo units (system 11 # atlas-forgejo.service and user atlas-forgejo-web.service share one data dir since 12 # 2026-10-11; the old script stopped only the system one, which was not the one 13 # serving), calls systemctl directly instead of the Python helper atlas-agent-admin, 14 # checks health over HTTPS after restart, and deletes a failed .partial file. 15 # - fsync, sha256, rename the .partial file, write latest.json atomically, 16 # keep the newest 30 snapshots 17 # 2026-11-09 (ipl-ops, infra/spark/forgejo/forgejo-backup.patch): on 2026-11-08 the 18 # runtime dir behind ~/atlas-forgejo-setup-`20260904` had been deleted while Forgejo kept 19 # running from the deleted file; this script stopped it, the restart failed with 20 # 203/EXEC, and the finished snapshot was thrown away. Now: 21 # - preflight: before stopping anything, the ExecStart binaries of the Forgejo and 22 # storage units (and of the user unit when it is running) must exist and be 23 # executable, or the run refuses (exit 1) with Forgejo left running 24 # - a restart failure no longer discards the snapshot: a complete archive is kept and 25 # recorded, retention is skipped, and the run still exits 1 naming the failure 26 # - tar and age exit codes are always logged 27 # - `forgejo-backup --dry-run` runs the preflight and the read-only checks only 28 # The age recipient below is a PUBLIC key. Its private half (the recovery identity) 29 # lives only on the operator Mac; this script never reads or needs it. 30 # 31 # Usage: 32 # forgejo-backup take a snapshot (timer, or aien-run-forgejo-backup) 33 # forgejo-backup --dry-run preflight only: binaries, mount, health; stops nothing 34 # forgejo-backup verify ARCHIVE [IDENTITY] 35 # Checks the archive. Without IDENTITY: checksum against latest.json (when it is 36 # the latest snapshot) and age header. With IDENTITY (an age identity file): 37 # also decrypts and unpacks into RAM (/dev/shm), runs SQLite integrity_check on 38 # the Forgejo database, counts repositories, then deletes the restore. 39 # Exit status is non-zero on any failure. 40 # 41 # Test overrides (environment): FORGEJO_BACKUP_DEST, FORGEJO_BACKUP_RECIPIENT, 42 # FORGEJO_BACKUP_SYS_UNIT, FORGEJO_BACKUP_STORAGE_UNIT, FORGEJO_BACKUP_USER_UNIT. 43 44 set -eu 45 umask 077 46 PATH=/usr/local/bin:/usr/bin:/bin 47 export PATH LC_ALL=C 48 49 ROOT=/home/drakestapleton 50 DEST=${FORGEJO_BACKUP_DEST:-$ROOT/atlas-forgejo-backups} 51 DATA=$ROOT/atlas-forgejo/data 52 RECIPIENT=${FORGEJO_BACKUP_RECIPIENT:-age1kj2a3s98qgu9nc7nlxd7npj50ekgqh32jfdp6jg9dgn2v6scrccqus5ec6} 53 SYS_UNIT=${FORGEJO_BACKUP_SYS_UNIT:-atlas-forgejo.service} # system unit (needs sudo to stop/start) 54 STORAGE_UNIT=${FORGEJO_BACKUP_STORAGE_UNIT:-atlas-forgejo-storage.service} # system unit: gocryptfs mount 55 USER_UNIT=${FORGEJO_BACKUP_USER_UNIT:-atlas-forgejo-web.service} # user unit; same data dir 56 HEALTH=https://127.0.0.1:3443/api/healthz 57 KEEP=30 58 DB_REL=data/forgejo.db # [database] PATH in app.ini, relative to DATA 59 REPOS_REL=git/repositories # [repository] ROOT in app.ini, relative to DATA 60 61 die() { echo "forgejo-backup: $*" >&2; exit 1; } 62 63 healthy() { curl -sk -m 5 "$HEALTH" 2>/dev/null | grep -q '"status": *"pass"'; } 64 65 # ExecStart binary of a unit ($1 = sys|user, $2 = unit), from systemd's own parse. 66 exec_path() { 67 if [ "$1" = sys ]; then v=$(systemctl show -p ExecStart --value "$2" 2>/dev/null || true) 68 else v=$(systemctl --user show -p ExecStart --value "$2" 2>/dev/null || true); fi 69 printf '%s\n' "$v" | sed -n 's/^{ path=\([^ ;]*\) ;.*/\1/p' | head -n 1 70 } 71 # Refuse before stopping anything when a restart could not exec (the 2026-11-08 outage). 72 preflight() { 73 bad=0 74 specs="sys:$SYS_UNIT sys:$STORAGE_UNIT" 75 # The user unit is only started again when it was running, so only then must it exec. 76 if unit_running user; then specs="$specs user:$USER_UNIT" 77 else echo "preflight: $USER_UNIT (user): not running, not checked"; fi 78 for spec in $specs; do 79 set -- "${spec%%:*}" "${spec#*:}" 80 p=$(exec_path "$1" "$2") 81 if [ -z "$p" ]; then echo "preflight: $2 ($1): no ExecStart found, not checked"; continue; fi 82 if [ -x "$p" ]; then echo "preflight: $2 ($1): ok $p" 83 else echo "preflight: $2 ($1): MISSING $p" >&2; bad=1; fi 84 done 85 [ "$bad" = 0 ] || die "refusing to stop Forgejo: a unit binary is missing, the restart would fail (203/EXEC)" 86 } 87 88 # Since 2026-10-11 two units run Forgejo on the same data dir; whichever starts first 89 # holds the data lock and the other restart-loops. A consistent snapshot must stop both. 90 unit_ctl() { # $1 = sys|user, $2 = stop|start ; same 190 s limit as the old script 91 if [ "$1" = sys ]; then 92 timeout 190 sudo -n /usr/bin/systemctl "$2" "$SYS_UNIT" 93 else 94 timeout 190 systemctl --user "$2" "$USER_UNIT" 95 fi 96 } 97 unit_running() { # active or restart-looping counts as "was running" 98 if [ "$1" = sys ]; then s=$(systemctl is-active "$SYS_UNIT" || true) 99 else s=$(systemctl --user is-active "$USER_UNIT" || true); fi 100 [ "$s" = active ] || [ "$s" = activating ] || [ "$s" = reloading ] 101 } 102 forgejo_stop() { 103 first=sys; second=user # start order: the unit serving now goes first 104 pid=$(systemctl --user show -p MainPID --value "$USER_UNIT" 2>/dev/null || echo 0) 105 if [ "${pid:-0}" != 0 ] && grep -q 'forgejo' "/proc/$pid/cmdline" 2>/dev/null; then 106 first=user; second=sys 107 fi 108 sys_was=0; user_was=0 109 unit_running sys && sys_was=1 110 unit_running user && user_was=1 111 stopped=1 112 rc=0 113 # atlas-forgejo-storage.service has Upholds=atlas-forgejo.service (2026-11-10); 114 # a /run ConditionPathExists hold keeps Upholds from restarting Forgejo mid-snapshot and vanishes on reboot. 115 sudo -n mkdir -p /run/systemd/system/atlas-forgejo.service.d || rc=1 116 printf '[Unit]\nConditionPathExists=!/run/forgejo-backup.hold\n' | sudo -n tee /run/systemd/system/atlas-forgejo.service.d/zz-backup-hold.conf >/dev/null || rc=1 117 sudo -n touch /run/forgejo-backup.hold || rc=1 118 timeout 60 sudo -n /usr/bin/systemctl daemon-reload >/dev/null 2>&1 || rc=1 119 unit_ctl sys stop || rc=1 120 unit_ctl user stop || rc=1 121 [ "$rc" = 0 ] || die "could not stop Forgejo" 122 w=0 123 while pgrep -f -- " web --work-path $DATA" >/dev/null; do 124 w=$((w + 1)) 125 [ "$w" -le 50 ] || die "a Forgejo process still uses $DATA" 126 sleep 0.2 127 done 128 } 129 forgejo_start() { # bring units back in the original order; first one must turn healthy 130 rc=0 131 sudo -n rm -f /run/forgejo-backup.hold || rc=1 132 eval "was=\$${first}_was" 133 if [ "$was" = 1 ]; then unit_ctl "$first" start || rc=1; fi 134 i=0 135 until healthy; do 136 i=$((i + 1)); [ "$i" -le 60 ] || { echo "forgejo-backup: Forgejo not healthy 60 s after restart" >&2; rc=1; break; } 137 sleep 1 138 done 139 eval "was=\$${second}_was" 140 if [ "$was" = 1 ]; then 141 if [ "$second" = sys ]; then timeout 190 sudo -n /usr/bin/systemctl --no-block start "$SYS_UNIT" || rc=1 142 else timeout 190 systemctl --user --no-block start "$USER_UNIT" || rc=1; fi 143 fi 144 return "$rc" 145 } 146 147 snapshot() { 148 [ -L "$DEST" ] && die "backup destination must not be a symlink: $DEST" 149 mkdir -p -m 700 "$DEST" 150 exec 9>>"$DEST/.lock" 151 flock -n 9 || die "another backup run holds $DEST/.lock" 152 preflight 153 mountpoint -q "$DATA" || die "encrypted Forgejo storage is not mounted: $DATA" 154 healthy || die "Forgejo is not healthy before the snapshot ($HEALTH)" 155 156 stamp=$(date -u +%Y%m%dT%H%M%S.%6NZ) 157 final=$DEST/forgejo-$stamp.tar.age 158 partial=$final.partial 159 rcdir=$(mktemp -d) 160 stopped=0 161 done_ok=0 162 cleanup() { 163 if [ "$stopped" = 1 ]; then 164 forgejo_start || echo "forgejo-backup: FAILED to restart Forgejo" >&2 165 fi 166 [ "$done_ok" = 1 ] || rm -f "$partial" 167 rm -rf "$rcdir" 168 } 169 trap cleanup EXIT 170 trap 'exit 1' INT TERM HUP 171 172 # Always attempt restart (via cleanup), including when stopping fails or times out. 173 forgejo_stop 174 set -C # create the partial file exclusively, never overwrite 175 { 176 timeout 600 tar --exclude=./log --exclude=./tmp -C "$DATA" -cf - . \ 177 && echo 0 >"$rcdir/tar" || echo $? >"$rcdir/tar" 178 } | timeout 630 age -r "$RECIPIENT" >"$partial" \ 179 && echo 0 >"$rcdir/age" || echo $? >"$rcdir/age" 180 set +C 181 tar_rc=$(cat "$rcdir/tar") age_rc=$(cat "$rcdir/age") 182 echo "forgejo-backup: tar=$tar_rc age=$age_rc" 183 # Restart now, before checksumming, to keep the outage as short as before. A failed 184 # restart is reported at the end; it no longer throws away a complete snapshot. 185 stopped=0 186 restart_ok=1 187 forgejo_start || { restart_ok=0; echo "forgejo-backup: Forgejo restart FAILED after the snapshot" >&2; } 188 [ "$tar_rc" = 0 ] && [ "$age_rc" = 0 ] \ 189 || die "snapshot or encryption failed (tar=$tar_rc age=$age_rc)" 190 sync "$partial" 191 192 sha=$(sha256sum "$partial" | cut -d' ' -f1) 193 bytes=$(stat -c %s "$partial") 194 mv "$partial" "$final" 195 done_ok=1 196 name=${final##*/} 197 printf '{\n "file": "%s",\n "sha256": "%s",\n "bytes": %s,\n "created_utc": "%s",\n "format": "tar+age",\n "consistent_service_stop": true\n}\n' \ 198 "$name" "$sha" "$bytes" "$stamp" >"$DEST/.latest.json.tmp" 199 mv -f "$DEST/.latest.json.tmp" "$DEST/latest.json" 200 sync "$DEST" 201 202 printf '{"file": "%s", "sha256": "%s", "bytes": %s, "created_utc": "%s", "format": "tar+age", "consistent_service_stop": true}\n' \ 203 "$name" "$sha" "$bytes" "$stamp" 204 [ "$restart_ok" = 1 ] || die "snapshot $name kept, but Forgejo did not come back: restart it by hand (retention skipped)" 205 206 # Retention: keep the newest $KEEP regular files (names sort by UTC time). 207 ls -1 "$DEST" | grep -E '^forgejo-.*\.tar\.age$' | sort | head -n "-$KEEP" | 208 while IFS= read -r stale; do 209 if [ -f "$DEST/$stale" ] && [ ! -L "$DEST/$stale" ]; then rm -f "$DEST/$stale"; fi 210 done 211 } 212 213 dry_run() { 214 echo "dry-run: nothing is stopped, written or deleted" 215 preflight 216 if mountpoint -q "$DATA"; then echo "mount: ok $DATA"; else echo "mount: NOT MOUNTED $DATA (a run would refuse)"; fi 217 if healthy; then echo "health: ok $HEALTH"; else echo "health: FAIL $HEALTH (a run would refuse)"; fi 218 if [ -f "$DEST/latest.json" ]; then echo "latest: $(sed -n 's/.*"file": "\([^"]*\)".*/\1/p' "$DEST/latest.json")"; fi 219 } 220 221 verify() { 222 archive=${1:-}; identity=${2:-} 223 [ -n "$archive" ] && [ -f "$archive" ] || die "verify: archive not found: $archive" 224 name=${archive##*/} 225 # 1. Checksum against latest.json when this archive is the latest snapshot. 226 if [ -f "$DEST/latest.json" ] && grep -q "\"file\": \"$name\"" "$DEST/latest.json"; then 227 want=$(sed -n 's/.*"sha256": "\([0-9a-f]*\)".*/\1/p' "$DEST/latest.json") 228 got=$(sha256sum "$archive" | cut -d' ' -f1) 229 [ "$want" = "$got" ] || die "verify: checksum MISMATCH for $name" 230 echo "checksum: ok (matches latest.json)" 231 else 232 echo "checksum: skipped (not the snapshot named in latest.json)" 233 fi 234 # 2. age header. 235 [ "$(head -c 21 "$archive")" = "age-encryption.org/v1" ] || die "verify: not an age file: $archive" 236 echo "age header: ok" 237 if [ -z "$identity" ]; then 238 echo "content: not checked (no identity given; the recovery identity lives on the operator Mac)" 239 return 0 240 fi 241 [ -f "$identity" ] || die "verify: identity file not found" 242 # 3. Decrypt and unpack into RAM, then check the database and repositories. 243 work=$(mktemp -d /dev/shm/forgejo-restore.XXXXXX) 244 trap 'rm -rf "$work"' EXIT 245 trap 'exit 1' INT TERM HUP 246 mkdir "$work/data" 247 { 248 age -d -i "$identity" "$archive" && echo 0 >"$work/age" || echo $? >"$work/age" 249 } | tar -xf - -C "$work/data" && echo 0 >"$work/tar" || echo $? >"$work/tar" 250 [ "$(cat "$work/age")" = 0 ] || die "verify: decryption FAILED (corrupt archive or wrong identity)" 251 [ "$(cat "$work/tar")" = 0 ] || die "verify: unpack FAILED" 252 db=$work/data/$DB_REL 253 [ -f "$db" ] || die "verify: database missing from archive ($DB_REL)" 254 ic=$(sqlite3 -readonly "$db" 'PRAGMA integrity_check' 2>&1) || die "verify: database will not open" 255 [ "$ic" = ok ] || die "verify: database integrity_check FAILED: $(echo "$ic" | head -3)" 256 echo "database integrity_check: ok ($(du -h "$db" | cut -f1))" 257 repos=$work/data/$REPOS_REL 258 [ -d "$repos" ] || die "verify: repositories directory missing ($REPOS_REL)" 259 count=$(find "$repos" -mindepth 2 -maxdepth 2 -type d -name '*.git' | wc -l) 260 [ "$count" -gt 0 ] || die "verify: no repositories in archive" 261 bad=0 262 for r in $(find "$repos" -mindepth 2 -maxdepth 2 -type d -name '*.git' | sort); do 263 [ -f "$r/HEAD" ] && [ -d "$r/objects" ] || { echo " BROKEN ${r#"$repos"/}"; bad=$((bad + 1)); } 264 printf ' %-8s %s\n' "$(du -sh "$r" | cut -f1)" "${r#"$repos"/}" 265 done 266 [ "$bad" = 0 ] || die "verify: $bad repositories are missing HEAD or objects" 267 echo "repositories: $count present, total $(du -sh "$repos" | cut -f1)" 268 echo "verify: PASS" 269 } 270 271 case ${1:-} in 272 "") snapshot ;; 273 --dry-run) dry_run ;; 274 verify) shift; verify "$@" ;; 275 *) die "usage: forgejo-backup [--dry-run | verify ARCHIVE [IDENTITY]]" ;; 276 esac