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.patch # 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.patch` 164 lines ยท 8699 bytes Raw Blame History 1 --- a/forgejo-backup 2 +++ b/forgejo-backup 3 @@ -14,11 +14,23 @@ 4 # checks health over HTTPS after restart, and deletes a failed .partial file. 5 # - fsync, sha256, rename the .partial file, write latest.json atomically, 6 # keep the newest 30 snapshots 7 +# 2026-11-09 (ipl-ops, infra/spark/forgejo/forgejo-backup.patch): on 2026-11-08 the 8 +# runtime dir behind ~/atlas-forgejo-setup-`20260904` had been deleted while Forgejo kept 9 +# running from the deleted file; this script stopped it, the restart failed with 10 +# 203/EXEC, and the finished snapshot was thrown away. Now: 11 +# - preflight: before stopping anything, the ExecStart binaries of the Forgejo and 12 +# storage units (and of the user unit when it is running) must exist and be 13 +# executable, or the run refuses (exit 1) with Forgejo left running 14 +# - a restart failure no longer discards the snapshot: a complete archive is kept and 15 +# recorded, retention is skipped, and the run still exits 1 naming the failure 16 +# - tar and age exit codes are always logged 17 +# - `forgejo-backup --dry-run` runs the preflight and the read-only checks only 18 # The age recipient below is a PUBLIC key. Its private half (the recovery identity) 19 # lives only on the operator Mac; this script never reads or needs it. 20 # 21 # Usage: 22 -# forgejo-backup take a snapshot (what the nightly timer runs) 23 +# forgejo-backup take a snapshot (timer, or aien-run-forgejo-backup) 24 +# forgejo-backup --dry-run preflight only: binaries, mount, health; stops nothing 25 # forgejo-backup verify ARCHIVE [IDENTITY] 26 # Checks the archive. Without IDENTITY: checksum against latest.json (when it is 27 # the latest snapshot) and age header. With IDENTITY (an age identity file): 28 @@ -26,7 +38,8 @@ 29 # the Forgejo database, counts repositories, then deletes the restore. 30 # Exit status is non-zero on any failure. 31 # 32 -# Test overrides (environment): FORGEJO_BACKUP_DEST, FORGEJO_BACKUP_RECIPIENT. 33 +# Test overrides (environment): FORGEJO_BACKUP_DEST, FORGEJO_BACKUP_RECIPIENT, 34 +# FORGEJO_BACKUP_SYS_UNIT, FORGEJO_BACKUP_STORAGE_UNIT, FORGEJO_BACKUP_USER_UNIT. 35 36 set -eu 37 umask 077 38 @@ -37,8 +50,9 @@ 39 DEST=${FORGEJO_BACKUP_DEST:-$ROOT/atlas-forgejo-backups} 40 DATA=$ROOT/atlas-forgejo/data 41 RECIPIENT=${FORGEJO_BACKUP_RECIPIENT:-age1kj2a3s98qgu9nc7nlxd7npj50ekgqh32jfdp6jg9dgn2v6scrccqus5ec6} 42 -SYS_UNIT=atlas-forgejo.service # system unit (needs sudo to stop/start) 43 -USER_UNIT=atlas-forgejo-web.service # user unit; both point at the same data dir 44 +SYS_UNIT=${FORGEJO_BACKUP_SYS_UNIT:-atlas-forgejo.service} # system unit (needs sudo to stop/start) 45 +STORAGE_UNIT=${FORGEJO_BACKUP_STORAGE_UNIT:-atlas-forgejo-storage.service} # system unit: gocryptfs mount 46 +USER_UNIT=${FORGEJO_BACKUP_USER_UNIT:-atlas-forgejo-web.service} # user unit; same data dir 47 HEALTH=https://127.0.0.1:3443/api/healthz 48 KEEP=30 49 DB_REL=data/forgejo.db # [database] PATH in app.ini, relative to DATA 50 @@ -48,6 +62,29 @@ 51 52 healthy() { curl -sk -m 5 "$HEALTH" 2>/dev/null | grep -q '"status": *"pass"'; } 53 54 +# ExecStart binary of a unit ($1 = sys|user, $2 = unit), from systemd's own parse. 55 +exec_path() { 56 + if [ "$1" = sys ]; then v=$(systemctl show -p ExecStart --value "$2" 2>/dev/null || true) 57 + else v=$(systemctl --user show -p ExecStart --value "$2" 2>/dev/null || true); fi 58 + printf '%s\n' "$v" | sed -n 's/^{ path=\([^ ;]*\) ;.*/\1/p' | head -n 1 59 +} 60 +# Refuse before stopping anything when a restart could not exec (the 2026-11-08 outage). 61 +preflight() { 62 + bad=0 63 + specs="sys:$SYS_UNIT sys:$STORAGE_UNIT" 64 + # The user unit is only started again when it was running, so only then must it exec. 65 + if unit_running user; then specs="$specs user:$USER_UNIT" 66 + else echo "preflight: $USER_UNIT (user): not running, not checked"; fi 67 + for spec in $specs; do 68 + set -- "${spec%%:*}" "${spec#*:}" 69 + p=$(exec_path "$1" "$2") 70 + if [ -z "$p" ]; then echo "preflight: $2 ($1): no ExecStart found, not checked"; continue; fi 71 + if [ -x "$p" ]; then echo "preflight: $2 ($1): ok $p" 72 + else echo "preflight: $2 ($1): MISSING $p" >&2; bad=1; fi 73 + done 74 + [ "$bad" = 0 ] || die "refusing to stop Forgejo: a unit binary is missing, the restart would fail (203/EXEC)" 75 +} 76 + 77 # Since 2026-10-11 two units run Forgejo on the same data dir; whichever starts first 78 # holds the data lock and the other restart-loops. A consistent snapshot must stop both. 79 unit_ctl() { # $1 = sys|user, $2 = stop|start ; same 190 s limit as the old script 80 @@ -73,13 +110,25 @@ 81 unit_running user && user_was=1 82 stopped=1 83 rc=0 84 + # atlas-forgejo-storage.service has Upholds=atlas-forgejo.service (2026-11-10); 85 + # a /run ConditionPathExists hold keeps Upholds from restarting Forgejo mid-snapshot and vanishes on reboot. 86 + sudo -n mkdir -p /run/systemd/system/atlas-forgejo.service.d || rc=1 87 + 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 88 + sudo -n touch /run/forgejo-backup.hold || rc=1 89 + timeout 60 sudo -n /usr/bin/systemctl daemon-reload >/dev/null 2>&1 || rc=1 90 unit_ctl sys stop || rc=1 91 unit_ctl user stop || rc=1 92 [ "$rc" = 0 ] || die "could not stop Forgejo" 93 - if pgrep -f -- " web --work-path $DATA" >/dev/null; then die "a Forgejo process still uses $DATA"; fi 94 + w=0 95 + while pgrep -f -- " web --work-path $DATA" >/dev/null; do 96 + w=$((w + 1)) 97 + [ "$w" -le 50 ] || die "a Forgejo process still uses $DATA" 98 + sleep 0.2 99 + done 100 } 101 forgejo_start() { # bring units back in the original order; first one must turn healthy 102 rc=0 103 + sudo -n rm -f /run/forgejo-backup.hold || rc=1 104 eval "was=\$${first}_was" 105 if [ "$was" = 1 ]; then unit_ctl "$first" start || rc=1; fi 106 i=0 107 @@ -100,6 +149,7 @@ 108 mkdir -p -m 700 "$DEST" 109 exec 9>>"$DEST/.lock" 110 flock -n 9 || die "another backup run holds $DEST/.lock" 111 + preflight 112 mountpoint -q "$DATA" || die "encrypted Forgejo storage is not mounted: $DATA" 113 healthy || die "Forgejo is not healthy before the snapshot ($HEALTH)" 114 115 @@ -129,9 +179,12 @@ 116 && echo 0 >"$rcdir/age" || echo $? >"$rcdir/age" 117 set +C 118 tar_rc=$(cat "$rcdir/tar") age_rc=$(cat "$rcdir/age") 119 - # Restart now, before checksumming, to keep the outage as short as before. 120 + echo "forgejo-backup: tar=$tar_rc age=$age_rc" 121 + # Restart now, before checksumming, to keep the outage as short as before. A failed 122 + # restart is reported at the end; it no longer throws away a complete snapshot. 123 stopped=0 124 - forgejo_start || die "Forgejo restart failed after the snapshot" 125 + restart_ok=1 126 + forgejo_start || { restart_ok=0; echo "forgejo-backup: Forgejo restart FAILED after the snapshot" >&2; } 127 [ "$tar_rc" = 0 ] && [ "$age_rc" = 0 ] \ 128 || die "snapshot or encryption failed (tar=$tar_rc age=$age_rc)" 129 sync "$partial" 130 @@ -146,13 +199,23 @@ 131 mv -f "$DEST/.latest.json.tmp" "$DEST/latest.json" 132 sync "$DEST" 133 134 + printf '{"file": "%s", "sha256": "%s", "bytes": %s, "created_utc": "%s", "format": "tar+age", "consistent_service_stop": true}\n' \ 135 + "$name" "$sha" "$bytes" "$stamp" 136 + [ "$restart_ok" = 1 ] || die "snapshot $name kept, but Forgejo did not come back: restart it by hand (retention skipped)" 137 + 138 # Retention: keep the newest $KEEP regular files (names sort by UTC time). 139 ls -1 "$DEST" | grep -E '^forgejo-.*\.tar\.age$' | sort | head -n "-$KEEP" | 140 while IFS= read -r stale; do 141 if [ -f "$DEST/$stale" ] && [ ! -L "$DEST/$stale" ]; then rm -f "$DEST/$stale"; fi 142 done 143 - printf '{"file": "%s", "sha256": "%s", "bytes": %s, "created_utc": "%s", "format": "tar+age", "consistent_service_stop": true}\n' \ 144 - "$name" "$sha" "$bytes" "$stamp" 145 +} 146 + 147 +dry_run() { 148 + echo "dry-run: nothing is stopped, written or deleted" 149 + preflight 150 + if mountpoint -q "$DATA"; then echo "mount: ok $DATA"; else echo "mount: NOT MOUNTED $DATA (a run would refuse)"; fi 151 + if healthy; then echo "health: ok $HEALTH"; else echo "health: FAIL $HEALTH (a run would refuse)"; fi 152 + if [ -f "$DEST/latest.json" ]; then echo "latest: $(sed -n 's/.*"file": "\([^"]*\)".*/\1/p' "$DEST/latest.json")"; fi 153 } 154 155 verify() { 156 @@ -207,6 +270,7 @@ 157 158 case ${1:-} in 159 "") snapshot ;; 160 + --dry-run) dry_run ;; 161 verify) shift; verify "$@" ;; 162 - *) die "usage: forgejo-backup [verify ARCHIVE [IDENTITY]]" ;; 163 + *) die "usage: forgejo-backup [--dry-run | verify ARCHIVE [IDENTITY]]" ;; 164 esac