Repository navigation
docs: back up and restore SQLite and PostgreSQL with their own tools - #737
LeeroyHannigan wants to merge 4 commits into
Conversation
The admin guide and the security model showed two pg_dump commands with a data database name that init does not create, and no page said how to copy a SQLite deployment or whether two pg_dump runs agree. Add a Database Backup and Restore section to the admin guide. Each procedure that needs the server stopped runs as one block under set -e and ends before it touches a file or a database unless extenddb status reports the server stopped, because extenddb stop stops nothing for a server started with serve --foreground. For SQLite the section gives VACUUM INTO while the server runs, a copy of the database file and whichever of its -wal and -shm files exist with the server stopped, and a restore that checks the copy with PRAGMA integrity_check before it moves the old files aside. For PostgreSQL it states that two pg_dump runs disagree when a table is created or deleted between them, and gives a dump pair taken with the servers stopped, pg_basebackup with its prerequisites, and the restore of each. The dump pair restore renames the databases it replaces and removes them only after the restored server passes a check. It prints the catalog's data_database_name and data_database_connection_string rows and sets both to their full new values when they name another database or address, because the server opens the database the connection string names. Every restore ends with a read of every table. The section also says what a copy holds: the catalog stores the encryption key next to the secrets it encrypts. The upgrade manual, security model and deployment guide now point at the section. The upgrade manual sends its plain-text dumps to the Rollback Procedure, which restores them with psql -f.
docs/manuals/05-admin-guide.md keeps main's Data Recovery entry in place of this branch's Database Backup and Restore section and its pointer to that section. Main's entry names each backend's databases and gives the PostgreSQL dump and restore commands. docs/manuals/10-security-model.md keeps main's Backup and Recovery text in place of this branch's pointer to the removed section. Main's text dumps the catalog and the data database under umask 077 and points at the admin guide's Data Recovery entry. docs/manuals/11-deployment-guide.md keeps main's checklist items on database backups and the backup store in place of this branch's item, which also pointed at the removed section. docs/manuals/07-upgrade-manual.md merged without a conflict. The merge drops this branch's sentence under step 2, whose link named the removed section, and keeps main's text. The procedures that main does not state follow in a separate commit.
…kup requirements Main's Data Recovery entry (#855) names each backend's databases and gives the PostgreSQL dump and restore commands. This commit adds to that entry the procedures it did not state. The branch's section measured each of them on sqlite3 3.46.0 and PostgreSQL 16. A paragraph after the table says how to check that the server is down before a copy or a restore. `extenddb stop` exits 1 and stops nothing when it finds no PID file. The paragraph gives `extenddb status --config /path/to/extenddb.toml | grep -q 'is not running'`. For PostgreSQL it adds the `pg_stat_activity` query that counts the application role's sessions, which covers servers on other hosts. A SQLite paragraph gives `VACUUM INTO` as the copy taken while the server runs, with `.timeout 5000`, `umask 077` and one directory per copy. It says that `.backup` did not finish under writes. A second paragraph checks the copied files with `PRAGMA integrity_check` in a work directory before they go in place, as the table's SQLite row describes. The last paragraph gives the `pg_basebackup` command and the requirements the PostgreSQL documentation sets for it: the `REPLICATION` attribute, `wal_level`, a `replication` line in `pg_hba.conf` and `wal_keep_size` for `-X fetch`. The commands use the entry's default names in place of the branch's placeholders. The branch's read check of a restored server and its `pg_restore` note are not carried over. The entry already says to read every table before you drop the renamed databases, and it restores the plain-text dumps with `psql -f`.
docs/manuals/05-admin-guide.md keeps the stop check, the VACUUM INTO copy and the integrity check from this branch. The pg_dump paragraph below them comes from main. It says what the catalog dump holds with and without a KEK file.
LJMart11
left a comment
There was a problem hiding this comment.
Reviewed at d5546bb. Ran the new procedures against a head SQLite build, a private PostgreSQL 16.16 cluster and SQLite WAL copies: VACUUM INTO, the file modes and pg_basebackup behaved as written. The comments are about cases where the stop check and the integrity check report success when they should not.
3 findings are left inline.
|
|
||
| SQLite's release history dates `VACUUM INTO` to version 3.27.0; the commands here were run with sqlite3 3.46.0, and the sqlite3 3.7.17 shell rejects it (`Error: near "INTO": syntax error`). Do not use the sqlite3 `.backup` command while the server takes writes. The shell copies 100 pages at a time through SQLite's online backup API, which the SQLite documentation says starts again from the first page when another connection writes to the database between two steps: with the client making about 100 writes a second to the 284 MB database, `.backup` had not finished after 300 s. | ||
|
|
||
| Before the copied files go in place (the SQLite row above), copy them into a work directory beside the database and check them there with `PRAGMA integrity_check`. The check writes a copied `-wal` file into the database file and removes it, so a copy taken with the server stopped goes into place as one file. A database file copied without its non-empty `-wal` file failed the check (`database disk image is malformed`); put the files in place only when the check prints `ok`: |
There was a problem hiding this comment.
This paragraph treats ok from PRAGMA integrity_check as proof that a copy kept its -wal, but the check tests page structure, not whether committed writes are present. In 8 trials, a WAL database (autocheckpoint at its default) was copied while its writer had it open, without the -wal file. 7 copies printed ok and were each missing 150 to 750 of 5050 to 9900 committed rows. Saying the check finds corruption but not a missing -wal, and that the three files must be copied together with the server stopped, would close this.
|
|
||
| ```bash | ||
| # Confirm the stop; exits 0 only when nothing listens on the configured port | ||
| extenddb status --config /path/to/extenddb.toml | grep -q 'is not running' |
There was a problem hiding this comment.
This check passes for a running server whenever the port it probes differs from the server's port, and on SQLite no second check catches that. A head SQLite server started with EXTENDDB__SERVER__PORT=18999 logged extenddb listening on 127.0.0.1:18999, and this exact command still exited 0 (extenddb is not running (port 18443 is not in use)). Telling readers to run the check with the server's environment, or with --port set to the server's port, would close this.
| Give the config file as an absolute path: a relative path names another file when the command runs from another directory. `extenddb status` connects to the port on 127.0.0.1, so for a server whose `bind_addr` is another address, also confirm the stop through whatever runs the server. On PostgreSQL, the query below counts the sessions of the application role on either database, which covers servers on other hosts and servers that `extenddb status` cannot see; go on only when it prints `0`: | ||
|
|
||
| ```bash | ||
| psql -At -c "SELECT count(*) FROM pg_stat_activity WHERE datname IN ('extenddb_catalog', 'extenddb') AND usename = 'extenddb'" postgres |
There was a problem hiding this comment.
This query names extenddb_catalog, extenddb and role extenddb, but the table at line 1283 says init --catalog-db and --data-db can choose other database names, and --extenddb-user can choose another role. On a PostgreSQL 16.16 cluster with 2 sessions open on a prod_catalog/prod pair as role myapp, the query counted only the 1 session on the default pair. Marking the three literals as the defaults, to be replaced with the names in the config's connection_string and the catalog's data_database_name row, would close this.
demohan
left a comment
There was a problem hiding this comment.
Followed as written on a default setup, every procedure on the page gives back a complete store: restored on servers built from this head, SQLite and PostgreSQL serve their tables, items, streams, GSIs, access key and IAM data. The branch merges onto main 222d80a. Three places can lose data or leave a reader without a way back.
I will approve once the stopped-server check holds for SQLite, the page no longer suggests integrity_check catches a missing -wal, and the base backup has its restore.
| | MongoDB | `extenddb_catalog` and `extenddb_data`, fixed names | | ||
| | Cassandra | The catalog keyspace `<keyspace_prefix>_catalog` and one keyspace per account, `<keyspace_prefix>_account_<account id>` (`keyspace_prefix` defaults to `extenddb`) | | ||
|
|
||
| Where a step in this entry needs the server stopped, confirm the stop before the step touches a file or a database. `extenddb stop` finds the server through the PID file the daemon writes. It stops nothing and exits 1 for a server started with `serve --foreground`, which writes no PID file, and when its `--config` path does not name the server's config file. Stop the server through whatever runs it: `sudo systemctl stop extenddb` for the Deployment Guide's systemd unit, the container runtime for a container, otherwise `extenddb stop --config /path/to/extenddb.toml`. Start it again the same way when the procedure is done. The command below exits 0 only when `extenddb status` reports that nothing listens on the configured port; go on only then: |
There was a problem hiding this comment.
This check reads the port from the config, so it can report a SQLite server stopped while one runs. Started with serve --port 24204 against a config of 24203, the check exits 0; the SQLite restore steps that follow then ran beside the live server, which acknowledged 798 PutItem with no error, and after a restart none of the 798 were there. On SQLite the server holds its lock file while it runs: flock -n <db>.lock true exits 1 with a server running and 0 with none, measured both ways. Separately, extenddb stop here works on a foreground server started with --write-pid-file, falls back to port 18443 without a config, and with another server's readable config stops that server.
|
|
||
| SQLite's release history dates `VACUUM INTO` to version 3.27.0; the commands here were run with sqlite3 3.46.0, and the sqlite3 3.7.17 shell rejects it (`Error: near "INTO": syntax error`). Do not use the sqlite3 `.backup` command while the server takes writes. The shell copies 100 pages at a time through SQLite's online backup API, which the SQLite documentation says starts again from the first page when another connection writes to the database between two steps: with the client making about 100 writes a second to the 284 MB database, `.backup` had not finished after 300 s. | ||
|
|
||
| Before the copied files go in place (the SQLite row above), copy them into a work directory beside the database and check them there with `PRAGMA integrity_check`. The check writes a copied `-wal` file into the database file and removes it, so a copy taken with the server stopped goes into place as one file. A database file copied without its non-empty `-wal` file failed the check (`database disk image is malformed`); put the files in place only when the check prints `ok`: |
There was a problem hiding this comment.
PRAGMA integrity_check prints ok on a database file copied without its -wal, while the copy lacks what the -wal held: after a crash under writes, twice, and after a clean stop, the file alone answered ok and was missing 115 rows and 115 stream records. A clean stop still left a 4.1 MB -wal with data in no other place, so the page should copy it with the file, or tell the reader integrity_check does not show the loss.
|
|
||
| On a new PostgreSQL server (the old one lost with its databases) create the application role first: a dump holds no roles, and without it `createdb -O extenddb` fails, `role "extenddb" does not exist`. Give it the password that the catalog dump's `data_database_connection_string` row carries (`postgresql://extenddb:<password>@<host>:<port>/extenddb`), the one the config file's `[storage.postgres] connection_string` names as well: `psql -c "CREATE USER extenddb WITH PASSWORD '<password>'" postgres`. In a catalog with a key ring that row is encrypted and the dump no longer gives the password; take it from the config file's `connection_string`, which names the same role. That row also records the data database's host and port, and the server connects to the data database there. On a server at another address, point the config file's `connection_string` at it and, after the restore and before the server starts, set the row. For a catalog without a key ring (no `[auth.encryption] key_file`): `psql -c "UPDATE settings SET value = 'postgresql://extenddb:<password>@<new host>:<new port>/extenddb' WHERE key = 'data_database_connection_string'" extenddb_catalog`. For a catalog with a key ring, the row is encrypted under the data encryption key, so write it with `extenddb encryption-key set-data-connection`, which reads the new string on standard input and needs the KEK file on the new host (the [Encryption Key](#encryption-key) section). Without that the server connects to the old address: it fails to start once nothing answers there, and while the old server still answers, the tables created then keep their items in the old server's data database while the new catalog lists them. | ||
|
|
||
| To back up without stopping the server, take a base backup. `init` creates both databases on one PostgreSQL server, and `pg_basebackup` copies that server as of one instant, so the two databases in the copy agree. The archive holds the server's databases and the write-ahead log that makes them consistent; a 147 MB archive took 3.0 s while a client wrote. The archive holds the catalog, so create it readable by its owner alone: |
There was a problem hiding this comment.
The page shows how to take the pg_basebackup archive but not how to restore it, and no other manual does. Restoring it replaces the whole PostgreSQL server: every database on it, and its pg_hba.conf and postgresql.conf, which a reader should know before choosing it. The command at :1353 does not check its result: a refused pg_basebackup exits 1 and leaves a 0-byte base.tar. The requirements at :1357 leave out max_wal_senders.
| ```bash | ||
| psql -At -c "SELECT count(*) FROM pg_stat_activity WHERE datname IN ('extenddb_catalog', 'extenddb') AND usename = 'extenddb'" postgres | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Optional. On SQLite, table backups live in backups/ beside the database by default and the copy leaves them out: started from another directory, the copy lists an old backup as AVAILABLE and refuses to restore it, "in a backup store other than the one this server is configured with". The example /path/to/backups reads like that folder. The session count query at :1295 names the role extenddb and the default database names, which a deployment may have changed.
What
Adds four procedures to the Data Recovery entry of the admin guide (
docs/manuals/05-admin-guide.md). Main's #855 rewrote that entry with the database names, the dump pair, the rename-aside restore and the role setup that this pull request first added. The entry does not state these four procedures:extenddb stopexits 1 and stops nothing for a server started withserve --foreground, which writes no PID file. It also exits 1 and stops nothing when its--configpath does not name the server's config file. The entry says to stop the server through whatever runs it, then to continue only whenextenddb status --config /path/to/extenddb.toml | grep -q 'is not running'exits 0. On PostgreSQL the entry also counts the application role's sessions on either database inpg_stat_activity, which covers servers on other hosts.VACUUM INTOwhile the server runs, underumask 077, and a warning not to use the sqlite3.backupcommand while the server takes writes.PRAGMA integrity_checkon a SQLite copy in a work directory, before the files go in place.pg_basebackup -D - -Ft -X fetch -c fastfor a PostgreSQL copy taken without stopping the server, with its requirements: theREPLICATIONattribute,wal_level, areplicationline inpg_hba.conf, andwal_keep_sizefor-X fetch.Why
Main's Data Recovery entry says to stop the server before a copy, but it does not say how to check that the server stopped. It names
pg_basebackupwithout its requirements. It gives no way to copy a SQLite deployment while the server runs, and no check of a SQLite copy before a restore.Related: #624 (off-host backup and restore), #707 (keeping the encryption key outside the catalog).
Testing done
Measured on 1 November 2026 at main
f49c5f823, with release builds ofextenddbfor SQLite and for PostgreSQL. The runs used sqlite3 3.46.0 (the SQLite version the server links) and PostgreSQL 16.15 (pgvector/pgvector:pg16).A client sent PutItem and UpdateItem one request at a time to two tables. One of the tables had a GSI, and the UpdateItem requests changed its key. The client logged each acknowledged write with its send and acknowledgement times. It wrote during each copy taken while a server ran, except where a row says no client was writing. For each restored copy a server was started and checked:
InvalidSignatureException.SQLite:
VACUUM INTO, 1 MB database, under writesVACUUM INTO, 284 MB database, about 100 writes a second.backup, 284 MB database, about 100 writes a secondextenddb stopexits 1 (wrote no PID file); theextenddb statuscheck failsPRAGMA integrity_checkon a database file copied without its non-empty-waldatabase disk image is malformedVACUUM INTOfails withnear "INTO": syntax errorPostgreSQL:
extenddb statuscheck fails; with a config whose port nothing listens on, the session count is 19pg_basebackup -Ft -X fetchunder writesconsistent recovery state reached, and ExtendDB served it with the config file unchangedpg_basebackupfrom a non-loopback addressno pg_hba.conf entry for replication connection; works after ahost replicationline and a reloadOn this branch after the merge with main,
bash -npasses on each new command block, and every link and anchor in the file resolves. No added line holds anextenddbversion literal.Found while measuring, not changed here:
extenddb catalog-checkon a consistent restored PostgreSQL pair reports each data table as orphaned and each table as missing. With--fix, it would drop the tables it calls orphaned (catalog-check expects pre-id relation names; --fix drops live data tables on PostgreSQL #652).extenddb servein daemon mode printedextenddb server startedand exited 0, and the daemon then exited because it could not open a damaged SQLite file.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, or public CLI surface, an RFC has been accepted or is linked
below. Otherwise, an ADR captures the decision (link below).
ADR / RFC: n/a
By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.