Skip to content

Build your personal knowledge base with Trilium Notes

client/options/database: add backup info

Adorian Doran17ba53c70ab8
diff --git a/apps/client/src/services/database_files.ts b/apps/client/src/services/database_files.ts
index c1a19c7b1f..c244fe66fb 100644
--- a/apps/client/src/services/database_files.ts
+++ b/apps/client/src/services/database_files.ts
@@ -1,3 +1,5 @@
+import { dayjs } from "@triliumnext/commons";
+
import { formatDateTime } from "../utils/formatters.js";
import { t } from "./i18n.js";
import { formatSize } from "./utils.js";
@@ -70,3 +72,26 @@ export function describeDatabaseFile(file: DatabaseFile): string {
return parts.join(" • ");
}
+
+/**
+ * How many backups there are and how long ago the last one was made — the two things a list of them
+ * only answers by being read through. Nothing is said while there are none: wherever this is shown
+ * the list itself, or the page it links to, states that more plainly than a sentence could.
+ *
+ * Shared by the backup page's own header and by the Database page's summary of it, so that the two
+ * never state the same thing differently.
+ */
+export function summarizeBackups(backups: { mtime: Date }[]): string | null {
+ if (!backups.length) {
+ return null;
+ }
+
+ const mostRecent = backups.reduce((latest, backup) => (
+ backup.mtime > latest.mtime ? backup : latest
+ ));
+
+ return t("backup.backups_summary", {
+ count: backups.length,
+ age: dayjs(mostRecent.mtime).fromNow(true)
+ });
+}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 017b14e7ec..b321a00d9b 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1757,6 +1757,8 @@
"info_attachments_one": "{{count}} attachment",
"info_attachments_other": "{{count}} attachments",
"info_size": "Size",
+ "info_backup": "Backup",
+ "info_no_backup": "No backups yet",
"maintenance": "Maintenance",
"analyze_space_usage": "Analyze space usage",
"analyze_space_usage_description": "Identify which notes take up the most space, and get a clearer picture of how large your notes are.",
diff --git a/apps/client/src/widgets/type_widgets/options/backup.tsx b/apps/client/src/widgets/type_widgets/options/backup.tsx
index 75482ac980..2608ecf931 100644
--- a/apps/client/src/widgets/type_widgets/options/backup.tsx
+++ b/apps/client/src/widgets/type_widgets/options/backup.tsx
@@ -4,13 +4,12 @@ import {
BackupDatabaseNowResponse,
BackupPassphraseStatus,
DatabaseBackup,
- dayjs,
ExistingBackupsResponse
} from "@triliumnext/commons";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { isBackupDownloadSupported } from "../../../services/backup_download";
-import { describeDatabaseFormat } from "../../../services/database_files";
+import { describeDatabaseFormat, summarizeBackups } from "../../../services/database_files";
import dialogService from "../../../services/dialog";
import { t } from "../../../services/i18n";
import options from "../../../services/options";
@@ -201,26 +200,6 @@ export function BackupStatus({ backups, refreshCallback }: BackupStatusProps) {
);
}
-/**
- * How many backups there are and how long ago the last one was made — the two things the list
- * itself only answers by being read through. Nothing is said while there are none: the list
- * stands empty right below, which states it more plainly than a sentence could.
- */
-function summarizeBackups(backups: DatabaseBackup[]) {
- if (!backups.length) {
- return null;
- }
-
- const mostRecent = backups.reduce((latest, backup) => (
- backup.mtime > latest.mtime ? backup : latest
- ));
-
- return t("backup.backups_summary", {
- count: backups.length,
- age: dayjs(mostRecent.mtime).fromNow(true)
- });
-}
-
export function BackupConfiguration() {
const [dailyBackupEnabled, setDailyBackupEnabled] = useTriliumOptionBool("dailyBackupEnabled");
const [weeklyBackupEnabled, setWeeklyBackupEnabled] = useTriliumOptionBool("weeklyBackupEnabled");
diff --git a/apps/client/src/widgets/type_widgets/options/database.spec.tsx b/apps/client/src/widgets/type_widgets/options/database.spec.tsx
index 064d5ad21f..66a246ace0 100644
--- a/apps/client/src/widgets/type_widgets/options/database.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/database.spec.tsx
@@ -27,6 +27,9 @@ const DATABASE_INFO = {
/** Set where a test needs the info card to have nothing to say. */
const failing = vi.hoisted(() => ({ info: false }));
+/** What the backup page would list; emptied by the test about a database never backed up. */
+let BACKUPS: { mtime: Date }[] = [];
+
// The info, maintenance and anonymization cards call the backend as they render and as they are used.
const server = vi.hoisted(() => ({
get: vi.fn(async (url: string) => {
@@ -36,6 +39,8 @@ const server = vi.hoisted(() => ({
throw new Error("no answer");
}
return DATABASE_INFO;
+ case "database/backups":
+ return { backupFolderPath: "/data/backup", backups: BACKUPS };
case "database/check-integrity":
return { results: [{ integrity_check: "ok" }] };
case "database/anonymized-databases":
@@ -102,6 +107,7 @@ beforeEach(() => {
setupMode.isStartOverPending.mockReset().mockResolvedValue(false);
setupMode.cancelStartOver.mockReset().mockResolvedValue(undefined);
failing.info = false;
+ BACKUPS = [ { mtime: new Date("2026-01-01T10:00:00Z") }, { mtime: new Date("2026-01-02T10:00:00Z") } ];
noteContext.setNote.mockClear();
contentManager.requestContentManagerSection.mockClear();
cleanup.showCleanupDialog.mockReset().mockResolvedValue(1234);
@@ -140,6 +146,24 @@ describe("what the database is", () => {
expect(size).toBe("3 MiB");
});
+ it("summarizes the backups as their own page does, and leads to it", async () => {
+ renderPage();
+ await settle();
+
+ const backup = container.querySelector<HTMLAnchorElement>(".database-info a[href]");
+ // The same sentence the backup page's header carries, counted over the same listing.
+ expect(backup?.textContent).toBe("backup.backups_summary=2");
+ expect(backup?.getAttribute("href")).toBe("#root/_hidden/_options/_optionsBackup");
+ });
+
+ it("says a database has never been backed up rather than leaving the row blank", async () => {
+ BACKUPS = [];
+ renderPage();
+ await settle();
+
+ expect(container.querySelector(".database-info a[href]")?.textContent).toBe("database.info_no_backup");
+ });
+
it("reads the figures again once the database has been compacted", async () => {
renderPage();
await settle();
diff --git a/apps/client/src/widgets/type_widgets/options/database.tsx b/apps/client/src/widgets/type_widgets/options/database.tsx
index 634acb0393..8cc06f5da9 100644
--- a/apps/client/src/widgets/type_widgets/options/database.tsx
+++ b/apps/client/src/widgets/type_widgets/options/database.tsx
@@ -5,10 +5,12 @@ import {
DatabaseAnonymizeResponse,
DatabaseCheckIntegrityResponse,
DatabaseInfoResponse,
- ExistingAnonymizedDatabasesResponse
+ ExistingAnonymizedDatabasesResponse,
+ ExistingBackupsResponse
} from "@triliumnext/commons";
import { useCallback, useEffect, useState } from "preact/hooks";
+import { summarizeBackups } from "../../../services/database_files";
import { t } from "../../../services/i18n";
import server from "../../../services/server";
import {
@@ -84,6 +86,9 @@ export default function DatabaseSettings() {
*/
function DatabaseInfo({ refreshToken }: { refreshToken: number }) {
const { data: info } = useFetch<DatabaseInfoResponse>("database/info", refreshToken);
+ // The backups are the one thing here this page does nothing about, so they are read from the
+ // page that does — the same listing, summarized by the same sentence its own header carries.
+ const { data: backups } = useFetch<ExistingBackupsResponse>("database/backups", refreshToken);
if (!info) {
return null;
@@ -117,11 +122,26 @@ function DatabaseInfo({ refreshToken }: { refreshToken: number }) {
<CardOption label={t("database.info_size")}>
<span className="database-info-value">{formatSize(info.sizeBytes)}</span>
</CardOption>
+
+ {backups && (
+ <CardOption label={t("database.info_backup")}>
+ {/* The whole value is the way to the page that acts on it: this row states
+ how the backups stand, and everything else about them is done there. */}
+ <span className="database-info-value">
+ <a className="tn-link" href={BACKUP_PAGE_LINK}>
+ {summarizeBackups(backups.backups) ?? t("database.info_no_backup")}
+ </a>
+ </span>
+ </CardOption>
+ )}
</Card>
</div>
);
}
+/** A note path, which is how one options page links to another. */
+const BACKUP_PAGE_LINK = "#root/_hidden/_options/_optionsBackup";
+
/**
* Keeping the database in good order: finding out what it is spent on, taking back what is no longer
* needed, and the checks and repairs that act on the file itself rather than on what is kept in it.