Restore a full backup from Settings
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s
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:
parent
e0bc7b580e
commit
7125dec3e0
13 changed files with 521 additions and 72 deletions
|
|
@ -11,10 +11,14 @@ import { appSettingsSchema, deleteSetting, getAppSettings, getSetting, setSettin
|
|||
import { createSnapshot, exportBundle, fullBackupStream, listBackups } from '../services/backup.ts';
|
||||
import { createClass, createDeck, createFolder, createNote, createQuiz, draftCardToInput, draftQuestionToInput, getClass, getDeck, getQuiz, listCards, listQuestions } from '../services/content.ts';
|
||||
import { createTemplate, deleteTemplate, listTemplates, resetTemplate, templateInputSchema, updateTemplate } from '../services/templates.ts';
|
||||
import { restoreFullBackup } from '../services/restore.ts';
|
||||
import { importV1 } from '../services/v1import.ts';
|
||||
import { sessionHash } from '../auth.ts';
|
||||
import { slugify } from '../util.ts';
|
||||
|
||||
export const APP_VERSION = '2.0.0';
|
||||
/** The largest full backup that can be restored through the app. */
|
||||
const RESTORE_MAX_BYTES = 20 * 1024 ** 3;
|
||||
|
||||
function dirSize(dir: string): number {
|
||||
try {
|
||||
|
|
@ -176,7 +180,45 @@ export function dataRoutes(app: AppContext): Hono {
|
|||
return c.json({ summary }, 201);
|
||||
});
|
||||
|
||||
/** Migration from a V1 database file. `?preview=1` only reports what would be imported. */
|
||||
/**
|
||||
* Restores a full backup zip (from "Download backup"): the database with every setting, and the
|
||||
* uploaded files. The zip is the raw request body, streamed to disk. See services/restore.ts.
|
||||
*/
|
||||
r.post('/backup/restore', async (c) => {
|
||||
const max = RESTORE_MAX_BYTES;
|
||||
const declared = Number(c.req.header('content-length') ?? 0);
|
||||
if (declared > max) throw new HttpError(413, 'too_large', 'The backup is larger than this server accepts.');
|
||||
const body = c.req.raw.body;
|
||||
if (!body) throw badRequest('Choose a backup zip');
|
||||
const tmp = path.join(app.config.dataDir, `.restore-upload-${Date.now()}.zip`);
|
||||
try {
|
||||
let size = 0;
|
||||
const out = fs.createWriteStream(tmp);
|
||||
try {
|
||||
for await (const chunk of body as unknown as AsyncIterable<Uint8Array>) {
|
||||
size += chunk.length;
|
||||
if (size > max) throw new HttpError(413, 'too_large', 'The backup is larger than this server accepts.');
|
||||
if (!out.write(chunk)) await new Promise((r) => out.once('drain', r));
|
||||
}
|
||||
} finally {
|
||||
await new Promise<void>((resolve, reject) => out.end((err?: Error | null) => (err ? reject(err) : resolve())));
|
||||
}
|
||||
if (size < 22) throw badRequest('Choose a backup zip');
|
||||
const head = Buffer.alloc(4);
|
||||
const fd = fs.openSync(tmp, 'r');
|
||||
fs.readSync(fd, head, 0, 4, 0);
|
||||
fs.closeSync(fd);
|
||||
if (head.readUInt32LE(0) !== 0x04034b50) throw badRequest('That file is not a zip. Choose a file made with "Download backup".');
|
||||
return c.json({ summary: await restoreFullBackup(app, tmp, { sessionHash: sessionHash(c) }) });
|
||||
} finally {
|
||||
fs.rmSync(tmp, { force: true });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Migration from a V1 database file. `?preview=1` only reports what would be imported. Not in
|
||||
* Settings any more (full-backup restore took its place); see docs/MIGRATION.md.
|
||||
*/
|
||||
r.post('/import/v1', bodyLimit({ maxSize: 500 * 1024 * 1024 }), async (c) => {
|
||||
const form = await c.req.parseBody().catch(() => {
|
||||
throw badRequest('Upload could not be read');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue