Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/manuals/05-admin-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Collaborator

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 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.


```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'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

```

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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`:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


```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
Expand Down Expand Up @@ -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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
# 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
Expand Down
Loading