Site administration
Back up the git data
Each git server holds a whole copy of every repository, and the three copies keep each other current. A bug, a wrong command or a deleted volume can still reach all three. So each server also keeps a backup of every repository in Neon object storage in its own region, off the server. This page sets the backups up, checks them, and restores a repository from them.
6 min read
Each server backs up its own copy to the bucket of its region:
| Server | Neon project | AWS region |
|---|---|---|
us (Ashburn) | source-git-us (wandering-mouse-57642733) | us-east-1 |
eu (Nuremberg) | source-git-eu (bitter-boat-65941388) | eu-central-1 |
asia (Singapore) | source-git-asia (snowy-water-65547520) | ap-southeast-1 |
Replication already brings every repository to every server, so no backup crosses an ocean, and each region has a whole backup of its own. The projects have been in place since 2026-12-03, each with a private bucket git on its branch main. Each is a project of its own, so the key of a git server reaches only the git backups of its region, and no LFS object, asset or vault file.
You need:
neonctl, signed in to the TanStack organization on Neon.- SSH to the git servers, as in Set up the git servers.
1. Make the storage project
To make the project of a region again (here eu):
neonctl projects create --org-id org-green-water-28502066 --name source-git-eu --region-id aws-eu-central-1 --no-secrets
neonctl buckets create git --project-id <project id> --branch main
Neon makes a "Default object storage credential" with every new project. Revoke it: neonctl credentials list --project-id <project id> --branch main, then neonctl credentials revoke <id> --project-id <project id> --branch main. Don't use neonctl env pull on this project: it makes that key again.
When the endpoint of the branch changes, change it in backup_endpoint of apps/gitd/deploy/install.sh. The endpoint is https://<branch id>.storage.<proxy host>, for example https://br-odd-tooth-bgiab96q.storage.c-9.eu-central-1.aws.neon.tech. neonctl projects get <project id> prints the proxy host.
2. Put the key on the server
Make a key and write it straight into /etc/gitd/backup-key.json on the server. Nothing is printed, and nothing is saved on your computer:
neonctl credentials create --project-id <project id> --branch main --name gitd-backup-gitd-eu --scope storage:read --scope storage:write --output json \
| python3 -c 'import json,sys; d=json.load(sys.stdin); print(json.dumps({"id": d["token_id"], "secret": d["s3_secret_access_key"]}))' \
| ssh gitd-eu 'umask 027; cat > /etc/gitd/backup-key.json.new && chown root:git /etc/gitd/backup-key.json.new && mv /etc/gitd/backup-key.json.new /etc/gitd/backup-key.json'
To rotate the key, make a new one the same way, restart gitd (systemctl restart gitd), then revoke the old key.
3. Turn the backups on
Install gitd as in step 5 of the setup. When the server has /etc/gitd/backup-key.json, install.sh adds these lines to /etc/gitd/env:
GITD_BACKUP_ENDPOINT=https://br-odd-tooth-bgiab96q.storage.c-9.eu-central-1.aws.neon.tech
GITD_BACKUP_S3_REGION=eu-central-1
GITD_BACKUP_KEY_FILE=/etc/gitd/backup-key.json
GITD_BACKUP=off installs with no backups. To turn them on with only a swap of the binary, add the three lines to /etc/gitd/env yourself, then systemctl restart gitd.
When gitd starts, it backs up every repository that has no backup yet, two at a time.
4. Check the backups
Look at the log of gitd. Each backup writes one line:
ssh gitd-eu 'journalctl -u gitd --since "-1h" | grep "backup of"'A line such as
backup of <repo id> at version <n>: <bytes> bytes sent in <time> (whole pack true, 1 packs, ...)is a backup that ended well. A failed backup saysfailedand when it tries again.Read the manifest of a repository:
neonctl buckets object get git/repos/<repo id>/manifest.json --file /dev/stdout --project-id <project id> --branch mainIts
versionis the refs version of the repository on the server when the backup ran (cat /srv/git/repos/<repo id>.git/gitd-version).
5. Restore a repository
gitd restore makes a bare repository in a new folder from the backup of the server's region, then checks it with git fsck --connectivity-only. Restore into a scratch folder first:
ssh gitd-eu 'set -a; . /etc/gitd/env; set +a; mkdir -p /srv/git/restore-check && chown git:git /srv/git/restore-check; runuser -u git -- env GITD_BACKUP_ENDPOINT="$GITD_BACKUP_ENDPOINT" GITD_BACKUP_S3_REGION="$GITD_BACKUP_S3_REGION" GITD_BACKUP_KEY_FILE="$GITD_BACKUP_KEY_FILE" /usr/local/bin/gitd restore <repo id> /srv/git/restore-check/<repo id>.git'
It prints the version, the packs and the time it took. Then compare it with the copy that the server serves, and remove the scratch folder:
ssh gitd-eu 'cd /srv/git; git -C repos/<repo id>.git for-each-ref > /tmp/a; git -C restore-check/<repo id>.git for-each-ref > /tmp/b; cmp /tmp/a /tmp/b && echo same refs; git -C restore-check/<repo id>.git fsck --strict --no-dangling; rm -rf restore-check /tmp/a /tmp/b'
To put a lost repository back, stop gitd, restore into /srv/git/repos/<repo id>.git, and start gitd: it writes the hooks of each repository when it starts. On a replica, the next push brings the repository up to date. On the primary, the restored refs are the ones of the last backup, so pushes after it are lost unless a replica has them: copy the repository from the replica with the newest version in its place (Add a replica server).
6. Seed a new server from the backups
A new server of a region can take its repositories from the bucket of its region, with no copy across an ocean. Give it the key of its region (step 2), list the repositories in the bucket, and restore each one before you start gitd:
neonctl buckets object list git/repos/ --project-id <project id> --branch main
Then go on with Add a replica server: its copy step only sends what changed after the backups.
Done
Each git server keeps a backup of every repository that it holds in the bucket of its region, a few seconds after each push or write, and you can rebuild a repository from it.
How it works
apps/gitd/backup.go keeps the backup of each repository as:
Key in the bucket git | What it holds |
|---|---|
repos/<id>/manifest.json | The refs version, HEAD, the refs file and the list of packs |
repos/<id>/refs/<version>-<sum> | Every ref of that version, one <id> <name> line each |
repos/<id>/packs/pack-<hash>.pack | Packs of git objects, each one whole: no object of a pack is a delta of an object in another pack |
- What a backup sends. A backup reads the refs version, the refs and HEAD, and the version again: a push in between makes it read them again. It packs the objects that the refs reach and that the refs of the last backup did not reach (
git pack-objects --revs), in packs of at most 1 GiB. It uploads the packs, then the refs, then the manifest. So a manifest names only files that are in the bucket, and its packs hold every object that its refs reach. - When. Every new refs version of a repository (a push, a write, a copy from the primary) marks it, and a background run backs it up 5 seconds later, two repositories at a time. A failed backup tries again after 30 seconds, then waits twice as long each time, up to 30 minutes. When gitd starts, and every 15 minutes, a pass marks each repository whose last backup is older than its version.
- No growth without end. The next backup packs every object that the refs reach into new packs, and deletes every file of the repository that the new manifest does not name, once the packs added since the last whole pack are 512, or hold at least 64 MiB and half the bytes of the whole pack. So a large repository with many small pushes is not sent whole again every few hours: t3code (3.9 GB, about 36 pushes an hour) adds a pack of a few hundred KB and its refs file of about 2.5 MB for each push.
- What the server keeps. The last manifest and its refs, in the repository folder (
gitd-backup.json,gitd-backup-refs). A server without them, as a new server, reads them from the bucket. - A deleted repository. The purge of a deleted repository asks each server to delete its copy (
DELETE /api/{repo},apps/gitd/delete.go). The server deletes the folder, then every key underrepos/<id>/. When that fails, the purge asks again. - When object storage fails. Pushes, fetches and pages go on: a backup never holds a push. The failed backups try again, and the pass every 15 minutes finds what is left.
- What it costs. Neon object storage costs $0.023 a GB-month, with no charge for each request. An upload is not egress.