Skip to content

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

feat(backup): POST /api/backup/restore-db route + restoreDbRequestSchema

Adam Eivy4b0479201d51
diff --git a/server/lib/validation.js b/server/lib/validation.js
index 935e1fdc37..f2585e51f1 100644
--- a/server/lib/validation.js
+++ b/server/lib/validation.js
@@ -795,6 +795,11 @@ export const restoreRequestSchema = z.object({
dryRun: z.boolean().optional().default(true)
});
+export const restoreDbRequestSchema = z.object({
+ snapshotId: z.string().min(1),
+ dryRun: z.boolean().optional().default(true)
+});
+
// CyberCity snapshot pipeline (issue #877): how often to capture a city-state
// frame and how many to retain. Validated as a settings slice on PUT /api/settings;
// service-side defaults (DEFAULT_SNAPSHOT_CONFIG) fill any absent field so an
diff --git a/server/routes/backup.js b/server/routes/backup.js
index b3101305c1..8afef7905f 100644
--- a/server/routes/backup.js
+++ b/server/routes/backup.js
@@ -1,6 +1,6 @@
import { Router } from 'express';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
-import { validateRequest, restoreRequestSchema } from '../lib/validation.js';
+import { validateRequest, restoreRequestSchema, restoreDbRequestSchema } from '../lib/validation.js';
import * as backup from '../services/backup.js';
import { getSettings } from '../services/settings.js';
@@ -48,4 +48,16 @@ router.post('/restore', asyncHandler(async (req, res) => {
res.json(result);
}));
+// 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);
+}));
+
export default router;
diff --git a/server/routes/backup.test.js b/server/routes/backup.test.js
index f564db4bd6..7ef2aac959 100644
--- a/server/routes/backup.test.js
+++ b/server/routes/backup.test.js
@@ -9,6 +9,7 @@ vi.mock('../services/backup.js', () => ({
runBackup: vi.fn(),
listSnapshots: vi.fn(),
restoreSnapshot: vi.fn(),
+ restorePostgres: vi.fn(),
DEFAULT_EXCLUDES: [
{ path: '/browser-profile/', reason: 'test', overridable: false },
{ path: '/loras/*.safetensors', reason: 'test', overridable: true }
@@ -165,4 +166,52 @@ describe('backup routes', () => {
);
});
});
+
+ describe('POST /api/backup/restore-db', () => {
+ it('calls restorePostgres with the validated body (dryRun default true)', async () => {
+ getSettings.mockResolvedValue({ backup: { destPath: '/dest' } });
+ backup.restorePostgres.mockResolvedValue({ status: 'ok', dryRun: true, sizeBytes: 10, tableCount: 1 });
+ const res = await request(buildApp())
+ .post('/api/backup/restore-db')
+ .send({ snapshotId: '2026-06-05T00-00-00' });
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual({ status: 'ok', dryRun: true, sizeBytes: 10, tableCount: 1 });
+ expect(backup.restorePostgres).toHaveBeenCalledWith(
+ '/dest',
+ '2026-06-05T00-00-00',
+ { dryRun: true }
+ );
+ });
+
+ it('forwards dryRun=false when explicitly requested', async () => {
+ getSettings.mockResolvedValue({ backup: { destPath: '/dest' } });
+ backup.restorePostgres.mockResolvedValue({ status: 'ok', dryRun: false });
+ await request(buildApp())
+ .post('/api/backup/restore-db')
+ .send({ snapshotId: 'snap-9', dryRun: false });
+ expect(backup.restorePostgres).toHaveBeenCalledWith(
+ '/dest',
+ 'snap-9',
+ { dryRun: false }
+ );
+ });
+
+ it('returns 400 when snapshotId is missing', async () => {
+ getSettings.mockResolvedValue({ backup: { destPath: '/dest' } });
+ const res = await request(buildApp()).post('/api/backup/restore-db').send({});
+ expect(res.status).toBe(400);
+ expect(res.body.code).toBe('VALIDATION_ERROR');
+ expect(backup.restorePostgres).not.toHaveBeenCalled();
+ });
+
+ it('returns 400 when no destination is configured', async () => {
+ getSettings.mockResolvedValue({ backup: {} });
+ const res = await request(buildApp())
+ .post('/api/backup/restore-db')
+ .send({ snapshotId: 'snap-1' });
+ expect(res.status).toBe(400);
+ expect(res.body.code).toBe('BACKUP_NOT_CONFIGURED');
+ expect(backup.restorePostgres).not.toHaveBeenCalled();
+ });
+ });
});