Skip to content
11 min read

Backups and restore drills

A backup is a claim that you can get your data back. The claim is only as good as the last time you tried. This runbook covers the pattern we run for managed forges: a nightly Forgejo dump, shipped encrypted to storage off the box, then rehearsed with restore drills that get stricter the more they prove.

It is written for anyone running Forgejo, not only for Fjord customers. For what Fjord provides on each tier, see Backups and restore.

The shape of the pipeline

Each night, on the forge’s own machine:

  1. Dump. forgejo dump writes one archive holding the repositories, the database, the attachments and the configuration.
  2. Gate. The archive is checked before anything leaves the machine.
  3. Ship. A client-side-encrypted backup tool (we use restic) uploads the archive to a repository on separate storage. The storage provider only ever sees ciphertext.
  4. Prune. Snapshots older than the retention window are removed.
  5. Report. The machine tells the control plane that tonight’s backup landed.

Each step has a failure mode that looks like success. The rest of this page is about those.

Gate the dump before you ship it

forgejo dump can exit non-zero, exit zero with a truncated file, or leave a file from an earlier night in place. A non-empty file proves none of those did not happen. Before uploading, require all of these:

  • The command succeeded. Check the exit code of this run’s dump, not the existence of a file.
  • The file is this run’s. Give every dump a unique name that includes the run’s timestamp and process id, and look for exactly that name. An older dump left in the same directory must not satisfy the check.
  • The archive is intact. Run an integrity test on the ZIP (unzip -t) and fail on any error.

If any check fails, exit non-zero and upload nothing. A night with no snapshot is a loud, fixable problem. A night with a corrupt snapshot is a quiet one that you discover during the outage.

Upload the one validated file, not the directory that contains it. A leftover neighbour that has not been cleaned up yet would otherwise ride along inside a snapshot that claims to be tonight’s.

Note

The dump runs as the same user that owns the forge’s data. On a fresh machine the data volume is often owned by root, and the repository directory is created lazily with the first repository. Create the backup directory and the repository root with the right owner before the dump runs, idempotently, or the first backup fails with a permissions error that looks like a Forgejo bug.

Retention that actually prunes

Retention in restic is applied with forget. A rolling window looks like this:

restic forget --keep-within 90d --prune

There is a trap in it. restic groups snapshots before applying a policy, and by default it groups by host and path. The window is measured from the newest snapshot in each group. If the path of every backup includes something unique per run, which a unique dump name does, then every snapshot is alone in its own group. Each one is its own newest snapshot, so nothing is ever old enough to remove, and the repository grows without bound while the command reports success.

Group by host alone:

restic forget --group-by host --keep-within 90d --prune

Two further points about the window:

  • It is rolling, not a countdown after cancellation. --keep-within 90d keeps roughly the last 90 days of snapshots while backups keep running. What happens to the repository when a customer leaves is a separate deletion step with its own policy.
  • Check that it prunes. Count snapshots after a few weeks. A repository whose snapshot count only rises is not applying its policy.

Stale locks

If a backup is killed mid-run, restic leaves a lock in the repository, and the next run stops with unable to create lock. Run restic unlock before each backup. It only removes locks that are provably stale, so it is safe as a routine first step, and it turns one crashed night into one night instead of every night after it.

Know when a backup is missing

A pipeline that only reports failures cannot tell you it did not run. Cron that never fired, a machine that is off, and a script that was replaced by an empty file all produce no error.

  • Report success, with evidence. After a backup is uploaded and its snapshot id is confirmed, have the machine send the completion time and the snapshot id to the control plane.
  • Alert on absence. Alert when the last success is older than the schedule plus a margin, not only when an error is logged.
  • Never let reporting fail the backup. The report is best effort. A network problem while reporting must not turn a good backup into a failed one.
  • Do not treat the report as proof. It says a backup ran. Only a restore says the backup works.

Dump a quiet forge

forgejo dump reads the database and the repositories one after the other. On a forge that is taking pushes, creating repositories or running background jobs while it runs, those two reads can describe different moments: a database row for a push whose objects are not in the archive, or objects with no row. Every gate above can pass and the archive can still restore to a forge with a missing change or a broken repository.

Decide how you handle this, and write the decision down:

  • Quiesce writers for the dump. Stop Forgejo, or put it into a read-only maintenance state that also holds SSH pushes and background jobs, for the length of the dump. This is the only option that gives a consistent archive, and it costs a short window each night.
  • Coordinate a snapshot underneath. A filesystem or volume snapshot taken while writes are paused, with the database dumped from the same moment.
  • Accept the gap and say so. If you dump a live forge, treat the archive as crash-consistent at best, run the level 2 check below on every drill, and record what you accepted.

Whatever you choose, take the reference manifest for drills (repository list, refs, row counts, attachment and LFS hashes) at the same boundary as the backup.

Guard the repository password

The encryption key is the backup. If the repository password is lost, every snapshot becomes unreadable noise, however many copies of the ciphertext exist.

  • Keep the means to recover the password outside the machine being backed up. A backup whose key lives only on the box it protects does not survive losing the box.
  • Do not rotate the material the password is derived from without a plan. A rotation that strands existing repositories looks like a successful change until the first restore.
  • Rehearse restoring with the password as it will be available during an incident, not as it is available to you at your desk.

Three levels of proof

Not every drill needs to be a full rebuild, but every drill should say which level it reached. Treat them as a ladder.

LevelWhat you doWhat it provesWhat it cannot prove
1. ArchiveList snapshots, restore the latest into a scratch directory, test the ZIP and confirm the dump contains the database file and the repositories directory.The snapshot exists, decrypts, and the archive is readable.That Forgejo can use it. A corrupted dump can still unzip.
2. SemanticLoad the database dump into a scratch database. Compare table, user and repository counts, and the Forgejo schema version, against the manifest. Match each repository record to its restored repository, compare branch and tag names and the commits they point to, then run git fsck. Hash every attachment and LFS object the database references and compare.The database and the repositories agree with each other and with what existed when the backup was taken.That a running Forgejo serves them. Anything you only sampled.
3. ServiceStart a throwaway Forgejo on the restored data and call a basic API endpoint. Sign in, open a sample repository, and download a sample attachment and a sample LFS object.The thing you would actually cut over to works end to end. Gives you a measured recovery time.Anything beyond the samples you picked. Nothing about tonight’s backup, only the one you restored.

Level 1 is the minimum for a new backup path and after any change to the backup script or its credentials. Levels 2 and 3 are the recurring bar. A sensible cadence is level 1 at setup and after every change, and levels 2 and 3 on a fixed schedule such as quarterly.

Note

Do not assume a one-line restore command exists for your Forgejo version. Restoring a dump means unpacking the archive, loading the SQL into a database of the same engine and a compatible schema version, and putting the repositories and data directories where Forgejo expects them. Pin the Forgejo and database versions to the source, and confirm the archive layout against the dump you actually have before you rely on a procedure.

Run drills so they cannot hurt anything

A drill restores production data to somewhere that is not production. That is exactly the situation in which a restored configuration can send a real email, fire a real webhook or sync a real mirror.

  • Use a throwaway target allocated for the drill, with fresh volumes. Never point a drill at an arbitrary host or at a path that already holds data.
  • Isolate the network. Bind services to loopback only and block outbound traffic from the target.
  • Restore while the service is stopped, then neutralise the integrations before first start: mail, webhooks, Actions, mirrors, and any single sign-on provider the configuration points at.
  • Always clean up, including when the drill fails or is interrupted: remove containers, volumes, temporary files and credentials. Use a trap or a finally for ordinary failures, and an expiry on the target for the case where the process itself is killed.
  • Never prune as part of cleanup. Cleaning up the drill must not delete the source backup or change production retention.
  • Handle the archive as the sensitive data it is. A dump contains everything the forge holds. Scratch copies get the same access limits as the forge and are deleted when the drill ends.

Measure honestly

A drill is only useful if its numbers mean something.

  • Recovery time runs from the moment you start fetching the backup to the moment verification passes. Include retrieving the snapshot, creating the target, importing, starting and verifying. Time to a failure is not a recovery time and must not be reported as one.
  • Recovery point is the time of the data the backup represents, not the time the upload finished. A nightly job has a recovery point gap of up to a day plus the time the job takes. Report what the drill observed, and do not treat one drill as a guarantee about every night.
  • Compare against a manifest taken at the same moment as the backup. Comparing the restore to live counts taken later reports differences that are only a day of normal work.
  • Say what was empty or sampled. If the source forge has no attachments or LFS objects, a restore that finds none has proved nothing about them. If you checked a sample instead of everything, say how large the sample was.
  • Record failures. A failed drill is the most valuable result. Keep it, with what failed and what you changed, rather than re-running until it passes.

Fjord publishes sanitized drill results, including failures, on the

backup drills page.

Failure modes

What you seeUsual causeWhat to do
unable to create lockAn earlier backup crashed and left a lock.Run restic unlock and retry. Make unlock a routine first step.
unable to open repositoryThe repository address or password is wrong, or the storage account changed.Check both values against the source of truth, not against another copy on the same machine.
Permission denied over SFTPThe storage credential no longer matches the account.Find what changed the credential. Do not rotate the secrets it derives from as a first fix.
A snapshot restores but the ZIP is empty or will not openThe dump failed and the gate did not stop the upload.Fix the gate first. Then re-run the backup and read the dump command’s exit code.
The repository grows and no snapshot is ever removedRetention groups by path and every run has a unique path.Group by host. See Retention that actually prunes.
The first backup on a new machine failsThe data or backup directory has the wrong owner, or the repository root does not exist yet.Create both with the right owner before the dump, every run.
No error, and no new snapshot for daysThe schedule did not run.Alert on the age of the last success, not only on errors.

Verify it is healthy

  • The latest snapshot is newer than your schedule plus its margin.
  • The snapshot count levels off at the size your retention window implies.
  • The last drill reached the level you promised and was dated within your cadence.
  • You can name where the repository password is recoverable from if the machine is gone.
  • Nothing in the last drill touched production.
Contributors
  • Stephen Way