From 08b086a71eeded0f22164ac27dad01096572f37e Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Tue, 20 Oct 2026 13:20:55 +0200 Subject: [PATCH] A crash-safe storage, savegames and daily backups - Writes go to a flushed temporary file and are renamed into place; the last intact version stays as .bak and reads fall back to it, so a power cut never leaves a damaged save. Same file format as before. - Savegames per account project (newest 30, one per day for 90 days), kept before restores and deletions; deleted projects restorable for 30 days. Projects dialog: savegames per project and "Recently deleted". - Daily gzip backup of data/ into backups/ (14 newest + monthly), restore script scripts/restore-backup.mjs. Co-Authored-By: Claude Opus 5.5 (1M context) --- .dockerignore | 1 + .gitignore | 1 + README.md | 23 ++- app/components/editor/App.vue | 1 + app/components/editor/ProjectsDialog.vue | 39 +++++ app/components/editor/VersionsDialog.vue | 66 ++++++++ app/composables/useEditor.ts | 71 ++++++++- app/lib/logic/storage.ts | 2 + deploy.sh | 2 +- docker-compose.yml | 4 +- nuxt.config.ts | 11 +- scripts/restore-backup.mjs | 47 ++++++ .../{[id].delete.ts => [id]/index.delete.ts} | 5 + .../{[id].get.ts => [id]/index.get.ts} | 0 .../{[id].put.ts => [id]/index.put.ts} | 11 +- server/api/me/projects/[id]/versions.get.ts | 13 ++ .../api/me/projects/[id]/versions/[v].get.ts | 10 ++ server/api/me/trash.get.ts | 15 ++ server/plugins/backup.ts | 10 ++ server/plugins/storage.ts | 11 ++ server/utils/backup.ts | 75 +++++++++ server/utils/history.ts | 71 +++++++++ server/utils/safe-fs.ts | 121 ++++++++++++++ tests/durability.test.ts | 149 ++++++++++++++++++ 24 files changed, 740 insertions(+), 19 deletions(-) create mode 100644 app/components/editor/VersionsDialog.vue create mode 100755 scripts/restore-backup.mjs rename server/api/me/projects/{[id].delete.ts => [id]/index.delete.ts} (67%) rename server/api/me/projects/{[id].get.ts => [id]/index.get.ts} (100%) rename server/api/me/projects/{[id].put.ts => [id]/index.put.ts} (83%) create mode 100644 server/api/me/projects/[id]/versions.get.ts create mode 100644 server/api/me/projects/[id]/versions/[v].get.ts create mode 100644 server/api/me/trash.get.ts create mode 100644 server/plugins/backup.ts create mode 100644 server/plugins/storage.ts create mode 100644 server/utils/backup.ts create mode 100644 server/utils/history.ts create mode 100644 server/utils/safe-fs.ts create mode 100644 tests/durability.test.ts diff --git a/.dockerignore b/.dockerignore index a5b5b7f..1648347 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,4 @@ localhost-key.pem *.map *.log data +backups diff --git a/.gitignore b/.gitignore index 176aeb2..40a6c62 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ playwright-report/ # Shared projects (server storage) data/ +backups/ diff --git a/README.md b/README.md index c4493b4..0d0f8c0 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,26 @@ The editor UI is in [app/components/editor/](app/components/editor/) with state ## Accounts -[server/api/auth/](server/api/auth/) (register, login, logout, me, password, delete) and [server/api/me/projects/](server/api/me/projects/) (list, get, put, delete). Passwords are hashed with scrypt; the session is a random token in an httpOnly, SameSite=Lax cookie, stored server-side as a SHA-256. API calls that change something are refused when their Origin is another site. Storage (Nitro fs): `data/users`, `data/sessions`, `data/accounts//` with an index (revision per project, deletions for a year) and one file per project. Each save sends the revision it started from; a mismatch answers 409 and the browser keeps the other version as a copy. Limits: 1000 projects and 100 MB per account, 2 MB per project, login 20 tries per 10 minutes per IP, 10 new accounts per hour per IP. +[server/api/auth/](server/api/auth/) (register, login, logout, me, password, delete) and [server/api/me/projects/](server/api/me/projects/) (list, get, put, delete). Passwords are hashed with scrypt; the session is a random token in an httpOnly, SameSite=Lax cookie, stored server-side as a SHA-256. API calls that change something are refused when their Origin is another site. Storage: `data/users`, `data/sessions`, `data/accounts//` with an index (revision per project, deletions for a year), one file per project, and savegames in `h//`. Each save sends the revision it started from; a mismatch answers 409 and the browser keeps the other version as a copy. Limits: 1000 projects and 100 MB per account, 2 MB per project, login 20 tries per 10 minutes per IP, 10 new accounts per hour per IP. + +## Power cuts, savegames, backups + +No database: everything is JSON files under `data/`, which keeps memory use flat. Three layers protect it: + +- **Crash-safe writes** ([server/utils/safe-fs.ts](server/utils/safe-fs.ts), mounted in [server/plugins/storage.ts](server/plugins/storage.ts)). A save writes a temporary file, flushes it to disk, keeps the current intact version as `.bak` and then renames the new file into place. A power cut at any moment leaves the old or the new version; reading falls back to `.bak` when a file is missing or damaged, and leftover temporary files are removed at start. +- **Savegames** ([server/utils/history.ts](server/utils/history.ts)). Before a save replaces an account project, its state is kept as a version (at most one per 15 minutes; always before a restore and on deletion). Kept: the newest 30 plus the last of each day for 90 days. Deleted projects stay restorable for 30 days. In the app: Projects dialog, clock icon (restore, or open as copy), and "Recently deleted". +- **Daily backup** ([server/utils/backup.ts](server/utils/backup.ts), [server/plugins/backup.ts](server/plugins/backup.ts)). Once a day the production server writes all of `data/` into `backups/logigate-YYYY-MM-DD.ndjson.gz`, leaving out damaged files. Kept: the newest 14 plus the first of each month for a year. `backups/` sits next to `data/` on the same disk, so it protects against damaged files and mistakes, not against losing the disk; copy it elsewhere for that. + +Restore a backup on edgerealm (the current `data/` is moved aside first, so this can be undone): + +```bash +cd /mnt/HC_Volume_102040804/srv/logigate +docker compose stop frontend +docker run --rm -v "$PWD":/app -w /app node:current-slim node scripts/restore-backup.mjs backups/logigate-2026-10-20.ndjson.gz +docker compose start frontend +``` + +In the browser, each localStorage write is atomic, so a power cut there loses at most the last few seconds. ## Compatibility @@ -59,4 +78,4 @@ pnpm build # .output/ ## Deploy -Same pattern as autismus-spektrum. On edgerealm the repo lives in `/mnt/HC_Volume_102040804/srv/logigate`; run `./deploy.sh` there. It pulls, builds in a one-shot container and recreates the `logigate` container. Share links and accounts live in `data/` next to the repo (mounted into the container), so redeploys keep them; include that folder in backups. Host nginx terminates TLS with the `*.blax.at` wildcard cert and forwards to Traefik, which routes `logicate.blax.at` and `logigate.blax.at` by the labels in [docker-compose.yml](docker-compose.yml). +Same pattern as autismus-spektrum. On edgerealm the repo lives in `/mnt/HC_Volume_102040804/srv/logigate`; run `./deploy.sh` there. It pulls, builds in a one-shot container and recreates the `logigate` container. Share links and accounts live in `data/` and the daily backups in `backups/`, both next to the repo and mounted into the container, so redeploys keep them. Host nginx terminates TLS with the `*.blax.at` wildcard cert and forwards to Traefik, which routes `logicate.blax.at` and `logigate.blax.at` by the labels in [docker-compose.yml](docker-compose.yml). diff --git a/app/components/editor/App.vue b/app/components/editor/App.vue index 42098cb..0d8e211 100644 --- a/app/components/editor/App.vue +++ b/app/components/editor/App.vue @@ -31,5 +31,6 @@ onMounted(() => { + diff --git a/app/components/editor/ProjectsDialog.vue b/app/components/editor/ProjectsDialog.vue index 3675004..6c0a5b8 100644 --- a/app/components/editor/ProjectsDialog.vue +++ b/app/components/editor/ProjectsDialog.vue @@ -6,6 +6,17 @@ const renameText = ref('') const confirmDelete = ref(null) const close = () => (ed.dialog.value = null) + +const showTrash = ref(false) +const trash = ref<{ id: string; name: string; deleted: number; v: number }[] | null>(null) +async function toggleTrash() { + showTrash.value = !showTrash.value + if (showTrash.value) trash.value = await ed.listTrash().catch(() => []) +} +async function restoreDeleted(t: { id: string; v: number }) { + await ed.restoreDeleted(t) + close() +} const when = (t: number) => new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' }).format(t) function open(id: string) { @@ -69,6 +80,14 @@ function onFile(e: Event) { + @@ -76,6 +95,26 @@ function onFile(e: Event) { + +
diff --git a/app/components/editor/VersionsDialog.vue b/app/components/editor/VersionsDialog.vue new file mode 100644 index 0000000..dd8dd7c --- /dev/null +++ b/app/components/editor/VersionsDialog.vue @@ -0,0 +1,66 @@ + + + diff --git a/app/composables/useEditor.ts b/app/composables/useEditor.ts index 277b0a5..45b7453 100644 --- a/app/composables/useEditor.ts +++ b/app/composables/useEditor.ts @@ -105,7 +105,9 @@ function create() { const notice = ref<{ text: string; kind: 'ok' | 'error' } | null>(null) const labelFocus = ref(0) const showHelp = ref(false) - const dialog = ref(null) + const dialog = ref(null) + /** Project whose savegames the versions dialog shows. */ + const versionsFor = ref(null) let noticeTimer: ReturnType | undefined function notify(text: string, kind: 'ok' | 'error' = 'ok') { @@ -855,14 +857,14 @@ function create() { async function upload(m: ProjectMeta, uid: string) { const p = localProject(m) if (!p) return - const body = { project: p, baseRev: m.cloud?.owner === uid ? m.cloud.rev : 0, link: m.link } + const body = { project: p, baseRev: m.cloud?.owner === uid ? m.cloud.rev : 0, link: m.link, snapshot: m.snapshot === true } let res = await authCall<{ meta?: CloudMeta; conflict?: boolean; status: number }>('PUT', `/api/me/projects/${m.id}`, body) if (res.conflict) { await keepRemoteCopy(m.id) res = await authCall('PUT', `/api/me/projects/${m.id}`, { ...body, force: true }) notify(`"${m.name}" was also changed on another device. That version is kept as a copy.`) } - if (res.meta) updateMeta(m.id, { cloud: { owner: uid, rev: res.meta.rev }, dirty: false }) + if (res.meta) updateMeta(m.id, { cloud: { owner: uid, rev: res.meta.rev }, dirty: false, snapshot: undefined }) } let syncingAccount = false @@ -999,6 +1001,64 @@ function create() { notify('Account deleted. Your projects are still in this browser.') } + // ── Savegames ───────────────────────────────────────────────────── + interface VersionInfo { + v: number + at: number + rev: number + name: string + size: number + } + + async function listVersions(projectId: string): Promise { + const res = await authCall<{ versions: VersionInfo[]; status: number }>('GET', `/api/me/projects/${projectId}/versions`) + return res.versions ?? [] + } + + async function fetchVersion(projectId: string, v: number): Promise { + const res = await authCall<{ version?: { project: unknown }; status: number }>('GET', `/api/me/projects/${projectId}/versions/${v}`) + if (!res.version) throw new Error('Version not found') + return toProject(res.version.project) + } + + /** + * Brings back a savegame: as a new project, or in place of the current + * state (which the server keeps as a savegame first, so this can be undone). + */ + async function restoreVersion(projectId: string, v: number, asCopy: boolean) { + if (projectId === meta.value.id && saveTimer) persistNow() + const p = await fetchVersion(projectId, v) + if (asCopy) { + p.name = `${p.name} (${new Date(v).toLocaleDateString()})` + activate(addProject(p), p) + notify(`Opened the saved version as "${p.name}"`) + return + } + const m = listProjects().find(x => x.id === projectId) + if (!m) return + const next: ProjectMeta = { ...m, name: p.name, updated: Date.now(), dirty: true, snapshot: true } + saveProject(next, p) + activate(next, p) + scheduleCloud() + notify('Restored. The state before is kept as a savegame too.') + } + + async function listTrash(): Promise<{ id: string; name: string; deleted: number; v: number }[]> { + const res = await authCall<{ projects: { id: string; name: string; deleted: number; v: number }[]; status: number }>('GET', '/api/me/trash') + return res.projects ?? [] + } + + async function restoreDeleted(item: { id: string; v: number }) { + const p = await fetchVersion(item.id, item.v) + persistNow() + setPendingDeletes(pendingDeletes().filter(d => d.id !== item.id)) + const m: ProjectMeta = { id: item.id, name: p.name, updated: Date.now(), dirty: true } + saveProject(m, p) + activate(m, p) + scheduleCloud() + notify(`"${p.name}" is back`) + } + /** Checks the session with the server, then syncs; again on focus and every minute. */ async function startAccount() { try { @@ -1114,6 +1174,11 @@ function create() { changePassword, deleteAccount, startAccount, + versionsFor, + listVersions, + restoreVersion, + listTrash, + restoreDeleted, copyText, shape, } diff --git a/app/lib/logic/storage.ts b/app/lib/logic/storage.ts index 18d6fc9..18e812d 100644 --- a/app/lib/logic/storage.ts +++ b/app/lib/logic/storage.ts @@ -21,6 +21,8 @@ export interface ProjectMeta { dirty?: boolean /** The example project a new browser starts with, never touched yet. */ starter?: boolean + /** Ask the server to keep the state before the next upload as a savegame (after restoring an old version). */ + snapshot?: boolean } export interface AccountUser { diff --git a/deploy.sh b/deploy.sh index 0f679c1..c4ec38d 100755 --- a/deploy.sh +++ b/deploy.sh @@ -68,7 +68,7 @@ if [ ! -f .env ]; then fi echo "==> Preparing docker data directories..." -mkdir -p docker-data/node_modules data/shares +mkdir -p docker-data/node_modules data/shares backups if [ "$(id -u)" -eq 0 ]; then echo "==> Fixing ownership on writable directories (root mode)..." diff --git a/docker-compose.yml b/docker-compose.yml index bfa2b08..11555d5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,8 +27,10 @@ services: - .env volumes: - ./.output:/app/.output:ro - # Projects shared by link (one JSON file each). Survives redeploys. + # Share links and accounts (JSON files). Survives redeploys. - ./data:/app/data + # Daily backups of data/, see scripts/restore-backup.mjs. + - ./backups:/app/backups labels: traefik.enable: "true" traefik.docker.network: "web" diff --git a/nuxt.config.ts b/nuxt.config.ts index 25bb99f..7091d84 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -28,15 +28,8 @@ export default defineNuxtConfig({ clientBundle: { scan: true }, }, - // Share links and accounts live as JSON files in data/ (a volume in production). - nitro: { - storage: { - shares: { driver: 'fs', base: './data/shares' }, - users: { driver: 'fs', base: './data/users' }, - sessions: { driver: 'fs', base: './data/sessions' }, - accounts: { driver: 'fs', base: './data/accounts' }, - }, - }, + // Share links and accounts live as JSON files in data/ (a volume in + // production), written crash-safe; see server/plugins/storage.ts. runtimeConfig: { public: { diff --git a/scripts/restore-backup.mjs b/scripts/restore-backup.mjs new file mode 100755 index 0000000..90f0267 --- /dev/null +++ b/scripts/restore-backup.mjs @@ -0,0 +1,47 @@ +#!/usr/bin/env node +// Restores data/ from a backup made by server/utils/backup.ts. +// +// node scripts/restore-backup.mjs backups/logigate-2026-10-20.ndjson.gz [target-dir] +// +// The target (default: data) is moved aside to data.before-restore-