Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
Verified, Restorable PostgreSQL Backups — Implementation Plan
For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: Make PortOS's PostgreSQL backups verified, loud-on-failure, restorable, and visible in the UI — so the Postgres data PortOS already stores (memory + catalog) is genuinely recoverable.
Architecture: Extend the existing rsync-based backup.js service. Replace dumpPostgres's boolean return with an explicit ok/skipped/failed status (verified by file size + table count); propagate failed into a new degraded backup state that emits an error-notification toast; add a dry-run-default restorePostgres counterpart with a route and a Zod schema; surface DB-backup status and a Restore-DB action in BackupTab. Backup-mechanism only — no data/ format change, no data migration, Postgres stays optional (skipped is benign).
Tech Stack: Node.js (ESM), Express, Zod validation, Vitest (server), React/Vite + Tailwind (client), Socket.IO error pipeline, pg_dump/psql via child_process.spawn.
Spec: docs/superpowers/specs/2026-06-05-verified-pg-backup-design.md
File Structure
- Modify
server/services/backup.js—dumpPostgres(status return + verify),runBackup(degraded state + notify + manifest), newrestorePostgres. - Modify
server/services/socket.js— export agetIo()accessor forioInstance. - Modify
server/lib/validation.js— addrestoreDbRequestSchema. - Modify
server/routes/backup.js— addPOST /api/backup/restore-db. - Modify
client/src/services/apiSystem.js— addrestoreDatabasewrapper. - Modify
client/src/components/settings/BackupTab.jsx— DB status block, degraded banner, per-snapshot Restore-DB action + confirm modal. - Modify
server/services/backup.test.js— tests for all status branches (create if absent). - Append
.changelog/NEXT.md— changelog entry.
Task 1: dumpPostgres returns explicit status
Files:
Modify:
server/services/backup.js:207-256(thedumpPostgresfunction)Test:
server/services/backup.test.jsStep 1: Write the failing tests
Add to server/services/backup.test.js (create the file with this header if it does not exist):
import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock the DB health check and child_process.spawn before importing backup.js
vi.mock('../lib/db.js', () => ({
checkHealth: vi.fn(),
}));
import { EventEmitter } from 'events';
import { spawn } from 'child_process';
vi.mock('child_process', () => ({ spawn: vi.fn() }));
import { checkHealth } from '../lib/db.js';
import * as fs from 'fs/promises';
// Helper: build a fake child process whose close/error we can drive.
function fakeProc() {
const proc = new EventEmitter();
proc.stderr = new EventEmitter();
return proc;
}
describe('dumpPostgres status classification', () => {
let dumpPostgres;
beforeEach(async () => {
vi.clearAllMocks();
({ dumpPostgres } = await import('./backup.js'));
});
it('returns skipped/not_configured when PG is not connected', async () => {
checkHealth.mockResolvedValue({ connected: false, hasSchema: false });
const result = await dumpPostgres('/tmp/x.sql');
expect(result).toEqual({ status: 'skipped', reason: 'not_configured' });
expect(spawn).not.toHaveBeenCalled();
});
it('returns skipped/not_configured when connected but no schema', async () => {
checkHealth.mockResolvedValue({ connected: true, hasSchema: false });
const result = await dumpPostgres('/tmp/x.sql');
expect(result.status).toBe('skipped');
});
it('returns failed/pg_dump_missing when spawn errors', async () => {
checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
const proc = fakeProc();
spawn.mockReturnValue(proc);
const p = dumpPostgres('/tmp/x.sql');
proc.emit('error', new Error('spawn pg_dump ENOENT'));
const result = await p;
expect(result.status).toBe('failed');
expect(result.reason).toBe('pg_dump_missing');
});
it('returns failed/dump_error on non-zero exit', async () => {
checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
const proc = fakeProc();
spawn.mockReturnValue(proc);
const p = dumpPostgres('/tmp/x.sql');
proc.stderr.emit('data', Buffer.from('FATAL: auth failed'));
proc.emit('close', 1);
const result = await p;
expect(result.status).toBe('failed');
expect(result.reason).toBe('dump_error');
expect(result.error).toContain('auth failed');
});
it('returns failed/empty_dump when exit 0 but file is 0 bytes', async () => {
checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
vi.spyOn(fs, 'stat').mockResolvedValue({ size: 0 });
vi.spyOn(fs, 'readFile').mockResolvedValue('');
const proc = fakeProc();
spawn.mockReturnValue(proc);
const p = dumpPostgres('/tmp/x.sql');
proc.emit('close', 0);
const result = await p;
expect(result.status).toBe('failed');
expect(result.reason).toBe('empty_dump');
});
it('returns ok with sizeBytes and tableCount on a good dump', async () => {
checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
vi.spyOn(fs, 'stat').mockResolvedValue({ size: 2048 });
vi.spyOn(fs, 'readFile').mockResolvedValue(
'CREATE TABLE memories (...);\nCREATE TABLE memory_links (...);\n'
);
const proc = fakeProc();
spawn.mockReturnValue(proc);
const p = dumpPostgres('/tmp/x.sql');
proc.emit('close', 0);
const result = await p;
expect(result.status).toBe('ok');
expect(result.sizeBytes).toBe(2048);
expect(result.tableCount).toBe(2);
});
});
- Step 2: Run the tests to verify they fail
Run: cd server && npx vitest run services/backup.test.js -t "dumpPostgres status"
Expected: FAIL — current dumpPostgres returns { success }, not { status }.
- Step 3: Rewrite
dumpPostgres
Replace server/services/backup.js:207-256 with:
/**
* Run pg_dump to create a PostgreSQL backup alongside the rsync snapshot.
* Returns an explicit status so the caller can distinguish "no PG configured"
* (benign, file mode) from "PG configured but dump failed" (data at risk):
* { status: 'ok', sizeBytes, tableCount, path }
* { status: 'skipped', reason: 'not_configured' }
* { status: 'failed', reason: 'pg_dump_missing'|'dump_error'|'empty_dump', error }
* @param {string} outputPath - Path to write the SQL dump file
*/
export async function dumpPostgres(outputPath) {
const health = await checkHealth();
if (!health.connected || !health.hasSchema) {
return { status: 'skipped', reason: 'not_configured' };
}
const pgHost = process.env.PGHOST || 'localhost';
const pgPort = process.env.PGPORT || '5432';
const pgDb = process.env.PGDATABASE || 'portos';
const pgUser = process.env.PGUSER || 'portos';
if (!process.env.PGPASSWORD) {
console.warn('⚠️ PGPASSWORD not set for pg_dump — using default');
}
return new Promise((resolve) => {
const proc = spawn('pg_dump', [
'-h', pgHost,
'-p', pgPort,
'-U', pgUser,
'-d', pgDb,
'--no-owner',
'--no-acl',
'-f', outputPath
], {
shell: false,
env: { ...process.env, PGPASSWORD: process.env.PGPASSWORD || 'portos' }
});
let stderr = '';
proc.stderr.on('data', (chunk) => { stderr += chunk.toString(); });
proc.on('close', async (code) => {
if (code !== 0) {
console.warn(`⚠️ pg_dump failed (code ${code}): ${stderr.trim()}`);
resolve({ status: 'failed', reason: 'dump_error', error: stderr.trim() });
return;
}
// Verify: a dump that exits 0 but is empty/truncated is still a failure.
const info = await stat(outputPath).catch(() => null);
if (!info || info.size === 0) {
console.warn('⚠️ pg_dump produced an empty dump file');
resolve({ status: 'failed', reason: 'empty_dump', error: 'dump file missing or 0 bytes' });
return;
}
const sql = await readFile(outputPath, 'utf-8').catch(() => '');
const tableCount = (sql.match(/^CREATE TABLE /gm) || []).length;
console.log(`💾 pg_dump complete: ${Math.round(info.size / 1024)}KB, ${tableCount} tables`);
resolve({ status: 'ok', sizeBytes: info.size, tableCount, path: outputPath });
});
proc.on('error', (err) => {
// pg_dump not installed — a configured-but-unbacked-up DB is at risk,
// so this is a failure, not a silent skip.
console.warn(`⚠️ pg_dump not available: ${err.message}`);
resolve({ status: 'failed', reason: 'pg_dump_missing', error: err.message });
});
});
}
- Step 4: Add
readFileto the imports
Modify server/services/backup.js:10:
import { access, readdir, readFile, stat, writeFile } from 'fs/promises';
- Step 5: Run the tests to verify they pass
Run: cd server && npx vitest run services/backup.test.js -t "dumpPostgres status"
Expected: PASS (6 tests).
- Step 6: Commit
git add server/services/backup.js server/services/backup.test.js
git commit -m "feat(backup): classify pg_dump outcome as ok/skipped/failed with verification"
Task 2: Export getIo() from socket.js
Files:
- Modify:
server/services/socket.js(near theioInstancedeclaration at line 65)
This is a one-line accessor needed by Task 3 so unattended (scheduled) backups — which call runBackup with io = null — can still emit the degraded toast. No test of its own; exercised via Task 3.
- Step 1: Add the accessor
After the let ioInstance = null; line (server/services/socket.js:65), add:
/**
* Return the module-level Socket.IO instance (null before initSocket runs).
* Lets services emit to clients from unattended paths (cron handlers) that
* don't receive an `io` argument.
*/
export function getIo() {
return ioInstance;
}
- Step 2: Verify it parses
Run: cd server && node --check services/socket.js
Expected: no output (exit 0).
- Step 3: Commit
git add server/services/socket.js
git commit -m "feat(socket): export getIo() accessor for unattended emit paths"
Task 3: runBackup sets degraded state, persists pgBackup, notifies, hashes dump
Files:
Modify:
server/services/backup.js— imports (top),runBackup(lines ~136-198),generateManifest(lines ~263-283)Test:
server/services/backup.test.jsStep 1: Write the failing tests
Add this describe block to server/services/backup.test.js:
describe('runBackup pg status propagation', () => {
// These tests mock the internal helpers runBackup depends on. We re-import
// backup.js and stub runRsync via the rsync module boundary. Because runBackup
// calls many fs helpers, we focus on the status-mapping contract by spying on
// saveState and dumpPostgres through the module.
let backup;
beforeEach(async () => {
vi.clearAllMocks();
vi.resetModules();
});
it('maps dumpPostgres failed -> overall status degraded', () => {
// Pure mapping helper (extracted in Step 3) is the unit under test.
const { backupStatusForPg } = require('./backup.js');
expect(backupStatusForPg({ status: 'failed', reason: 'dump_error' })).toBe('degraded');
});
it('maps dumpPostgres skipped -> overall status ok', () => {
const { backupStatusForPg } = require('./backup.js');
expect(backupStatusForPg({ status: 'skipped', reason: 'not_configured' })).toBe('ok');
});
it('maps dumpPostgres ok -> overall status ok', () => {
const { backupStatusForPg } = require('./backup.js');
expect(backupStatusForPg({ status: 'ok', sizeBytes: 10, tableCount: 1 })).toBe('ok');
});
});
Note:
requireinside an ESM Vitest file works because Vitest provides a CJS-interoprequire. If the project's Vitest config rejectsrequire, replace each withconst { backupStatusForPg } = await import('./backup.js');and make theitcallbacksasync.
- Step 2: Run to verify failure
Run: cd server && npx vitest run services/backup.test.js -t "pg status propagation"
Expected: FAIL — backupStatusForPg is not exported.
- Step 3: Add the pure mapping helper + use it in
runBackup
In server/services/backup.js, add this exported helper just above runBackup (after the DEFAULT_STATE block, ~line 60):
/**
* Map a dumpPostgres result to the overall backup status. Only a *failed*
* dump (PG configured but the dump errored) degrades the backup; a *skipped*
* dump (no PG — file mode) is benign and stays 'ok'.
* @param {{status: string}} pgResult
* @returns {'ok'|'degraded'}
*/
export function backupStatusForPg(pgResult) {
return pgResult?.status === 'failed' ? 'degraded' : 'ok';
}
- Step 4: Update the imports
Modify server/services/backup.js:13-15 to add the error-emit helpers and the io accessor:
import { PATHS, ensureDir, readJSONFile, sha256File } from '../lib/fileUtils.js';
import { getEvent } from './eventScheduler.js';
import { checkHealth } from '../lib/db.js';
import { emitErrorEvent, ServerError } from '../lib/errorHandler.js';
import { getIo } from './socket.js';
- Step 5: Rewrite the pg-dump + saveState section of
runBackup
Replace server/services/backup.js:181-198 (from the // Dump PostgreSQL alongside comment through the return complete({...})) with:
// Dump PostgreSQL alongside the file backup. Result is NO LONGER swallowed —
// a configured-but-failed dump must degrade the backup and alert the user.
const pgDumpPath = join(snapshotDir, 'portos-db.sql');
const pgResult = await dumpPostgres(pgDumpPath);
manifest = await generateManifest(dataDestDir, join(snapshotDir, 'manifest.json'), pgDumpPath).catch(fail);
const status = backupStatusForPg(pgResult);
const lastRun = new Date().toISOString();
await saveState({
lastRun,
lastSnapshotId: snapshotId,
status,
filesChanged: changedFiles.length,
pgBackup: pgResult,
error: pgResult.status === 'failed' ? `DB dump ${pgResult.reason}` : null
}).catch(fail);
if (io) io.emit('backup:completed', { snapshotId, filesChanged: changedFiles.length, status, pgBackup: pgResult });
// Loud-on-failure: surface a degraded DB dump as a warning toast, even on
// unattended scheduled runs (which pass io=null) via the module-level io.
if (pgResult.status === 'failed') {
const errIo = io || getIo();
if (errIo) {
emitErrorEvent(errIo, new ServerError(
`Backup DB dump failed: ${pgResult.reason}`,
{ status: 500, code: 'BACKUP_DB_DUMP_FAILED', severity: 'warning' }
));
}
}
return complete({ snapshotId, filesChanged: changedFiles.length, status, lastRun, manifest, pgBackup: pgResult });
Note: this removes the old standalone
if (io) io.emit('backup:completed', ...)line that was at 196 — it is folded into the block above. Verify there is no leftover duplicate emit after this edit.
- Step 6: Extend
generateManifestto hash the dump
The dump lives one level above the data/ dir generateManifest walks, so hash it explicitly. Replace the signature and add the dump entry in server/services/backup.js:263-283:
/**
* Generate a SHA-256 manifest for all files in snapshotDataDir, plus the
* sibling pg dump (which lives outside the data/ tree). Hashing the dump means
* a truncated/corrupt portos-db.sql is detectable, not silently trusted.
* @param {string} snapshotDataDir - Directory to hash
* @param {string} manifestPath - Path to write manifest.json
* @param {string|null} [pgDumpPath=null] - Sibling SQL dump to also hash
*/
export async function generateManifest(snapshotDataDir, manifestPath, pgDumpPath = null) {
const entries = await readdir(snapshotDataDir, { recursive: true }).catch(() => []);
const files = {};
for (const entry of entries) {
const filePath = join(snapshotDataDir, entry);
const info = await stat(filePath).catch(() => null);
if (!info || !info.isFile()) continue;
files[entry] = await sha256File(filePath);
}
if (pgDumpPath) {
const dumpInfo = await stat(pgDumpPath).catch(() => null);
if (dumpInfo?.isFile()) {
files['../portos-db.sql'] = await sha256File(pgDumpPath);
}
}
const manifest = {
generatedAt: new Date().toISOString(),
fileCount: Object.keys(files).length,
files
};
await writeFile(manifestPath, JSON.stringify(manifest, null, 2), 'utf-8');
console.log(`💾 Backup manifest: ${manifest.fileCount} files`);
return manifest;
}
- Step 7: Run the tests to verify they pass
Run: cd server && npx vitest run services/backup.test.js -t "pg status propagation"
Expected: PASS (3 tests).
- Step 8: Run the full backup test file (no regressions)
Run: cd server && npx vitest run services/backup.test.js
Expected: PASS (all tasks' tests).
- Step 9: Commit
git add server/services/backup.js server/services/backup.test.js
git commit -m "feat(backup): degraded status + pgBackup state + warning toast + dump hashing"
Task 4: restorePostgres with dry-run default
Files:
Modify:
server/services/backup.js— addrestorePostgresafterrestoreSnapshot(~line 347)Test:
server/services/backup.test.jsStep 1: Write the failing tests
Add to server/services/backup.test.js:
describe('restorePostgres', () => {
let restorePostgres;
beforeEach(async () => {
vi.clearAllMocks();
({ restorePostgres } = await import('./backup.js'));
});
it('rejects a path-traversal snapshotId', async () => {
await expect(restorePostgres('/dest', '../../etc', { dryRun: true }))
.rejects.toThrow(/Invalid snapshotId/);
});
it('returns skipped/no_dump when the sql file is absent', async () => {
vi.spyOn(fs, 'stat').mockRejectedValue(new Error('ENOENT'));
const result = await restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: true });
expect(result).toEqual({ status: 'skipped', reason: 'no_dump' });
});
it('dry-run reports size/tableCount without spawning psql', async () => {
vi.spyOn(fs, 'stat').mockResolvedValue({ size: 4096, isFile: () => true });
vi.spyOn(fs, 'readFile').mockResolvedValue('CREATE TABLE a (...);\n');
const result = await restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: true });
expect(result.status).toBe('ok');
expect(result.dryRun).toBe(true);
expect(result.sizeBytes).toBe(4096);
expect(result.tableCount).toBe(1);
expect(spawn).not.toHaveBeenCalled();
});
it('refuses a real restore when PG is not connected', async () => {
vi.spyOn(fs, 'stat').mockResolvedValue({ size: 4096, isFile: () => true });
checkHealth.mockResolvedValue({ connected: false, hasSchema: false });
const result = await restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
expect(result).toEqual({ status: 'skipped', reason: 'not_configured' });
expect(spawn).not.toHaveBeenCalled();
});
});
- Step 2: Run to verify failure
Run: cd server && npx vitest run services/backup.test.js -t "restorePostgres"
Expected: FAIL — restorePostgres not defined.
- Step 3: Implement
restorePostgres
Add after restoreSnapshot (after server/services/backup.js:347):
/**
* Restore the PostgreSQL dump from a snapshot. Dry-run by default — mirrors
* restoreSnapshot's safety default. A real restore pipes the snapshot's
* portos-db.sql into psql; the dump was written with --no-owner --no-acl so
* it replays cleanly.
* { status: 'ok', dryRun, sizeBytes, tableCount } (dry-run or applied)
* { status: 'skipped', reason: 'no_dump' } (no sql file in snapshot)
* { status: 'skipped', reason: 'not_configured' } (real restore, PG unreachable)
* { status: 'failed', reason: 'restore_error', error }
* @param {string} destPath - Backup destination root
* @param {string} snapshotId
* @param {{dryRun?: boolean}} [options]
*/
export async function restorePostgres(destPath, snapshotId, { dryRun = true } = {}) {
if (!snapshotId || !/^[\w\-.:T]+$/.test(snapshotId)) {
throw new Error(`Invalid snapshotId: ${snapshotId}`);
}
const snapshotsRoot = resolve(join(destPath, 'snapshots', MACHINE_HOST));
const sqlPath = join(snapshotsRoot, snapshotId, 'portos-db.sql');
const rel = relative(snapshotsRoot, resolve(sqlPath));
if (!rel || rel.startsWith('..') || isAbsolute(rel)) {
throw new Error(`Path traversal detected for snapshotId: ${snapshotId}`);
}
const info = await stat(sqlPath).catch(() => null);
if (!info || !info.isFile?.()) {
return { status: 'skipped', reason: 'no_dump' };
}
const sql = await readFile(sqlPath, 'utf-8').catch(() => '');
const tableCount = (sql.match(/^CREATE TABLE /gm) || []).length;
if (dryRun) {
return { status: 'ok', dryRun: true, sizeBytes: info.size, tableCount };
}
// Never half-restore: require a reachable DB before replaying.
const health = await checkHealth();
if (!health.connected) {
return { status: 'skipped', reason: 'not_configured' };
}
const pgHost = process.env.PGHOST || 'localhost';
const pgPort = process.env.PGPORT || '5432';
const pgDb = process.env.PGDATABASE || 'portos';
const pgUser = process.env.PGUSER || 'portos';
return new Promise((resolveP) => {
const proc = spawn('psql', [
'-h', pgHost, '-p', pgPort, '-U', pgUser, '-d', pgDb, '-f', sqlPath
], { shell: false, env: { ...process.env, PGPASSWORD: process.env.PGPASSWORD || 'portos' } });
let stderr = '';
proc.stderr.on('data', (chunk) => { stderr += chunk.toString(); });
proc.on('close', (code) => {
if (code === 0) {
console.log(`💾 psql restore complete from snapshot ${snapshotId}: ${tableCount} tables`);
resolveP({ status: 'ok', dryRun: false, sizeBytes: info.size, tableCount });
} else {
console.warn(`⚠️ psql restore failed (code ${code}): ${stderr.trim()}`);
resolveP({ status: 'failed', reason: 'restore_error', error: stderr.trim() });
}
});
proc.on('error', (err) => {
console.warn(`⚠️ psql not available: ${err.message}`);
resolveP({ status: 'failed', reason: 'restore_error', error: err.message });
});
});
}
- Step 4: Run to verify pass
Run: cd server && npx vitest run services/backup.test.js -t "restorePostgres"
Expected: PASS (4 tests).
- Step 5: Commit
git add server/services/backup.js server/services/backup.test.js
git commit -m "feat(backup): add restorePostgres with dry-run default and traversal guard"
Task 5: Route + Zod schema for DB restore
Files:
Modify:
server/lib/validation.js:792-796(afterrestoreRequestSchema)Modify:
server/routes/backup.jsTest:
server/routes/backup.test.js(create if absent)Step 1: Add the Zod schema
After restoreRequestSchema in server/lib/validation.js (after line 796), add:
export const restoreDbRequestSchema = z.object({
snapshotId: z.string().min(1),
dryRun: z.boolean().optional().default(true)
});
- Step 2: Write the failing route test
Create/append server/routes/backup.test.js:
import { describe, it, expect, vi, beforeEach } from 'vitest';
import express from 'express';
import request from 'supertest';
vi.mock('../services/backup.js', () => ({
getState: vi.fn().mockResolvedValue({ status: 'ok' }),
getNextRunTime: vi.fn().mockReturnValue(null),
DEFAULT_EXCLUDES: [],
runBackup: vi.fn(),
listSnapshots: vi.fn().mockResolvedValue([]),
restoreSnapshot: vi.fn(),
restorePostgres: vi.fn().mockResolvedValue({ status: 'ok', dryRun: true, sizeBytes: 10, tableCount: 1 }),
}));
vi.mock('../services/settings.js', () => ({
getSettings: vi.fn().mockResolvedValue({ backup: { destPath: '/dest' } }),
}));
import backupRouter from './backup.js';
import * as backup from '../services/backup.js';
function makeApp() {
const app = express();
app.use(express.json());
app.use('/api/backup', backupRouter);
// minimal error middleware so thrown ServerErrors become JSON
app.use((err, req, res, next) => res.status(err.status || 500).json({ error: err.message }));
return app;
}
describe('POST /api/backup/restore-db', () => {
beforeEach(() => vi.clearAllMocks());
it('calls restorePostgres with the validated body (dryRun default true)', async () => {
const app = makeApp();
const res = await request(app).post('/api/backup/restore-db').send({ snapshotId: '2026-06-05T00-00-00' });
expect(res.status).toBe(200);
expect(backup.restorePostgres).toHaveBeenCalledWith('/dest', '2026-06-05T00-00-00', { dryRun: true });
});
it('400s when snapshotId is missing', async () => {
const app = makeApp();
const res = await request(app).post('/api/backup/restore-db').send({});
expect(res.status).toBe(400);
});
});
- Step 3: Run to verify failure
Run: cd server && npx vitest run routes/backup.test.js
Expected: FAIL — route not defined (404).
- Step 4: Add the route
In server/routes/backup.js, update the validation import and add the route before export default router;:
import { validateRequest, restoreRequestSchema, restoreDbRequestSchema } from '../lib/validation.js';
// POST /api/backup/restore-db
router.post('/restore-db', asyncHandler(async (req, res) => {
const { snapshotId, dryRun } = validateRequest(restoreDbRequestSchema, req.body);
const settings = await getSettings();
const destPath = settings.backup?.destPath;
if (!destPath) {
throw new ServerError('No backup destination configured in settings', { status: 400, code: 'BACKUP_NOT_CONFIGURED' });
}
const result = await backup.restorePostgres(destPath, snapshotId, { dryRun });
res.json(result);
}));
- Step 5: Run to verify pass
Run: cd server && npx vitest run routes/backup.test.js
Expected: PASS (2 tests).
- Step 6: Commit
git add server/lib/validation.js server/routes/backup.js server/routes/backup.test.js
git commit -m "feat(backup): POST /api/backup/restore-db route + restoreDbRequestSchema"
Task 6: Client API wrapper
Files:
Modify:
client/src/services/apiSystem.js:97(afterrestoreBackup)Step 1: Add the wrapper
After the restoreBackup line in client/src/services/apiSystem.js, add:
export const restoreDatabase = (data, options) => request('/backup/restore-db', { method: 'POST', body: JSON.stringify(data), ...options });
restoreDatabaseaccepts{ snapshotId, dryRun }. The UI (Task 7) owns its own error toast, so it passes{ silent: true }asoptions.
- Step 2: Verify the barrel re-exports it
api.js re-exports apiSystem.js wholesale. Confirm:
Run: cd client && grep -n "apiSystem" src/services/api.js
Expected: a line like export * from './apiSystem.js'; (the existing backup wrappers are imported the same way).
- Step 3: Commit
git add client/src/services/apiSystem.js
git commit -m "feat(backup): add restoreDatabase client API wrapper"
Task 7: BackupTab UI — DB status, degraded banner, Restore-DB
Files:
- Modify:
client/src/components/settings/BackupTab.jsx
This task has no unit test (presentational); it's verified by the manual smoke test in Task 8.
- Step 1: Extend status load + imports
In client/src/components/settings/BackupTab.jsx:
Update the API import (line 8) to add the new wrappers and snapshots fetch:
import { getSettings, updateSettings, getBackupStatus, triggerBackup, getBackupSnapshots, restoreDatabase } from '../../services/api';
Add state for the last pg result, snapshots, and restore UI (after line 52):
const [pgBackup, setPgBackup] = useState(null);
const [backupStatus, setBackupStatus] = useState('never');
const [snapshots, setSnapshots] = useState([]);
const [restoreTarget, setRestoreTarget] = useState(null); // snapshotId pending confirm
const [restorePreview, setRestorePreview] = useState(null); // dry-run result
In the initial useEffect (line 55-73), capture the new status fields and load snapshots:
useEffect(() => {
Promise.all([
getSettings(),
getBackupStatus({ silent: true }).catch(() => null),
getBackupSnapshots({ silent: true }).catch(() => []),
])
.then(([settings, status, snaps]) => {
const backup = settings?.backup || {};
const saved = backup.destPath || '';
const savedExcludes = asArray(backup.excludePaths);
const savedDisabled = asArray(backup.disabledDefaultExcludes);
setDestPath(saved);
setSavedDestPath(saved);
setEnabled(backup.enabled ?? false);
setCronExpression(backup.cronExpression || '0 2 * * *');
setExcludePaths(savedExcludes);
setSavedExcludePaths(savedExcludes);
setDisabledDefaultExcludes(savedDisabled);
setSavedDisabledDefaultExcludes(savedDisabled);
setDefaultExcludes(asArray(status?.defaultExcludes));
setPgBackup(status?.pgBackup ?? null);
setBackupStatus(status?.status ?? 'never');
setSnapshots(Array.isArray(snaps) ? snaps : []);
})
.catch(() => toast.error('Failed to load settings'))
.finally(() => setLoading(false));
}, []);
- Step 2: Capture status after a manual run
Replace the handleRunNow block (lines 105-113) so it records the new status fields:
const [handleRunNow, running] = useAsyncAction(async () => {
const result = await triggerBackup({ silent: true });
if (result?.skipped) {
toast('Backup already running');
} else {
setPgBackup(result?.pgBackup ?? null);
setBackupStatus(result?.status ?? 'ok');
const dbNote = result?.pgBackup?.status === 'failed' ? ' (DB dump FAILED)' : '';
toast.success(`Backup complete — ${result?.filesChanged ?? 0} files changed${dbNote}`, { icon: '💾' });
getBackupSnapshots({ silent: true }).then(s => setSnapshots(Array.isArray(s) ? s : [])).catch(() => {});
}
return result;
}, { errorMessage: 'Backup failed' });
- Step 3: Add a DB-status renderer helper (above the
return)
Add before the return ( (after line 148):
const renderPgStatus = () => {
if (!pgBackup) return <span className="text-gray-500">No backup run yet</span>;
if (pgBackup.status === 'ok') {
return <span className="text-port-success">✅ {Math.round((pgBackup.sizeBytes || 0) / 1024)} KB · {pgBackup.tableCount} tables</span>;
}
if (pgBackup.status === 'skipped') {
return <span className="text-gray-400">⏭️ Not configured (file mode)</span>;
}
return <span className="text-port-warning">❌ Dump failed: {pgBackup.reason}</span>;
};
const handleRestoreDb = async (snapshotId) => {
// Dry-run first to show what would restore, then open the confirm modal.
const preview = await restoreDatabase({ snapshotId, dryRun: true }, { silent: true })
.catch(() => null);
if (!preview || preview.status === 'skipped') {
toast.error(preview?.reason === 'no_dump' ? 'No DB dump in this snapshot' : 'DB restore unavailable');
return;
}
setRestorePreview(preview);
setRestoreTarget(snapshotId);
};
const confirmRestoreDb = async () => {
const snapshotId = restoreTarget;
setRestoreTarget(null);
const result = await restoreDatabase({ snapshotId, dryRun: false }, { silent: true })
.catch(() => ({ status: 'failed', reason: 'request_error' }));
if (result.status === 'ok') {
toast.success(`Database restored from ${snapshotId}`, { icon: '💾' });
} else {
toast.error(`DB restore failed: ${result.reason || 'unknown'}`);
}
setRestorePreview(null);
};
- Step 4: Render the DB status block + degraded banner + snapshots/restore + modal
Insert this block immediately inside the outer <div ...> of the return, right after the opening div (after line 151), so it sits at the top of the tab:
{backupStatus === 'degraded' && (
<div className="bg-port-warning/10 border border-port-warning/40 rounded-lg px-3 py-2 text-sm text-port-warning">
⚠️ Last backup degraded — files were saved but the database dump failed. Check that <code>pg_dump</code> is installed and PostgreSQL is reachable.
</div>
)}
<div className="space-y-1">
<label className="block text-sm text-gray-400">Database Backup (last run)</label>
<div className="text-sm">{renderPgStatus()}</div>
</div>
Then add a snapshots + restore section just before the closing action-buttons <div className="flex flex-wrap items-center gap-2 pt-2 ..."> (before line 272):
{snapshots.length > 0 && (
<div className="space-y-2">
<label className="block text-sm text-gray-400">Snapshots</label>
<ul className="space-y-1.5">
{snapshots.slice(0, 10).map((snap) => (
<li key={snap.id} className="flex items-center justify-between gap-2 text-xs bg-port-bg border border-port-border rounded-lg px-2.5 py-1.5">
<span className="text-gray-300 truncate">{snap.id}</span>
<button
onClick={() => handleRestoreDb(snap.id)}
className="shrink-0 px-2 py-1 bg-port-border hover:bg-port-border/70 text-white rounded transition-colors"
>
Restore DB
</button>
</li>
))}
</ul>
</div>
)}
{restoreTarget && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60" role="dialog" aria-modal="true">
<div className="bg-port-card border border-port-border rounded-xl p-5 max-w-md w-full mx-4 space-y-4">
<h3 className="text-white text-sm font-medium">Restore database?</h3>
<p className="text-sm text-gray-400">
This replays <code>portos-db.sql</code> from snapshot <code className="text-gray-300">{restoreTarget}</code>
{restorePreview && <> ({Math.round((restorePreview.sizeBytes || 0) / 1024)} KB · {restorePreview.tableCount} tables)</>}
{' '}into the live PostgreSQL database. Existing rows may be overwritten.
</p>
<div className="flex justify-end gap-2">
<button onClick={() => { setRestoreTarget(null); setRestorePreview(null); }} className="px-3 py-2 text-sm text-gray-400 hover:text-white transition-colors">Cancel</button>
<button onClick={confirmRestoreDb} className="px-3 py-2 text-sm bg-port-warning hover:bg-port-warning/80 text-black font-medium rounded-lg transition-colors">Restore</button>
</div>
</div>
</div>
)}
- Step 5: Build the client to verify it compiles
Run: cd client && npm run build
Expected: build succeeds (no JSX/syntax errors).
- Step 6: Commit
git add client/src/components/settings/BackupTab.jsx
git commit -m "feat(backup): surface DB backup status, degraded banner, and Restore DB in BackupTab"
Task 8: Changelog + full verification
Files:
Append:
.changelog/NEXT.mdStep 1: Append the changelog entry
Append to .changelog/NEXT.md (match the file's existing heading style — check the top of the file first):
### Added
- Verified, restorable PostgreSQL backups: `pg_dump` outcomes are now classified
(ok / skipped / failed) and verified by size + table count; a configured-but-failed
dump degrades the backup and raises a warning toast (even on scheduled runs); the
dump is hashed into the snapshot manifest; a new dry-run-default DB restore
(`POST /api/backup/restore-db`) and a "Restore DB" action with confirmation modal
are available in the Backup settings tab.
- Step 2: Run the full server test suite
Run: cd server && npm test
Expected: PASS (including services/backup.test.js and routes/backup.test.js).
- Step 3: Run the client test suite
Run: cd client && npm test
Expected: PASS (no regressions).
- Step 4: Commit
git add .changelog/NEXT.md
git commit -m "docs(changelog): verified restorable Postgres backups"
- Step 5: Run /simplify on the changed code
Per the project workflow, run /simplify to review the changed code for reuse/quality before opening a PR. Address any safe simplifications and re-run the relevant tests.
Deferred follow-ups (from review)
Verify the DB dump against its manifest hash before restore (codex P2, declined for this PR).
restorePostgrestrusts a non-emptyportos-db.sqlon size alone; the manifest now records../portos-db.sql's sha256, so a restore could re-hash and compare before replaying. Declined now because it's outside PortOS's stated threat model (single-user, Tailscale-private, never public — so post-write tampering isn't a defended threat), and the truncation case it guards is already largely covered: the empty-dump guard rejects 0-byte files and the--single-transactionrestore rolls back a syntactically broken dump. The residual risk is a dump truncated at a clean statement boundary — narrow. Add the hash check if PortOS ever relaxes the private-network assumption, or if a real truncated-restore incident occurs.server/services/backup.jsrestorePostgres~line 444.Integration test for
runBackuppg-status wiring. Task 3 covered only the purebackupStatusForPghelper (the plan scoped outrunBackupintegration testing because it touches many fs helpers). The wiring —saveStatewritingdegraded+pgBackup, thebackup:completedemit carrying the new fields, theemitErrorEventwarning fire on failure, andgenerateManifest'spgDumpPathbranch hashing intofiles['../portos-db.sql']— is verified by code-reading only.generateManifest's new branch in particular is cheap to test in isolation (mostly fs, already mocked in the suite) and would lock in the parent-relative key contract. Promote to aplanissue if this path regresses.
Self-Review Notes (plan author)
- Spec coverage: §1 dump classification → Task 1. §2 state/manifest/propagation → Task 3 (+ getIo in Task 2). §3 degraded notification → Task 3. §4 restorePostgres → Task 4. §5 route+schema → Task 5. §6 UI → Task 7 (+ Task 6 wrapper). Testing → Tasks 1,3,4,5. Changelog → Task 8. All sections mapped.
- Type consistency: status strings
ok/skipped/failed(dump) andok/degraded(overall) used identically across service, route, tests, and UI.pgBackupfield name consistent across saveState, /status spread (verifiedsaveStatemerges),backup:completedemit, and UI.restorePostgres/restoreDatabasenames consistent server↔client. - Verified assumptions:
saveStatedoes{...current, ...patch}sopgBackupflows throughGET /status(backup.js:360-367).getIo()does not pre-exist (added in Task 2).apiSystem.jsholds backup wrappers and is barrel-exported viaapi.js.restoreSnapshot's traversal guard regex reused verbatim inrestorePostgres.