fix(backup): refuse a Postgres restore the role or tools cannot run before anything is copied

The admin restore now answers a privileged role, old client tools or an unreachable server with a 400 naming the reason, before it keeps a pre-restore dump. RESTORE_FROM_BACKUP checks the same before it unpacks the archive, and removes the unpacked copy when the load fails.
This commit is contained in:
Maurice
2026-11-23 14:57:15 +01:00
parent e630e92183
commit 89da19e535
5 changed files with 112 additions and 13 deletions
@@ -1,6 +1,6 @@
import type { DbSettings } from '../../app-config';
import { postgresTarget, type DatabaseEngine } from '../../db/engine';
import { pgDump, pgDumpMigrations, pgRestore } from '../../db/engine/postgres/pg-tools';
import { pgDump, pgDumpMigrations, pgRestore, pgRestoreTarget } from '../../db/engine/postgres/pg-tools';
import { unknownMigrations } from '../../db/known-migrations';
import { DataPathsService } from '../app-config/data-paths.service';
import { logWarn } from '../audit/audit-log.logger';
@@ -106,10 +106,20 @@ export class PostgresDatabaseBackup implements DatabaseBackupStrategy {
await pgDump(url, target, binDir);
}
/** Throws a DomainError (400) with the refusal when the archive cannot be restored here, before anything is replaced. */
/**
* Throws a DomainError (400) with the refusal when the archive cannot be
* restored here, before anything is replaced or copied: the dump's own
* checks, then the tools, the server and the role `pgRestore` would refuse.
*/
async verify(extractDir: string): Promise<void> {
const refusal = await checkPostgresDump(extractDir, this.connection.binDir);
const { url, binDir } = this.connection;
const refusal = await checkPostgresDump(extractDir, binDir);
if (refusal) throw new DomainError(400, refusal);
try {
await pgRestoreTarget(url, binDir);
} catch (err) {
throw new DomainError(400, describeError(err));
}
}
/** A kept copy must be a dump this build can restore: a SQLite file a switch of engine left behind is refused. */