Skip to content

Build your personal knowledge base with Trilium Notes

refactor(options/backup): delegate directory management to server

Elian Doran372daa8fdc2c
diff --git a/apps/client/src/widgets/type_widgets/options/backup.tsx b/apps/client/src/widgets/type_widgets/options/backup.tsx
index 87812d40d0..de493e9578 100644
--- a/apps/client/src/widgets/type_widgets/options/backup.tsx
+++ b/apps/client/src/widgets/type_widgets/options/backup.tsx
@@ -1,4 +1,4 @@
-import { BackupDatabaseNowResponse, DatabaseBackup } from "@triliumnext/commons";
+import { BackupDatabaseNowResponse, DatabaseBackup, ExistingBackupsResponse } from "@triliumnext/commons";
import { useCallback, useEffect, useState } from "preact/hooks";
import { t } from "../../../services/i18n";
@@ -14,26 +14,28 @@ import OptionsSection from "./components/OptionsSection";
export default function BackupSettings() {
const [backups, setBackups] = useState<DatabaseBackup[]>([]);
+ const [backupFolderPath, setBackupFolderPath] = useState<string | null>(null);
const refreshBackups = useCallback(() => {
- server.get<DatabaseBackup[]>("database/backups").then((backupFiles) => {
+ server.get<ExistingBackupsResponse>("database/backups").then((response) => {
// Sort the backup files by modification date & time in a desceding order
- backupFiles.sort((a, b) => {
+ const backupFiles = [...response.backups].sort((a, b) => {
if (a.mtime < b.mtime) return 1;
if (a.mtime > b.mtime) return -1;
return 0;
});
setBackups(backupFiles);
+ setBackupFolderPath(response.backupFolderPath);
});
- }, [setBackups]);
+ }, []);
useEffect(refreshBackups, []);
return (
<>
<BackupConfiguration refreshCallback={refreshBackups} />
- <BackupList backups={backups} />
+ <BackupList backups={backups} backupFolderPath={backupFolderPath} />
</>
);
}
@@ -84,16 +86,12 @@ export function BackupConfiguration({ refreshCallback }: { refreshCallback: () =
);
}
-export function BackupList({ backups }: { backups: DatabaseBackup[] }) {
- // All backups live in the same directory, so display it once instead of repeating it per file.
- const firstBackup = backups.at(0);
- const backupFolder = firstBackup?.filePath.slice(0, -(firstBackup.fileName.length + 1));
-
+export function BackupList({ backups, backupFolderPath }: { backups: DatabaseBackup[]; backupFolderPath: string | null }) {
return (
<OptionsSection
title={t("backup.existing_backups")}
- description={backupFolder && (
- <span className="selectable-text">{t("backup.backup_location_description", { backupFolder })}</span>
+ description={backupFolderPath && (
+ <span className="selectable-text">{t("backup.backup_location_description", { backupFolder: backupFolderPath })}</span>
)}
>
{backups.length > 0 ? (
diff --git a/apps/server/internal.openapi.yaml b/apps/server/internal.openapi.yaml
index d6105e63e5..e42536aedb 100644
--- a/apps/server/internal.openapi.yaml
+++ b/apps/server/internal.openapi.yaml
@@ -2774,9 +2774,24 @@ paths:
content:
application/json:
schema:
- type: array
- items:
- type: string
+ type: object
+ properties:
+ backupFolderPath:
+ type: string
+ nullable: true
+ description: Directory where the backups are stored, or null if there is no user-accessible location.
+ backups:
+ type: array
+ items:
+ type: object
+ properties:
+ fileName:
+ type: string
+ filePath:
+ type: string
+ mtime:
+ type: string
+ format: date-time
/api/database/vacuum-database:
post:
diff --git a/apps/server/src/backup_provider.ts b/apps/server/src/backup_provider.ts
index c2138c0222..be09d18db8 100644
--- a/apps/server/src/backup_provider.ts
+++ b/apps/server/src/backup_provider.ts
@@ -1,10 +1,9 @@
import type { DatabaseBackup } from "@triliumnext/commons";
-import { BackupOptionsService, BackupService, sync_mutex as syncMutexService } from "@triliumnext/core";
+import { BackupOptionsService, BackupService, getLog, sync_mutex as syncMutexService } from "@triliumnext/core";
import fs from "fs";
import path from "path";
import dataDir from "./services/data_dir.js";
-import { getLog } from "@triliumnext/core";
import sql from "./services/sql.js";
export default class ServerBackupService extends BackupService {
@@ -28,6 +27,10 @@ export default class ServerBackupService extends BackupService {
});
}
+ override getBackupFolderPath(): string {
+ return path.resolve(dataDir.BACKUP_DIR);
+ }
+
override scheduleBackups(): void {
// Run regular backups every 4 hours
setInterval(() => this.regularBackup(), 4 * 60 * 60 * 1000);
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 7d1be2f4af..8a1ccf7766 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -113,6 +113,12 @@ export interface DatabaseBackup {
mtime: Date;
}
+export interface ExistingBackupsResponse {
+ /** The directory where the backups are stored, or null if there is no user-accessible location (e.g. OPFS on standalone). */
+ backupFolderPath: string | null;
+ backups: DatabaseBackup[];
+}
+
export type ChangePasswordResponse = Response;
export interface TOTPStatus {
diff --git a/packages/trilium-core/src/routes/api/backup.spec.ts b/packages/trilium-core/src/routes/api/backup.spec.ts
index e7021bcb2c..95b185b0e1 100644
--- a/packages/trilium-core/src/routes/api/backup.spec.ts
+++ b/packages/trilium-core/src/routes/api/backup.spec.ts
@@ -1,3 +1,4 @@
+import type { ExistingBackupsResponse } from "@triliumnext/commons";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { getBackup } from "../../services/backup";
@@ -25,10 +26,11 @@ describe("Backup API (core)", () => {
vi.restoreAllMocks();
});
- it("lists existing backups as an array (real service)", async () => {
- const res = await api.get(`/api/database/backups`);
+ it("lists existing backups with their folder path (real service)", async () => {
+ const res = await api.get<ExistingBackupsResponse>(`/api/database/backups`);
expect(res.status).toBe(200);
- expect(Array.isArray(res.body)).toBe(true);
+ expect(Array.isArray(res.body.backups)).toBe(true);
+ expect(res.body).toHaveProperty("backupFolderPath");
});
it("creates a backup and returns the resulting file", async () => {
diff --git a/packages/trilium-core/src/routes/api/backup.ts b/packages/trilium-core/src/routes/api/backup.ts
index b9839c380a..e3dd5fe5b8 100644
--- a/packages/trilium-core/src/routes/api/backup.ts
+++ b/packages/trilium-core/src/routes/api/backup.ts
@@ -1,9 +1,13 @@
-import type { BackupDatabaseNowResponse, DatabaseBackup } from "@triliumnext/commons";
+import type { BackupDatabaseNowResponse, ExistingBackupsResponse } from "@triliumnext/commons";
import { getBackup } from "../../services/backup.js";
import { Request, Response } from "express";
-async function getExistingBackups(): Promise<DatabaseBackup[]> {
- return getBackup().getExistingBackups();
+async function getExistingBackups(): Promise<ExistingBackupsResponse> {
+ const backup = getBackup();
+ return {
+ backupFolderPath: backup.getBackupFolderPath(),
+ backups: await backup.getExistingBackups()
+ };
}
async function backupDatabase(): Promise<BackupDatabaseNowResponse> {
diff --git a/packages/trilium-core/src/services/backup.ts b/packages/trilium-core/src/services/backup.ts
index 46a1182a19..fe1a94d503 100644
--- a/packages/trilium-core/src/services/backup.ts
+++ b/packages/trilium-core/src/services/backup.ts
@@ -47,6 +47,14 @@ export default abstract class BackupService {
*/
abstract getExistingBackups(): Promise<DatabaseBackup[]>;
+ /**
+ * Get the directory where backups are stored, for display purposes.
+ * Returns null when there is no user-accessible location (e.g. OPFS on standalone).
+ */
+ getBackupFolderPath(): string | null {
+ return null;
+ }
+
/**
* Get the content of a backup file.
* Returns null if the backup doesn't exist or access is denied.