Backing up a Cloudflare stack: the backup that lives outside the account
What we back up from Cloudflare (D1 databases, DNS, inventory), why the main copy lives outside the account, how it is encrypted, why git branches need bundles rather than a tarball, and the check that proves a backup can actually be restored.
- The risk is not only "someone deletes data". It is also "we lose access to the account". A backup stored only in the same account does not help then.
- We export D1 databases, DNS for every zone and an inventory of workers, pages and buckets every night, encrypt with age, and store the main copy outside Cloudflare.
- A second, readable copy goes to a separate drive, so losing the encryption key is not fatal either.
- Git is backed up as bundles of every branch, because unfinished work lives in branches. And a daily check proves the newest backup can actually be decrypted.
Our whole web and agent stack runs on Cloudflare (why). One summer a billing hold on the account stopped production deploys for about three weeks; it was later lifted as a false alarm. Nothing was lost, but it changed how we think about backups. The question is not only "what if a table is deleted?" but "what if we cannot log in tomorrow?".
What we back up, and how
| What | How | Note |
|---|---|---|
| D1 databases | Export to SQL dump via the API | Contain personal data, always encrypted |
| DNS | Export of every zone | The most valuable item |
| Zones and inventory | Lists of workers, pages, KV namespaces, D1 databases and buckets | The map of names and bindings; code itself is in git |
| Git repositories | git bundle --all from a local mirror | All branches, tags and history |
What we deliberately do not back up here: the code of workers and pages (git is the source of truth) and secret values (they belong to the encrypted secrets vault, see handling secrets).
Where it goes
- Main copy, encrypted, outside Cloudflare. A nightly job (02:40 UTC) exports, encrypts with age and uploads the files as a release in a private backup repository. Releases do not count towards the repository size limit, a lesson learned when the repository grew past 5 GB and pushes started failing.
- Second copy, readable, on a separate drive. It protects against losing the hosting provider and against losing the encryption key. Two copies with different weaknesses beat one perfect copy.
- A third copy inside Cloudflare, in a private storage bucket, only as a convenience. Never in a publicly served bucket.
In depth: lessons that shaped the scripts
A backup must refuse to succeed when it made nothing
Our first version reported success for three days while the backup repository stayed empty. Each partial failure was "skipped", and the script exited with success. Now the backup script verifies the token at the start and, at the end, checks that at least one database dump exists, the zone list is not empty and no file is suspiciously small. Otherwise it fails loudly.
A tarball of main is not a git backup
An early idea was to download the main branch through the API. That looks like a backup but saves only the current tree of one branch, while unfinished work lives in feature branches. Bundles made with git bundle create --all keep everything, and each one is verified with git bundle verify before it is kept.
Big files get split, and checked
Release assets have a size cap, so encrypted dumps above about 1.9 GB are split into parts. The script checks that the joined parts have the same hash as the original before it reports success.
The check that matters
One command answers "do we have a backup?" by checking four things: the secrets for the job exist, the last run finished successfully, the newest encrypted file is less than 30 hours old, and it can be decrypted with the key. The fourth one is the point: finding out on restore day that a backup cannot be opened is too late.
Restoring
Download the newest release, decrypt with the key from the keychain, load the SQL into a fresh database; import DNS through the dashboard or the API. Writing these three steps down, and testing them, is part of the backup, not an afterthought.
For more on where data should live in the first place, see text file, Git or database. If backups are part of a larger automation plan, five jobs to hand to AI agents first includes watching them.
Frequently asked questions
Does Cloudflare back up my D1 database?
D1 has point-in-time recovery inside Cloudflare, which protects against mistakes. It does not protect against losing access to the account. For that you need an export stored somewhere else.
What is the most valuable thing to back up?
DNS. If zones disappear, every site and mailbox on the domain stops working at once. Data can be missed for a while; a dead domain cannot.
Why encrypt backups?
Database dumps contain customer e-mails and texts. An unencrypted dump in a repository stays there forever, even after deletion. We encrypt with age before anything leaves the machine that made it.
How do you know a backup works?
A check that does not ask whether backup is configured, but whether a recent file exists and can be decrypted. A backup that cannot be opened is not a backup.
Written from our own production systems by the Flowsmith editorial team, with AI assistance.
Want this running in your company?
Flowsmith designs and operates automations like this for small and mid-sized teams — with a person approving anything that leaves the building.