Harden private media and backup recovery; add V2 deployment and migration guides

This commit is contained in:
Elijah 2026-11-11 19:31:32 -07:00
parent c549c34f60
commit ffaa1f6afa
19 changed files with 623 additions and 14 deletions

View file

@ -89,6 +89,10 @@ export function fullBackupStream(app: AppContext): { stream: ReadableStream<Uint
const tmp = path.join(app.config.backupsDir, `.export-${Date.now()}.db`);
app.db.snapshot(tmp);
const entries: { name: string; file: string }[] = [{ name: 'study.db', file: tmp }];
// Include the generated key so a full backup can actually restore encrypted AI settings.
// APP_SECRET is deliberately never exported; deployments using it must retain it separately.
const keyFile = path.join(app.config.dataDir, 'secret.key');
if (fs.existsSync(keyFile)) entries.push({ name: 'secret.key', file: keyFile });
for (const [dir, prefix] of [
[app.config.uploadsDir, 'uploads'],
[app.config.mediaDir, 'media'],
@ -104,7 +108,7 @@ export function fullBackupStream(app: AppContext): { stream: ReadableStream<Uint
version: 1,
createdAt: new Date().toISOString(),
files: entries.length,
restore: 'Stop the container, copy study.db, uploads/ and media/ into the data folder (replacing what is there), keep your existing secret.key, then start the container.',
restore: 'Stop the container. Restore into a NEW data folder: study.db, uploads/, media/ and secret.key if present. Preserve the original APP_SECRET separately if configured. Never retain old study.db-wal or study.db-shm files. Keep the old folder intact until the restored installation is verified.',
};
let index = -1;
let controllerRef: ReadableStreamDefaultController<Uint8Array> | null = null;
@ -139,11 +143,14 @@ export function fullBackupStream(app: AppContext): { stream: ReadableStream<Uint
zip.add(entry);
try {
entry.push(new Uint8Array(fs.readFileSync(file)), true);
} catch {
entry.push(new Uint8Array(0), true);
} catch (error) {
controllerRef?.error(error);
zip.terminate();
cleanup();
}
},
cancel() {
zip.terminate();
cleanup();
},
});