Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

Restore test: pick the newest dump, not the wal/ folder (PR 1961)

Janikc21b07b44b37
diff --git a/apps/api/src/backup-settings.test.ts b/apps/api/src/backup-settings.test.ts
index 7fd2eed0..7ab1b294 100644
--- a/apps/api/src/backup-settings.test.ts
+++ b/apps/api/src/backup-settings.test.ts
@@ -75,3 +75,37 @@ test('the backup settings are not offered for .env, which the API container read
expect(readFileSync(join(root, '.gitignore'), 'utf8')).toMatch(/^backup\.env$/m)
expect(readFileSync(join(root, '.dockerignore'), 'utf8')).toMatch(/^backup\.env$/m)
})
+
+// WAL archiving keeps base/ and wal/ next to the dumps, and rclone lists folders whatever --include says: the
+// restore took 'wal/' for the newest backup (prod restore test, 2026-11-22). The fake rclone lists like the real
+// one: the folders unless --files-only, and their contents unless --max-depth 1.
+test('the restore picks the newest dump, not the base/ or wal/ folder next to it', () => {
+ writeFileSync(
+ join(dir, 'docker'),
+ [
+ '#!/usr/bin/env bash',
+ `printf '%s\\n' "$*" >> "${dir}/calls"`,
+ 'case " $* " in *" lsf "*) ;; *) exit 0 ;; esac',
+ 'echo linedesk-20261120T090000Z.dump',
+ 'echo linedesk-20261127T215859Z.dump.age',
+ 'case " $* " in *" --files-only "*) ;; *) echo base/; echo wal/ ;; esac',
+ 'case " $* " in *" --max-depth 1 "*) ;; *) echo wal/000000010000000000000001.gz ;; esac',
+ '',
+ ].join('\n'),
+ )
+ const run = spawnSync('bash', [join(root, 'scripts/restore-backup.sh')], {
+ env: { PATH: `${dir}:${process.env.PATH}`, HOME: dir, BACKUP_ENV_FILE: writeSettings(0o600) },
+ encoding: 'utf8',
+ })
+ expect(run.stderr).not.toContain('no backup found')
+ expect(run.stdout).toContain('downloading linedesk-20261127T215859Z.dump.age')
+ // Encrypted and no private key here: it stops right after the download, before any Postgres.
+ expect(run.stderr).toContain('BACKUP_AGE_IDENTITY')
+})
+
+test('the backup list and the cleanup of old backups stay out of base/ and wal/', () => {
+ const run = listBackups(writeSettings(0o600))
+ expect(run.status, run.stderr).toBe(0)
+ expect(readFileSync(join(dir, 'args'), 'utf8').split('\n')).toContain('--max-depth')
+ expect(readFileSync(join(root, 'scripts/backup.sh'), 'utf8')).toMatch(/rclone delete --max-depth 1 /)
+})
diff --git a/docs/backups-wal.md b/docs/backups-wal.md
index 485a637e..fec2d76b 100644
--- a/docs/backups-wal.md
+++ b/docs/backups-wal.md
@@ -31,7 +31,7 @@ Postgres stays on its internal network with no keys and no internet: only the ho
1. `backup.env` set up and the nightly backup working ([self-hosting.md](self-hosting.md#backups)).
2. `.env`: append `:docker-compose.wal.yml` to `COMPOSE_FILE`, then `scripts/deploy.sh --no-build`. Postgres is recreated with the new settings at a moment without calls (the deploy waits for one).
3. `sudo ops/server/setup.sh` (it enables both timers once it sees the file in `COMPOSE_FILE`).
-4. `scripts/wal-base-backup.sh` once, so there is a base backup today; `scripts/backup.sh --list` shows `base/` and `wal/`.
+4. `scripts/wal-base-backup.sh` once, so there is a base backup today; its last line names the upload in `base/`. (`scripts/backup.sh --list` shows only the nightly dumps, not `base/` and `wal/`.)
Encrypted backups: set `BACKUP_AGE_RECIPIENT` in `backup.env` (Security, above); the next run of each timer encrypts. The host needs `age` (`apt install age`).
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 86e6ffc5..52ecc0e0 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -43,8 +43,10 @@ trap 'rm -rf "$work"' EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
+# The dumps only: base/ and wal/ (WAL archiving, docs/backups-wal.md) hold thousands of files, and walking them made
+# every listing and the old-backup cleanup take over 10 minutes.
if [ "${1:-}" = --list ]; then
- rclone lsl "$target"
+ rclone lsl --max-depth 1 "$target"
exit 0
fi
@@ -71,5 +73,5 @@ else
fi
rclone copyto "/work/$name" "$target/$name"
log "deleting backups older than $keep_days days"
-rclone delete --min-age "${keep_days}d" --include 'linedesk-*.dump' --include 'linedesk-*.dump.age' "$target"
+rclone delete --max-depth 1 --min-age "${keep_days}d" --include 'linedesk-*.dump' --include 'linedesk-*.dump.age' "$target"
log "done: $target/$name"
diff --git a/scripts/restore-backup.sh b/scripts/restore-backup.sh
index 70bb3402..c81d0f64 100755
--- a/scripts/restore-backup.sh
+++ b/scripts/restore-backup.sh
@@ -27,8 +27,10 @@ trap 'exit 130' INT
trap 'exit 143' TERM
log() { echo "[restore $(date -u +%FT%TZ)] $*"; }
-# The newest by its time, encrypted or not (the name sorts by time up to the extension).
-name=${1:-$(rclone lsf --include 'linedesk-*.dump' --include 'linedesk-*.dump.age' "$target" | sort -t. -k1,1 | tail -n 1)}
+# The newest by its time, encrypted or not (the name sorts by time up to the extension). Files at the top only:
+# rclone lists folders whatever --include says, and WAL archiving keeps base/ and wal/ next to the dumps
+# (wal/ would sort last and be taken for the newest backup).
+name=${1:-$(rclone lsf --files-only --max-depth 1 --include 'linedesk-*.dump' --include 'linedesk-*.dump.age' "$target" | sort -t. -k1,1 | tail -n 1)}
[[ "$name" =~ ^linedesk-[0-9TZ]+\.dump(\.age)?$ ]] || { echo "restore: no backup found (or not a backup name: '$name')" >&2; exit 1; }
log "downloading $name"
rclone copyto "$target/$name" "/work/$name"