Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,7 @@ App_Data/
/backups/
*.dump
*.sql.gz

# An update that stopped halfway (scripts/update.sh)
/.update-in-progress
/.update-in-progress.tmp
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ version may change things that need attention when updating, and these notes say

### For people running an archive and for maintainers

- **PostgreSQL 19.** The database image moves from 18 to 19. A new PostgreSQL version cannot open the data of the old
one, so `scripts/update.sh` now moves the archive with the backup: it stops the app, backs up, starts the new database
beside the old data and restores into it. The old database stays in the `db-data` volume, renamed and otherwise untouched, so going back stays
possible; once the archive has run well, remove it with the command the update prints (see "A new PostgreSQL version"
in `docs/operations.md`). The update takes longer than usual and checks first that the volume has room for a second copy of
the database. An update that is interrupted midway leaves `.update-in-progress` and refuses to go on until
`sh scripts/update.sh --recover` has put the old database back. `scripts/restore.sh` has a new `--database-only`. Future
major versions work the same way. `scripts/backup.sh` no longer recreates the database container, and
`scripts/test-update.sh` tests the move.

- **The 1.0 checklist** is written down in `docs/release-1.0.md`: each condition, how it is checked, and where it stands. Some are
not met yet and it says so (a group that has run it for a season, a second person who
can approve a release). `SUPPORT.md` says which versions get security fixes and what is not supported: the newest minor only, as
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
services:
db:
# Pinned by digest; Dependabot updates it.
image: postgres:18-alpine@sha256:123b17998ccaf47e53b502e8c6a1e79afd8f579c6f3eec83c563da026f957395
image: postgres:19-alpine@sha256:8b0ff4cdf221f0c53d7fcda681d3b461be725c492cd881c5ec9c0ad25023f188
environment:
POSTGRES_DB: commonage
POSTGRES_USER: commonage
Expand Down
4 changes: 4 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ upgrades the database when it starts. If the new version does not come up, the s
and restores the backup, so the archive is as it was. Anything added during those minutes would be lost, so update when
nobody is working on the archive. The archive is down for a short time either way.

If the new release uses a newer PostgreSQL, the script moves the database with the backup and keeps the old one in the volume
until you remove it; the release notes say so, and [Backup and restore](operations.md#a-new-postgresql-version) has the
details.

The script is part of Commonage from version 0.3. To get from 0.2 to it once, by hand:

```sh
Expand Down
19 changes: 19 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,25 @@ unchanged tag, merge by themselves once every required check has passed. Each ch
rebase the next waiting update (`.github/workflows/dependabot-auto-merge.yml`), since `main` only accepts pull
requests that are up to date. An update whose checks fail stays open for a maintainer.

**A new major version of `postgres` is not ignored.** `scripts/update.sh` compares the major version of the running
database with the one the new release's `compose.yaml` starts (read from the `image: postgres:<major>` line), and when they
differ it stops the app, backs up, renames the old data in the `db-data` volume (`<major>/docker` to `<major>/kept`: the new
image refuses to start while a folder `/var/lib/postgresql/*/docker` of another major exists, which is why a plain bump
breaks existing archives), starts an empty new database and restores the backup's database into it
(`restore.sh --database-only`); a failed start renames the old data back. A file `.update-in-progress` marks the time in
between, and `update.sh --recover` clears it. `scripts/backup.sh` uses `--no-recreate` so that the backup still reads the
old database. So the Dependabot change to `compose.yaml` is the only change to the scripts and compose files a bump needs, as long as the image keeps this
layout (PostgreSQL 18 and later) and the volume stays mounted at `/var/lib/postgresql`. Besides the pull request itself,
change `postgres:<major>-alpine` in `tests/Commonage.Tests/CommonageAppFactory.cs` and
`tests/Commonage.BrowserTests/BrowserFixture.cs`, which Dependabot does not see. CI job `update` starts the previous
release (PostgreSQL 18 now) and updates to the pull request. It covers: the move to the new major with the data still there;
a failed update going back to the original database (the same cluster comes back); an update killed after the old data
was set aside, the refusal that follows and `--recover`; the old data still being in the volume, and its removal with the
printed command. The `update-from-*` jobs cover the move from each earlier minor. Not covered: a disk too full for the
second copy (the check is only read by hand), a lost connection that is not a kill of the script, and the first major bump
after 19 (20 is untested until it comes; the script reads all numbers from the compose file). Release notes for the release that carries
the bump should say it (see the entry for 19 in `CHANGELOG.md`).

Major updates are never merged automatically. Read the release notes, check in the CI log that all tests ran (a test
runner that finds no tests still succeeds), then merge.

Expand Down
50 changes: 50 additions & 0 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,56 @@ up, deletes all containers and volumes, restores and compares, and checks that n
without `COMMONAGE_LOGIN_KEY`. It runs in CI on every pull request, as its own
Compose project on ports 18080 and 15432, so it can also run next to your own archive.

### A new PostgreSQL version

Now and then a release moves to a new major version of PostgreSQL (18 to 19, for example), which cannot open the data of
the old one. `scripts/update.sh` notices this and handles it, without you doing anything extra. It stops the app, makes
the backup, checks its checksums again, sets the old data aside, starts an empty new database and restores the backup's
database into it; the uploaded files stay as they are. The old database is not deleted: the script only renames its folder
in the `db-data` volume (`/var/lib/postgresql/18/docker` becomes `/var/lib/postgresql/18/kept`), because the new PostgreSQL
image refuses to start next to it. If the new version does not come up, the script renames it back and starts the
previous release on it, so the archive is as it was. The archive is down while this happens, longer than for an ordinary
update, in proportion to the size of the database.

**If the update is interrupted** (the connection drops, the power goes) after the old database was set aside, the file
`.update-in-progress` stays in the folder, and `scripts/update.sh` refuses to do anything else until it is cleared up. It prints
where the old database is and what to do; the short way is:

```sh
sh scripts/update.sh --recover
```

It puts the archive back on the release it was on, with its old database. Nothing is lost: the database is in the volume
and the backup is in the backup folder. The recovery can be run again if it is interrupted too.

**Disk space.** Until you remove the old database, the volume holds the database twice, and the backup is a third copy.
The update checks the free space for the backup, and also the free space of the database volume for the second copy (about
one and a half times the size of the database), and stops before changing anything when it is short.

**The old database holds the archive's data, personal data of contributors included.** Free the space, and the data,
when the archive has run well for a while (a week, say). The update prints this command, with the right version number:

```sh
docker compose run --rm --no-deps --entrypoint rm db -rf /var/lib/postgresql/18
```

This is final. Until then, going back to the previous release later is possible. Stop the archive
(`docker compose stop web db`), check out the previous release (and set `COMMONAGE_VERSION` in `.env` as before), then,
with your two version numbers:

```sh
docker compose run --rm --no-deps --entrypoint sh db -c 'ls /var/lib/postgresql/18; rm -rf /var/lib/postgresql/19/docker'
```

If the listing shows `kept` and no `docker`, run `mv /var/lib/postgresql/18/kept /var/lib/postgresql/18/docker` the same way.
If it shows `docker` as well, a database of the old version was started in between: do not move anything, and restore the
newest backup with `scripts/restore.sh` instead, which also brings the data back, although only as of that backup. Then
`docker compose up -d`. Whatever was added to the archive since the update is not in the old database: restore the newest
backup afterwards (`scripts/restore.sh`) to have it.

An archive that is moved by hand (`git checkout` and `docker compose up`, without the script) does not start: the new
database refuses a volume that holds the old one. Use `scripts/update.sh`.

## Demo archive

`COMMONAGE_DEMO=en` or `COMMONAGE_DEMO=de` in `.env` fills a new, empty archive with made-up content when the app
Expand Down
1 change: 1 addition & 0 deletions docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ vulnerability, see [SECURITY.md](../SECURITY.md).
| Secret | Where it is, and how it is protected |
|---|---|
| Passwords | Only as hashes in the database (ASP.NET Core Identity, PBKDF2 with a salt per password). |
| Old database after a PostgreSQL major upgrade | `scripts/update.sh` keeps the old database in the database volume as `/var/lib/postgresql/<major>/kept`, so that going back is possible. It holds everything in the archive, contributors' personal data included, until a curator removes it with the command the update prints (also in `docs/operations.md`). It is in the volume only, owned by the database, not reachable from the web; the backup made for the move is written owner-only (`umask 077`) like every backup, and holds the same data. `.update-in-progress` holds only paths and version numbers. Accepted: until removed, deleted items and anything erased since stay in that copy. `scripts/test-update.sh`. |
| Login keys | The keys that sign login cookies, setup links and form tokens are in the database (`DataProtectionKeys`), so that logins survive a restore. Each is encrypted with AES-256-GCM with `COMMONAGE_LOGIN_KEY` (`Security/LoginKeys.cs`), which is in `.env` only, not in the database or the backups. Tested in `LoginKeyTests` and in the backup-and-restore test, which also checks that no key in the database can be read. |
| Authenticator secrets and recovery codes | In the database, encrypted with the login keys (`ProtectedUserStore`), so they need `COMMONAGE_LOGIN_KEY` as well. Recovery codes are shown once and kept nowhere else. |
| Setup links | Not stored. They are signed with the login keys, work once and for seven days, and a new one makes older ones invalid. |
Expand Down
4 changes: 3 additions & 1 deletion scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ sha256() {
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$@"; else shasum -a 256 "$@"; fi
}

docker compose up -d --wait db >/dev/null
# Never recreates the database container: during an update that moves to a new PostgreSQL major, the new compose files
# would start an empty database instead of the one that has to be backed up.
docker compose up -d --wait --no-recreate db >/dev/null

# The database first, then the files: an upload in between leaves an extra file, which does no harm.
echo "Backing up the database..."
Expand Down
33 changes: 24 additions & 9 deletions scripts/restore.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/bin/sh
# Restores a backup made with scripts/backup.sh. Everything currently in the archive is replaced.
#
# scripts/restore.sh [--yes] <backup-folder>
# scripts/restore.sh [--yes] [--database-only] <backup-folder>
#
# --database-only leaves the uploaded files as they are. scripts/update.sh uses it when it moves the archive to a new
# PostgreSQL version, which changes nothing in the files.
#
# Restore into the same or a newer version of Commonage; the app upgrades the database when it starts.
# See docs/operations.md.
Expand All @@ -10,12 +13,18 @@ set -eu
export MSYS_NO_PATHCONV=1

confirmed=no
if [ "${1:-}" = "--yes" ]; then
confirmed=yes
database_only=no
while [ $# -gt 0 ]; do
case "$1" in
--yes) confirmed=yes ;;
--database-only) database_only=yes ;;
-*) echo "Usage: scripts/restore.sh [--yes] [--database-only] <backup-folder>" >&2; exit 2 ;;
*) break ;;
esac
shift
fi
done
if [ $# -ne 1 ]; then
echo "Usage: scripts/restore.sh [--yes] <backup-folder>" >&2
echo "Usage: scripts/restore.sh [--yes] [--database-only] <backup-folder>" >&2
exit 2
fi

Expand All @@ -34,7 +43,9 @@ else
fi

if [ "$confirmed" != yes ]; then
printf 'This replaces the whole archive (database and files) with the backup in %s.\nType "restore" to continue: ' "$folder"
scope="database and files"
[ "$database_only" != yes ] || scope="database"
printf 'This replaces the whole archive (%s) with the backup in %s.\nType "restore" to continue: ' "$scope" "$folder"
read -r answer
[ "$answer" = restore ] || { echo "Nothing was changed."; exit 1; }
fi
Expand All @@ -48,9 +59,13 @@ docker compose exec -T db dropdb --username=commonage --if-exists --force common
docker compose exec -T db createdb --username=commonage commonage
docker compose exec -T db pg_restore --username=commonage --dbname=commonage --no-owner --exit-on-error < "$folder/database.dump"

echo "Restoring the uploaded files..."
docker compose run --rm --no-deps -T --entrypoint sh web -c \
'find /app/App_Data/media -mindepth 1 -delete && tar -C /app/App_Data -xf -' < "$folder/media.tar"
if [ "$database_only" = yes ]; then
echo "Leaving the uploaded files as they are."
else
echo "Restoring the uploaded files..."
docker compose run --rm --no-deps -T --entrypoint sh web -c \
'find /app/App_Data/media -mindepth 1 -delete && tar -C /app/App_Data -xf -' < "$folder/media.tar"
fi

echo "Starting the app..."
docker compose up -d web >/dev/null
Expand Down
Loading