Restore a full backup from Settings
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s

Settings → Data → Import gets "Restore a full backup" in place of the V1
import row. The zip from "Download backup" is uploaded and replaces the
database, every setting and the uploaded files on the running server:

- the staged database is upgraded to this schema (newer backups are refused),
  saved keys are re-encrypted for this server's key, interrupted jobs are
  marked, and the restoring session is carried over;
- a pre-restore snapshot is written, running generations stop, and the file
  is swapped under the open DB handle (DB.replaceWith); uploads and media are
  copied into place and background work restarts.

The /api/import/v1 endpoint stays for the V1 cutover (docs/MIGRATION.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-16 12:33:42 -07:00
parent e0bc7b580e
commit 7125dec3e0
13 changed files with 521 additions and 72 deletions

View file

@ -14,7 +14,7 @@ function bind(params: Param[]): (string | number | bigint | null | Uint8Array)[]
* exactly what a one-user app in one container needs, with no native add-on to build.
*/
export class DB {
readonly raw: DatabaseSync;
private handle: DatabaseSync;
readonly path: string;
private statements = new Map<string, StatementSync>();
private txDepth = 0;
@ -23,11 +23,53 @@ export class DB {
constructor(file: string) {
this.path = file;
this.raw = new DatabaseSync(file);
this.raw.exec('PRAGMA journal_mode = WAL');
this.raw.exec('PRAGMA synchronous = NORMAL');
this.raw.exec('PRAGMA foreign_keys = ON');
this.raw.exec('PRAGMA busy_timeout = 5000');
this.handle = DB.open(file);
}
private static open(file: string): DatabaseSync {
const raw = new DatabaseSync(file);
raw.exec('PRAGMA journal_mode = WAL');
raw.exec('PRAGMA synchronous = NORMAL');
raw.exec('PRAGMA foreign_keys = ON');
raw.exec('PRAGMA busy_timeout = 5000');
return raw;
}
/** The node:sqlite handle. Changes when a full backup is restored (`replaceWith`). */
get raw(): DatabaseSync {
return this.handle;
}
/**
* Swaps the database file for `file` (a complete, closed SQLite file in the same folder) under
* this same object, so every part of the app keeps working with the restored data. If the
* swap fails, the current database is reopened untouched.
*/
replaceWith(file: string): void {
if (this.txDepth > 0) throw new Error('A database transaction is still open.');
if (this.path === ':memory:') throw new Error('An in-memory database cannot be replaced.');
this.statements.clear();
try {
this.handle.exec('PRAGMA wal_checkpoint(TRUNCATE)');
} catch {
/* closing writes it anyway */
}
this.handle.close();
try {
const aside = `${this.path}.replaced`;
fs.rmSync(aside, { force: true });
fs.renameSync(this.path, aside);
try {
fs.renameSync(file, this.path);
} catch (e) {
fs.renameSync(aside, this.path);
throw e;
}
for (const ext of ['-wal', '-shm']) fs.rmSync(`${this.path}${ext}`, { force: true });
fs.rmSync(aside, { force: true });
} finally {
this.handle = DB.open(this.path);
}
}
private stmt(sql: string): StatementSync {