Articles ·
Back up a hosted database with one line of cron
Your backup is a file you can open. A cron line that downloads it every night, checks it, and keeps a month of copies.
A hosted database you cannot get out of is a risk. With Served the way out is one request that returns an ordinary SQLite file, which means a backup is a file on a machine you control, and making one every night is a line of cron.
The line
0 3 * * * curl -fsS -H "Authorization: Bearer $SERVED_KEY" -o "$HOME/backups/todo-$(date +\%F).db" https://api.served.dev/v1/databases/todo/download
At three each morning this saves the database todo as todo-2026-11-27.db. Three details matter:
-fmakescurlfail on an error status, so an expired key does not quietly save an error message as your backup.- The
%in the date format is written\%, because cron treats a bare%as a line break. - Cron does not read your shell profile. Set
SERVED_KEY=served_...on a line of its own at the top of the crontab.
The file you get is a consistent copy from a single moment, even if something was writing at the time, and it is compacted on the way out.
A backup you have not opened is a hope
Because the backup is a real database, checking it takes one command:
sqlite3 ~/backups/todo-2026-11-27.db "PRAGMA integrity_check"
It prints ok or tells you what is wrong. Better still, ask it something only a good copy could answer, such as the number of rows in your largest table.
As a script
This version backs up every database on the account, checks each copy, and keeps thirty days. It needs curl, jq and sqlite3.
#!/bin/sh
set -eu
dir="$HOME/backups/$(date +%F)"
mkdir -p "$dir"
auth="Authorization: Bearer $SERVED_KEY"
curl -fsS -H "$auth" https://api.served.dev/v1/databases | jq -r '.databases[].name' |
while read -r name; do
curl -fsS -H "$auth" -o "$dir/$name.db" "https://api.served.dev/v1/databases/$name/download"
result=$(sqlite3 "$dir/$name.db" "PRAGMA integrity_check")
[ "$result" = "ok" ] || { echo "backup of $name is damaged: $result" >&2; exit 1; }
done
# keep thirty days
find "$HOME/backups" -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec rm -r {} +
Run it from cron and have cron mail you its output, so that a failure is something you hear about.
Putting one back
Restoring is the same request in the other direction:
curl -fsS -X PUT --data-binary @"$HOME/backups/2026-11-27/todo.db" \ -H "Authorization: Bearer $SERVED_KEY" \ https://api.served.dev/v1/databases/todo/upload
This replaces the whole database with the file. To look before you leap, upload it under another name first, such as todo-restored, and query that.
What we keep, and why you still want your own
Served does not yet keep a second copy of your database anywhere. Until it does, the copies you make are the only ones.
Dated copies that you hold are what protect you from your own mistakes, and from ours. A restore-point feature is planned as a paid add-on for people who would sooner not run the cron job. Until then, and probably after, the cron job is ten minutes well spent.
Served is hosted SQLite. Real SQLite behind an HTTP API, $3 a month, and the file is yours to download whenever you like.