Files
TREK/server/src/nest/backup/backup.module.ts
T
Maurice 0aa5b36661 fix(backup): back up and restore through an injected database port
Backup, restore and the demo reset each handled the SQLite file
themselves: backup.impl closed and reinitialized the connection, built
MaintenanceRepository with new, and assumed the database was
data/travel.db. That tied the whole chain to one local file and hid two
bugs.

They now go through DATABASE_BACKUP, a port declared in the shared
database kernel (snapshot, verify, keep a copy before a restore, replace).
SqliteDatabaseBackup implements it with what the code did before:
VACUUM INTO for a consistent copy, and a close, copy, sidecar drop,
rename and reopen through DatabaseLifecycle for a restore. BackupService
injects it and hands it to backup.impl. The demo reset and the baseline
save use the same port; the first baseline is saved by DemoResetJob once
the app is up, because the boot-time seed runs before anything can be
injected. Another engine can plug in a logical dump behind the port.

Fixes on the way:

- The backup and the restore ignored TREK_DB_FILE. The backup looked for
  data/travel.db and, with the database elsewhere, silently left it out
  of the archive; the restore wrote data/travel.db and never replaced
  the live database. Both now use DatabaseLifecycle.file, the path the
  connection runs on, and the pre-restore copy sits next to it.
- A failed VACUUM INTO fell back to archiving the live WAL-mode file
  without a word, the torn copy the snapshot exists to prevent. The
  backup now fails: nothing is archived or committed, the existing
  backups are untouched, a partial snapshot is removed, and the log says
  why. A missing database file and a failed checkpoint are logged too.
2026-10-08 18:38:28 +02:00

51 lines
2.0 KiB
TypeScript

import { Module } from '@nestjs/common';
import { MulterModule } from '@nestjs/platform-express';
import type { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
import { BackupController } from './backup.controller';
import { BackupService } from './backup.service';
import { AutoBackupJob } from './auto-backup.job';
import { AuditModule } from '../audit/audit.module';
import { SchedulingModule } from '../scheduling/scheduling.module';
import { AppConfigModule } from '../app-config/app-config.module';
import { StorageModule } from '../storage/storage.module';
import { StorageService } from '../storage/storage.service';
import { MAX_BACKUP_UPLOAD_SIZE } from './backup.impl';
import { DatabaseBackupModule } from './database-backup.module';
/**
* Multer options for the restore upload. The uploaded zip is a restore INPUT,
* not a stored object — it gets no storage category and spools to the
* driver-agnostic scratch dir (data/tmp), where the handler consumes and
* unlinks it. Resolved through DI so no path is computed at module load.
*/
export function buildBackupUploadOptions(storage: StorageService): MulterOptions {
return {
dest: storage.tempDir(),
fileFilter: (_req: unknown, file: Express.Multer.File, cb: (err: Error | null, accept: boolean) => void) => {
if (file.originalname.endsWith('.zip')) return cb(null, true);
cb(new Error('Only ZIP files allowed'), false);
},
limits: { fileSize: MAX_BACKUP_UPLOAD_SIZE },
};
}
@Module({
imports: [
AppConfigModule,
AuditModule,
// Global, so the admin domain sees the port too; imported here because the
// backup domain is what it belongs to.
DatabaseBackupModule,
SchedulingModule,
StorageModule,
MulterModule.registerAsync({
imports: [StorageModule],
inject: [StorageService],
useFactory: buildBackupUploadOptions,
}),
],
controllers: [BackupController],
providers: [BackupService, AutoBackupJob],
})
export class BackupModule {}