Keep one long push from breaking the backup, and lock repos by handle
Browse files`flock PATH` creates an empty file when PATH is missing, so a repository deleted between the listing and the lock left a root-owned file at its name that blocked recreating it, and failed the backup. Lock an open descriptor for the directory instead, and skip it if it's gone. A repository a push holds past the lock wait now keeps the previous snapshot's copy (hard links), so every other repository is still backed up. The snapshot becomes latest, lists what's stale in its MANIFEST, and the run fails so the operator hears. `latest` is now replaced by rename, so a crash can't leave none. The backup tests kept their snapshots inside the folder holding the keys file, so each snapshot copied all earlier ones; they now live beside it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 22:00
- Commit
- 6e8596b44597
- Parent
- e6e0b23
3 files changed +97−11
| @@ -129,7 +129,7 @@ journalctl -u gitv-web -g 'sign-in|password change' | ||
| ## Backups | ||
| `deploy/gitv-backup` snapshots everything the server can't rebuild: the repositories (with their trash, reflogs, push logs and visibility markers), `/etc/gitv` (the SSH keys) and `/var/lib/gitv-web` (the web sign-in). Each snapshot is a plain copy in `/mnt/gitv-backup/gitv/snapshots/<UTC time>/`. Files unchanged since the previous snapshot are hard links to it, so a day's snapshot costs only what changed. Each repository is copied under a lock that pushes also take, so no copy catches a push half way. Every copy is then checked with `git fsck`, and only a snapshot that passes becomes `latest`. Snapshots are kept for 30 days. | ||
| `deploy/gitv-backup` snapshots everything the server can't rebuild: the repositories (with their trash, reflogs, push logs and visibility markers), `/etc/gitv` (the SSH keys) and `/var/lib/gitv-web` (the web sign-in). Each snapshot is a plain copy in `/mnt/gitv-backup/gitv/snapshots/<UTC time>/`. Files unchanged since the previous snapshot are hard links to it, so a day's snapshot costs only what changed. Each repository is copied under a lock that pushes also take, so no copy catches a push half way. A repository a push holds for more than 15 minutes keeps its copy from the previous snapshot (noted in the snapshot's `MANIFEST`), so the rest are still backed up, and the run then fails so you hear about it. Every copy is then checked with `git fsck`, and only a snapshot that passes becomes `latest`. Snapshots are kept for 30 days. | ||
| The backup holds the authenticator secret and the password hash, so it is private to root (mode 0700). | ||
| @@ -27,7 +27,10 @@ impl Setup { | ||
| fs::create_dir_all(&web).unwrap(); | ||
| fs::write(web.join("login"), "user=Um9i\n").unwrap(); | ||
| fs::write(&host.keys, "restrict,command=\"gitv serve ada\" ssh-ed25519 AAAA\n").unwrap(); | ||
| let backups = host.dir.join("backups"); | ||
| // Beside the host, not in it: the keys file's folder is backed up whole, and a backup | ||
| // folder inside it would copy every earlier snapshot into each new one. | ||
| let backups = scratch(&format!("{name}-backups")); | ||
| fs::remove_dir(&backups).unwrap(); | ||
| Setup { host, backups, web } | ||
| } | ||
| @@ -155,18 +158,60 @@ fn a_corrupt_repository_fails_the_backup_and_keeps_latest() { | ||
| } | ||
| #[test] | ||
| fn a_backup_waits_for_a_push_in_progress() { | ||
| fn a_push_in_progress_keeps_its_last_copy_without_holding_up_the_rest() { | ||
| let setup = Setup::new("backup-lock"); | ||
| let work = work("backup-lock-work"); | ||
| setup.push(&work, "project"); | ||
| // Hold the lock as a push does; the backup gives up after its (one second) wait. | ||
| let repo = gitv_core::RepoDir::at(&setup.host.repo("ada", "project")).unwrap(); | ||
| setup.push(&work, "busy"); | ||
| setup.push(&work, "other"); | ||
| // Hold the lock as a push does; the backup gives up on it after its (one second) wait. | ||
| let repo = gitv_core::RepoDir::at(&setup.host.repo("ada", "busy")).unwrap(); | ||
| let held = repo.lock(false).unwrap(); | ||
| // Never backed up before: there is no copy to keep, but the other repository is backed up. | ||
| let out = setup.backup(); | ||
| assert!(!out.status.success(), "an incomplete backup must fail"); | ||
| assert!(stderr(&out).contains("ada/busy.git for over 1s, and no earlier snapshot"), "{}", stderr(&out)); | ||
| let first = setup.latest(); | ||
| assert!(first.join("repos/ada/other.git/HEAD").is_file(), "the other repository was backed up"); | ||
| assert!(!first.join("repos/ada/busy.git").exists()); | ||
| assert!(fs::read_to_string(first.join("MANIFEST")).unwrap().contains("never backed up): ada/busy.git")); | ||
| drop(held); | ||
| // Backed up once, then held again while it moves on: the snapshot keeps the earlier copy. | ||
| let out = setup.backup(); | ||
| assert!(out.status.success(), "{}", stderr(&out)); | ||
| let good = setup.latest(); | ||
| commit(&work, "Second"); | ||
| setup.push(&work, "busy"); | ||
| setup.push(&work, "other"); | ||
| let held = repo.lock(false).unwrap(); | ||
| let out = setup.backup(); | ||
| assert!(!out.status.success()); | ||
| assert!(stderr(&out).contains("held it for over 1s"), "{}", stderr(&out)); | ||
| assert!(stderr(&out).contains("kept its copy from"), "{}", stderr(&out)); | ||
| let latest = setup.latest(); | ||
| assert_ne!(latest, good, "everything else still moves on"); | ||
| let tip = |root: &Path, name: &str| git(&root.join(format!("repos/ada/{name}.git")), &["log", "-1", "--format=%s"]); | ||
| assert_eq!(tip(&latest, "busy"), "First", "the held repository is its last good copy"); | ||
| assert_eq!(tip(&latest, "other"), "Second"); | ||
| let head = |root: &Path| fs::metadata(root.join("repos/ada/busy.git/HEAD")).unwrap().ino(); | ||
| assert_eq!(head(&latest), head(&good), "the kept copy is hard links, not a second copy"); | ||
| drop(held); | ||
| let out = setup.backup(); | ||
| assert!(out.status.success(), "{}", stderr(&out)); | ||
| assert_eq!(tip(&setup.latest(), "busy"), "Second"); | ||
| } | ||
| #[test] | ||
| fn latest_is_replaced_in_one_step() { | ||
| let setup = Setup::new("backup-latest"); | ||
| let work = work("backup-latest-work"); | ||
| setup.push(&work, "project"); | ||
| // A leftover from a run that crashed between making the new link and renaming it. | ||
| fs::create_dir_all(setup.backups.join("gitv")).unwrap(); | ||
| std::os::unix::fs::symlink("snapshots/gone", setup.backups.join("gitv/.latest.new")).unwrap(); | ||
| assert!(setup.backup().status.success()); | ||
| assert!(setup.latest().join("MANIFEST").is_file()); | ||
| assert!(fs::symlink_metadata(setup.backups.join("gitv/.latest.new")).is_err(), "nothing is left beside it"); | ||
| } | ||
| #[test] | ||
| @@ -64,20 +64,52 @@ copy() { | ||
| mkdir -p "$snap/repos" | ||
| copy "$repos/" "$snap/repos/" repos --exclude='/*/*.git/' || die "copying $repos failed" | ||
| # Each repository under its lock, so no push is caught half way. | ||
| # Each repository under its lock, so no push is caught half way. The lock is taken on a | ||
| # descriptor for the directory itself: `flock PATH` would create an empty file at PATH if the | ||
| # repository were deleted after the listing, leaving a file that blocks the name for good. | ||
| # | ||
| # A repository a push holds for longer than lock_wait keeps the previous snapshot's copy (or | ||
| # none, if it has never been backed up), so one long push can't cost every other repository | ||
| # its backup. The snapshot still becomes `latest`, but the run fails so the operator hears. | ||
| count=0 | ||
| stale=() | ||
| missing=() | ||
| while IFS= read -r -d '' repo; do | ||
| rel=${repo#"$repos"/} | ||
| if ! { exec {fd}<"$repo"; } 2>/dev/null; then | ||
| say "$rel was deleted during the backup; skipped" | ||
| continue | ||
| fi | ||
| mkdir -p "$snap/repos/$rel" | ||
| link=() | ||
| if [[ -n $previous && -d $previous/repos/$rel ]]; then link=(--link-dest="$previous/repos/$rel"); fi | ||
| if ! flock -x -w "$lock_wait" "$repo" rsync -a --delete "${link[@]}" "$repo/" "$snap/repos/$rel/"; then | ||
| locked=0 | ||
| flock -E 75 -x -w "$lock_wait" "$fd" || locked=$? | ||
| if ((locked == 75)); then | ||
| exec {fd}<&- | ||
| if [[ -n ${link[*]} ]]; then | ||
| rsync -a "$previous/repos/$rel/" "$snap/repos/$rel/" "${link[@]}" || die "reusing the previous copy of $rel failed" | ||
| say "a push held $rel for over ${lock_wait}s; kept its copy from $(basename "$previous")" | ||
| stale+=("$rel") | ||
| count=$((count + 1)) | ||
| else | ||
| rm -rf "${snap:?}/repos/$rel" | ||
| say "a push held $rel for over ${lock_wait}s, and no earlier snapshot has it; not backed up" | ||
| missing+=("$rel") | ||
| fi | ||
| continue | ||
| fi | ||
| ((locked == 0)) || die "couldn't lock $rel" | ||
| copied=0 | ||
| rsync -a --delete "${link[@]}" "$repo/" "$snap/repos/$rel/" || copied=$? | ||
| exec {fd}<&- | ||
| if ((copied)); then | ||
| if [[ ! -e $repo ]]; then | ||
| say "$rel was deleted during the backup; skipped" | ||
| rm -rf "${snap:?}/repos/$rel" | ||
| continue | ||
| fi | ||
| die "copying $rel failed (or a push held it for over ${lock_wait}s)" | ||
| die "copying $rel failed" | ||
| fi | ||
| count=$((count + 1)) | ||
| done < <(find "$repos" -mindepth 2 -maxdepth 2 -type d -name '*.git' -print0 | sort -z) | ||
| @@ -107,6 +139,8 @@ rm -f "$base/fsck.err" | ||
| echo "snapshot: $stamp" | ||
| echo "host: $(hostname)" | ||
| echo "repositories: $count" | ||
| echo "stale (a push held them; copied from the previous snapshot): ${stale[*]}" | ||
| echo "missing (a push held them; never backed up): ${missing[*]}" | ||
| echo "previous: ${previous:+$(basename "$previous")}" | ||
| echo "$(git --version)" | ||
| } >"$snap/MANIFEST" | ||
| @@ -116,7 +150,9 @@ if ((failed)); then | ||
| die "$failed repositories failed verification; kept as $stamp.failed, latest is unchanged" | ||
| fi | ||
| mv "$snap" "$snapshots/$stamp" | ||
| ln -sfn "snapshots/$stamp" "$base/latest" | ||
| # Made beside `latest` and renamed over it, so a crash never leaves no `latest` at all. | ||
| ln -sfn "snapshots/$stamp" "$base/.latest.new" | ||
| mv -T "$base/.latest.new" "$base/latest" | ||
| say "snapshot $stamp: $count repositories" | ||
| # Retention: snapshots older than keep_days, never the newest. | ||
| @@ -124,3 +160,8 @@ find "$snapshots" -mindepth 1 -maxdepth 1 -type d -mtime +"$keep_days" ! -name " | ||
| while IFS= read -r -d '' old; do | ||
| rm -rf "$old" && say "removed old snapshot $(basename "$old")" | ||
| done | ||
| # Everything else is backed up; now fail, so a held repository shows in `systemctl --failed`. | ||
| if ((${#stale[@]} + ${#missing[@]})); then | ||
| die "incomplete: stale ${stale[*]:-none}; missing ${missing[*]:-none} (pushes held them for over ${lock_wait}s)" | ||
| fi | ||