GITv

Back up the SSH host keys, and fill gaps in the recovery steps

Browse files

A server rebuilt without its host keys greets every client with "REMOTE HOST IDENTIFICATION HAS CHANGED". The backup now keeps /etc/ssh/ssh_host_* (and nothing else of sshd's), and the recovery steps put them back. The recovery steps also now relabel what they restore for SELinux, and enable all three timers by name. The README notes that the weekly restore check needs room for one more whole snapshot on the volume. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Author
Um9i vulgarmessiah@gmail.com
Date
22 Nov 2026, 22:09
Commit
12b9bf393d38
Parent
4788d2f

3 files changed +32−6

+8−4
@@ -129,9 +129,9 @@ 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. 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.
`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), `/var/lib/gitv-web` (the web sign-in) and the server's SSH host keys (`/etc/ssh/ssh_host_*`), so a rebuilt server is one clients already trust. 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).
The backup holds the authenticator secret, the password hash and the SSH host keys' private halves, so it is private to root (mode 0700).
**The volume.** Create a DigitalOcean block storage volume in the droplet's region (ext4), attach it, and mount it at `/mnt/gitv-backup`:
@@ -154,7 +154,7 @@ systemctl enable --now gitv-backup.timer gitv-restore-check.timer
systemctl start gitv-backup # a first snapshot now
```
The backup runs daily at about 02:30. Every Sunday, `gitv-restore check` copies the latest snapshot out, runs a full `git fsck` on every repository, and deletes the copy, proving the backups restore. A failure of either shows in `systemctl --failed` and `journalctl -u gitv-backup`.
The backup runs daily at about 02:30. Every Sunday, `gitv-restore check` copies the latest snapshot out, runs a full `git fsck` on every repository, and deletes the copy, proving the backups restore. That copy is a full one on the backup volume, so keep free space there for at least one more whole snapshot (`du -sh /mnt/gitv-backup/gitv/latest/`), or the check can fill the volume and break the next backup. A failure of either shows in `systemctl --failed` and `journalctl -u gitv-backup`.
## Disaster recovery
@@ -178,10 +178,14 @@ The backup runs daily at about 02:30. Every Sunday, `gitv-restore check` copies
rsync -a /srv/gitv-restore/repos/ /srv/gitv/repos/
rsync -a /srv/gitv-restore/etc-gitv/ /etc/gitv/
rsync -a /srv/gitv-restore/gitv-web/ /var/lib/gitv-web/
cp -p /srv/gitv-restore/ssh-host-keys/ssh_host_* /etc/ssh/
# Copied files carry their folder's default SELinux label, not the one the policy names
# (sshd reads authorized_keys only as ssh_home_t); restorecon gives each its own.
restorecon -R /srv/gitv /etc/gitv /var/lib/gitv-web /etc/ssh
rm -rf /srv/gitv-restore
```
5. Start `sshd`, `gitv-web` and `caddy`, and enable the timers. Point DNS at the new droplet. Check a clone over SSH and over HTTPS, and sign in to the web browser.
5. Restart `sshd` (it now presents the old host keys), start `gitv-web` and `caddy`, and enable the timers: `systemctl enable --now gitv-purge.timer gitv-backup.timer gitv-restore-check.timer`. Point DNS at the new droplet. Check a clone over SSH and over HTTPS, and sign in to the web browser.
Pushes made after the last snapshot are lost from the server, but every clone still has them: pushing again restores them.
@@ -18,6 +18,7 @@ struct Setup {
host: Host,
backups: PathBuf,
web: PathBuf,
ssh: PathBuf,
}
impl Setup {
@@ -31,7 +32,12 @@ impl Setup {
// 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 }
// Stand-ins for /etc/ssh: host keys, and configuration that isn't backed up.
let ssh = scratch(&format!("{name}-ssh"));
fs::write(ssh.join("ssh_host_ed25519_key"), "private\n").unwrap();
fs::write(ssh.join("ssh_host_ed25519_key.pub"), "public\n").unwrap();
fs::write(ssh.join("sshd_config"), "Port 22\n").unwrap();
Setup { host, backups, web, ssh }
}
fn run(&self, script: &str, args: &[&str]) -> Output {
@@ -41,6 +47,7 @@ impl Setup {
.env("GITV_KEYS", &self.host.keys)
.env("GITV_WEB_AUTH", self.web.join("login"))
.env("GITV_BACKUP_DIR", &self.backups)
.env("GITV_BACKUP_SSH_DIR", &self.ssh)
.env("GITV_BACKUP_REQUIRE_MOUNT", "0")
.env("GITV_BACKUP_LOCK_WAIT", "1")
.output()
@@ -97,6 +104,9 @@ fn snapshots_hold_everything_and_restore_whole() {
assert_eq!(fs::read_dir(first.join("repos/ada/.trash")).unwrap().count(), 1, "the trash is kept");
assert!(fs::read_to_string(first.join("etc-gitv/authorized_keys")).unwrap().contains("ssh-ed25519"));
assert_eq!(fs::read_to_string(first.join("gitv-web/login")).unwrap(), "user=Um9i\n");
assert_eq!(fs::read_to_string(first.join("ssh-host-keys/ssh_host_ed25519_key")).unwrap(), "private\n");
assert!(first.join("ssh-host-keys/ssh_host_ed25519_key.pub").is_file());
assert!(!first.join("ssh-host-keys/sshd_config").exists(), "only the host keys are kept");
assert!(fs::read_to_string(first.join("MANIFEST")).unwrap().contains("repositories: 2"));
// The next snapshot shares unchanged files with this one.
@@ -1,6 +1,7 @@
#!/bin/bash
# Snapshot everything GITv needs to come back after losing the server: the repositories (with
# their trash, reflogs, push logs and visibility markers), the SSH keys file and the web sign-in.
# their trash, reflogs, push logs and visibility markers), the SSH keys file, the web sign-in,
# and the server's SSH host keys (so a rebuilt server is the one clients already trust).
#
# Each run writes <backup dir>/gitv/snapshots/<UTC time>/, a plain copy that restores with
# rsync or cp. Files unchanged since the previous snapshot are hard links to it, so a snapshot
@@ -12,6 +13,7 @@
# GITV_BACKUP_KEEP_DAYS delete snapshots older than this (30)
# GITV_BACKUP_REQUIRE_MOUNT refuse unless the dir is a mount (1)
# GITV_BACKUP_LOCK_WAIT seconds to wait for a push (900)
# GITV_BACKUP_SSH_DIR where the SSH host keys are (/etc/ssh)
# GITV_REPOS, GITV_KEYS, GITV_WEB_AUTH as for gitv and gitv-web.
set -euo pipefail
umask 077
@@ -22,6 +24,7 @@ lock_wait=${GITV_BACKUP_LOCK_WAIT:-900}
repos=${GITV_REPOS:-/srv/gitv/repos}
keys_dir=$(dirname "${GITV_KEYS:-/etc/gitv/authorized_keys}")
web_dir=$(dirname "${GITV_WEB_AUTH:-/var/lib/gitv-web/login}")
ssh_dir=${GITV_BACKUP_SSH_DIR:-/etc/ssh}
say() { echo "gitv-backup: $*" >&2; }
die() { say "$*"; exit 1; }
@@ -124,6 +127,15 @@ for pair in "$keys_dir:etc-gitv" "$web_dir:gitv-web"; do
fi
done
# The host keys only (ssh_host_*), not the rest of sshd's configuration. A rebuilt server
# without them greets every client with "REMOTE HOST IDENTIFICATION HAS CHANGED".
if compgen -G "$ssh_dir/ssh_host_*" >/dev/null; then
copy "$ssh_dir/" "$snap/ssh-host-keys/" ssh-host-keys --include='ssh_host_*' --exclude='*' ||
die "copying the SSH host keys from $ssh_dir failed"
else
say "warning: no SSH host keys in $ssh_dir; not backed up"
fi
# Prove the copies are whole: every ref and reflog entry must reach complete history.
failed=0
while IFS= read -r -d '' copy_dir; do