Repository navigation
docs: back up and restore SQLite and PostgreSQL with their own tools #737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
56c59a3
6f978b5
98950fe
d5546bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1284,6 +1284,41 @@ Table backups (`CreateBackup`) hold one table's items and definition; they do no | |
| | 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: | ||
|
|
||
| ```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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
| ``` | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This query names |
||
| ``` | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional. On SQLite, table backups live in |
||
| On SQLite, `VACUUM INTO` copies the database while the server runs. It reads the database in one read transaction and writes a new database file with no `-wal` or `-shm` file. The copy holds the writes committed before its read transaction began, a few milliseconds after the command starts, and the server keeps serving reads and writes while it runs: on a 284 MB database the copy took 2.0 s, with a client making about 100 writes a second. `.timeout 5000` sets the shell's busy timeout (its help reads `Try opening locked tables for MS milliseconds`), so the command waits for a lock instead of failing at once. On SQLite the catalog is the database file, so keep the copy readable by its owner alone: under umask 022 `VACUUM INTO` creates a file with mode 0644, which every local user can read, and under `umask 077` with mode 0600. The commands keep each copy in a directory of its own under `/path/to/backups`, in a file named `extenddb.sqlite` like the database: | ||
|
|
||
| ```bash | ||
| # Copy while the server runs | ||
| umask 077 | ||
| d=/path/to/backups/$(date +%Y%m%d%H%M%S) | ||
| mkdir -p "$d" | ||
| sqlite3 -cmd ".timeout 5000" /path/to/extenddb.sqlite "VACUUM INTO '$d/extenddb.sqlite'" | ||
| ``` | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This paragraph treats
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ```bash | ||
| # Check a copy before it goes in place | ||
| mkdir /path/to/restore-new | ||
| cp -p /path/to/backups/<copy>/extenddb.sqlite* /path/to/restore-new/ | ||
| sqlite3 /path/to/restore-new/extenddb.sqlite 'PRAGMA integrity_check' | ||
| # ok | ||
| ``` | ||
|
|
||
| On PostgreSQL the catalog and data databases are one copy: dump both with every server of the catalog stopped, or, to back up without stopping the server, take a base backup (`pg_basebackup`) of the PostgreSQL server that holds both. Each `pg_dump` takes its own snapshot of one database, so two dumps taken while a server runs disagree when a table is created or deleted between them. Without `[auth.encryption] key_file`, the catalog dump holds the encryption key and the application role's password (in the `data_database_connection_string` setting); with a KEK file, it holds the wrapped data encryption key and that row encrypted, which open only with the KEK (the [Encryption Key](#encryption-key) section). In either case create both files readable by their owner alone, with `umask 077`. With the default names: | ||
|
|
||
| ```bash | ||
|
|
@@ -1311,6 +1346,21 @@ psql -c 'DROP DATABASE extenddb_catalog_before_restore' -c 'DROP DATABASE extend | |
|
|
||
| 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: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The page shows how to take the |
||
|
|
||
| ```bash | ||
| # Base backup, while the server runs, as a role with the REPLICATION attribute | ||
| umask 077 | ||
| pg_basebackup -D - -Ft -X fetch -c fast > base.tar | ||
| ``` | ||
|
|
||
| The PostgreSQL documentation for `pg_basebackup` sets these requirements: | ||
|
|
||
| - The role that runs it is a superuser or has the `REPLICATION` attribute. | ||
| - The server's `wal_level` is `replica` (the default) or `logical`. | ||
| - `pg_hba.conf` allows a replication connection from your host. A line for database `all` does not match a replication connection; it needs `replication` in the database column. The `pgvector/pgvector:pg16` image allows replication connections from the loopback address only: a connection from the container's own non-loopback address failed with `no pg_hba.conf entry for replication connection from host`, and worked once a `host replication all <address>/32 scram-sha-256` line was added and the server reloaded its configuration. | ||
| - With `-X fetch`, the server must still hold all the write-ahead log written during the backup when the backup ends, or the backup fails and is unusable. Set `wal_keep_size` high enough for the log the server writes while the backup runs. | ||
|
|
||
| --- | ||
|
|
||
| ## License | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check reads the port from the config, so it can report a SQLite server stopped while one runs. Started with
serve --port 24204against 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 trueexits 1 with a server running and 0 with none, measured both ways. Separately,extenddb stophere 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.