Site administration
Back up the analytics database
If the analytics server loses its data, the events are gone unless you have a backup. Set up a nightly backup of the database to Neon object storage, once on each new server.
4 min read
You need:
- To be signed in to the server as root. See Sign in to the server.
neonctl, signed in to the TanStack organization on Neon, on your computer.- The Source repository with wrangler signed in, on your computer, for step 3.
The backups go to the Neon project source-backups (wandering-moon-95155232, AWS us-east-1), which has one private bucket, backups, on its branch main. It is a project of its own, so its keys reach no other data of the forge. It has been in place since 2026-11-30. To make it again:
neonctl projects create --org-id org-green-water-28502066 --name source-backups --region-id aws-us-east-1 --no-secrets
neonctl buckets create backups --project-id <project id> --branch main
A new project has a new S3 endpoint: https://<branch id>.storage.<cell>.us-east-1.aws.neon.tech. <branch id> is the id of its branch main (neonctl branches list --project-id <project id>), and <cell> is the first part of its proxy_host (neonctl projects get <project id> --output json), for example c-16. Put the endpoint in NEON_BACKUP_ENDPOINT in apps/web/wrangler.keys.jsonc, and in the URLs of steps 1 and 2.
1. Give the server its key
The server signs in to Neon with a key of its own. On your computer, this command makes the key and writes it into a config file on the server. The key goes from neonctl to the server, so it is not shown, and no file on your computer keeps it. Replace <server IPv4>:
neonctl credentials create --project-id wandering-moon-95155232 --branch main --name clickhouse-server --scope storage:read --scope storage:write --output json \
| python3 -c '
import json, sys
key = json.load(sys.stdin)
print(f"""<clickhouse>
<s3>
<neon_backups>
<endpoint>https://br-steep-term-bcsp8mb3.storage.c-16.us-east-1.aws.neon.tech/backups/</endpoint>
<region>us-east-1</region>
<access_key_id>{key["token_id"]}</access_key_id>
<secret_access_key>{key["s3_secret_access_key"]}</secret_access_key>
</neon_backups>
</s3>
</clickhouse>""")' \
| ssh root@<server IPv4> 'umask 077 && cat > /etc/clickhouse-server/config.d/neon-backups.xml && chown root:clickhouse /etc/clickhouse-server/config.d/neon-backups.xml && chmod 640 /etc/clickhouse-server/config.d/neon-backups.xml && systemctl restart clickhouse-server'
2. Write the script and its cron file
On the server, run:
cat > /usr/local/bin/clickhouse-backup-neon <<'EOF'
#!/bin/sh
set -eu
day=$(date -u +%F)
clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "BACKUP DATABASE analytics TO S3('https://br-steep-term-bcsp8mb3.storage.c-16.us-east-1.aws.neon.tech/backups/$day/')"
chmod 700 /usr/local/bin/clickhouse-backup-neon
echo '17 3 * * * root /usr/local/bin/clickhouse-backup-neon >> /var/log/clickhouse-backup.log 2>&1' > /etc/cron.d/clickhouse-backup
3. Give the keys Worker its key
Neon object storage runs no lifecycle rules, so the forge deletes the old backups itself. The keys Worker signs those requests with a key of the project, in the secret NEON_BACKUP_KEYS. Run this from apps/web. It makes the key and puts it straight into the secret, so nothing is printed and no file keeps it:
neonctl credentials create --project-id wandering-moon-95155232 --branch main --name keys-worker --scope storage:read --scope storage:write --output json \
| python3 -c 'import json, sys; key = json.load(sys.stdin); print(json.dumps([{"id": key["token_id"], "secret": key["s3_secret_access_key"]}]))' \
| pnpm exec wrangler versions secret put NEON_BACKUP_KEYS -c wrangler.keys.jsonc --tag backup-keys --message "Neon backup storage key"
pnpm exec wrangler versions deploy <the version id it printed>@100% -c wrangler.keys.jsonc
The new version has a tag, so the next ci/deploy goes on as usual (README.md, "Deploying"). The endpoint and the region are vars of the keys Worker (NEON_BACKUP_ENDPOINT and NEON_BACKUP_REGION in wrangler.keys.jsonc). Only the web Worker can use this key, and only for the bucket backups.
4. Revoke the default key of the project
Neon makes a "Default object storage credential" with each new project. It can read and write the bucket. Find its id, then revoke it:
neonctl credentials list --project-id wandering-moon-95155232 --branch main
neonctl credentials revoke <id of the default object storage credential> --project-id wandering-moon-95155232 --branch main
5. Run the first backup
On the server, run the script once:
/usr/local/bin/clickhouse-backup-neon
6. See the last backup
On the server, run:
clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "SELECT name, status, error, end_time FROM system.backups ORDER BY start_time DESC LIMIT 3"
A good backup has the status BACKUP_CREATED. The table shows only the backups since ClickHouse last started. For older backups, read /var/log/clickhouse-backup.log.
Done
The newest row has the status BACKUP_CREATED, so your events have a copy that you can restore. Restore an analytics backup shows how.
The nightly backup
Every night at 03:17 UTC, the server saves the database analytics to the Neon bucket backups, under <day>/. <day> is the UTC day, for example 2026-11-04.
/etc/cron.d/clickhouse-backupruns the script/usr/local/bin/clickhouse-backup-neonas root. The script writes to the log/var/log/clickhouse-backup.log.- The script runs one query:
BACKUP DATABASE analytics TO S3('https://br-steep-term-bcsp8mb3.storage.c-16.us-east-1.aws.neon.tech/backups/<day>/'). - The key of the server is in
/etc/clickhouse-server/config.d/neon-backups.xml. ClickHouse uses it for each URL under the endpoint of that file, so no key is in a query. - Every day at 03:07 UTC, the daily work of the web cron deletes each folder whose day is more than 8 days before that day (
analytics backupsinapps/web/src/server/cron.ts). So a backup stays for 8 full days. While the keys Worker has no key inNEON_BACKUP_KEYS, nothing is deleted.
Move the backups of a server from R2
A server that was set up before 2026-11-30 writes its backups to the R2 bucket gitflare-analytics, under backups/. To move it to Neon:
Do steps 1 to 6 above. Step 2 changes the cron file, so the next backups go to Neon.
Keep
/etc/clickhouse-server/config.d/r2-backups.xmlfor 8 days. A restore of a backup from before the move reads R2 with it.After 8 days, the lifecycle rule of R2 has deleted the last backup there. Then delete the old files on the server:
rm /usr/local/bin/clickhouse-backup-r2 /etc/clickhouse-server/config.d/r2-backups.xml systemctl restart clickhouse-serverIn the Cloudflare dashboard, delete the R2 token of the server.