Skip to content

Build your personal knowledge base with Trilium Notes

Backend for backup download button

Lorinc936ccbd962e0b3c
diff --git a/apps/server/src/routes/api/database.ts b/apps/server/src/routes/api/database.ts
index c29f6e9aaa..e014676712 100644
--- a/apps/server/src/routes/api/database.ts
+++ b/apps/server/src/routes/api/database.ts
@@ -1,15 +1,17 @@
-"use strict";
+import { BackupDatabaseNowResponse, DatabaseCheckIntegrityResponse } from "@triliumnext/commons";
+import type { Request, Response } from "express";
+import fs from "fs";
+import path from "path";
-import sql from "../../services/sql.js";
-import log from "../../services/log.js";
-import backupService from "../../services/backup.js";
+import becca_loader from "../../becca/becca_loader.js";
+import ValidationError from "../../errors/validation_error.js";
import anonymizationService from "../../services/anonymization.js";
+import backupService from "../../services/backup.js";
import consistencyChecksService from "../../services/consistency_checks.js";
-import type { Request } from "express";
-import ValidationError from "../../errors/validation_error.js";
+import dataDir from "../../services/data_dir.js";
+import log from "../../services/log.js";
+import sql from "../../services/sql.js";
import sql_init from "../../services/sql_init.js";
-import becca_loader from "../../becca/becca_loader.js";
-import { BackupDatabaseNowResponse, DatabaseCheckIntegrityResponse } from "@triliumnext/commons";
function getExistingBackups() {
return backupService.getExistingBackups();
@@ -58,6 +60,27 @@ function checkIntegrity() {
} satisfies DatabaseCheckIntegrityResponse;
}
+function downloadBackup(req: Request, res: Response) {
+ const filePath = req.query.filePath as string;
+ if (!filePath) {
+ res.status(400).send("Missing filePath");
+ return;
+ }
+
+ const resolvedPath = path.resolve(filePath);
+ if (!resolvedPath.startsWith(path.resolve(dataDir.BACKUP_DIR) + path.sep)) {
+ res.status(403).send("Access denied");
+ return;
+ }
+
+ if (!fs.existsSync(resolvedPath)) {
+ res.status(404).send("Backup file not found");
+ return;
+ }
+
+ res.download(resolvedPath, path.basename(resolvedPath));
+}
+
export default {
getExistingBackups,
backupDatabase,
@@ -66,5 +89,6 @@ export default {
rebuildIntegrationTestDatabase,
getExistingAnonymizedDatabases,
anonymize,
- checkIntegrity
+ checkIntegrity,
+ downloadBackup
};
diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.ts
index ce9b84f0a9..aaa199bd14 100644
--- a/apps/server/src/routes/routes.ts
+++ b/apps/server/src/routes/routes.ts
@@ -307,7 +307,7 @@ function register(app: express.Application) {
// backup requires execution outside of transaction
asyncRoute(PST, "/api/database/backup-database", [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.backupDatabase, apiResultHandler);
apiRoute(GET, "/api/database/backups", databaseRoute.getExistingBackups);
-
+ route(GET, "/api/database/backup/download", [auth.checkApiAuthOrElectron], databaseRoute.downloadBackup);
// VACUUM requires execution outside of transaction
asyncRoute(PST, "/api/database/vacuum-database", [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.vacuumDatabase, apiResultHandler);