Archive the whole data volume in Backups, not just the database

Settings > Backups gains a whole-data archive beside the database
snapshots, and both lists now fully work from the page.

- Data archives: `write-data-backup-archive.ts` tars all of DATA_DIR into
  BACKUP_ARCHIVE_DIR as cobble-data-<epoch-ms>.tar.gz, every entry under one
  root folder named after the archive, writing a .part name and renaming on
  success so a cut-off run is never listed. The newest
  AppData.DataBackupsKept are kept; db/.backups is left as it is.
- Restore: `restore-data-backup.ts` extracts to a temp dir, requires one
  root folder holding a db folder, takes a safety archive first, then
  replaces DATA_DIR (the database snapshots included) and `restart-app.ts`
  restarts the process through the docker socket, since the open SQLite
  handle points at the replaced file.
- Upload and restore: a modal over each list (database file or data
  archive), replacing the old /admin/settings/backups/restore page.
- Delete for both kinds, icon-only row actions shared through
  RowIconButton, and restore on every row.
- One backup or restore of either kind at a time (with-backup-run-lock).
- BACKUP_ARCHIVE_DIR is a start-up path (ProcessOnlyEnvKeys); the installer
  mounts $COBBLE_HOME/backups-archives at /backups-archives and recreates a
  container that lacks it. The image installs tar and gzip.
This commit is contained in:
2026-10-22 11:00:37 +01:00
parent fd75111bb1
commit a88898d169
56 changed files with 1686 additions and 261 deletions
@@ -0,0 +1,52 @@
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
import createDataBackup from "@/src/functions/backend/backups/create-data-backup";
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
/**
* Archives the whole `DATA_DIR` (Settings > Backups > Data archives).
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
req,
}) => {
const user = await grabSuperAdminUser({ req });
if (!user?.id) {
return {
success: false,
msg: `Unauthorized`,
};
}
try {
const backup = await createDataBackup();
await logEvent({
title: "Data backup created",
category: "system",
user_id: user.id,
plain_text: `User #${user.id} created a data backup (${backup.name}).`,
metadata: { backup_name: backup.name },
ip_address: grabRequestIP(req),
});
return {
success: true,
};
} catch (error: any) {
await logError({
title: "Data backup failed",
error,
user_id: user.id,
ip_address: grabRequestIP(req),
});
return {
success: false,
msg: error.message,
};
}
};
@@ -0,0 +1,57 @@
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
import deleteDataBackup from "@/src/functions/backend/backups/delete-data-backup";
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
import type { ApiReqParams } from "@/src/types";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
/**
* Deletes one whole-data archive.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
req,
body,
}) => {
const user = await grabSuperAdminUser({ req });
if (!user?.id) {
return {
success: false,
msg: `Unauthorized`,
};
}
const backup_name = (body as ApiReqParams | undefined)?.backup_name;
try {
deleteDataBackup({ backup_name });
await logEvent({
title: "Data backup deleted",
category: "system",
user_id: user.id,
plain_text: `User #${user.id} deleted data archive ${backup_name}.`,
metadata: { backup_name },
ip_address: grabRequestIP(req),
});
return {
success: true,
};
} catch (error: any) {
await logError({
title: "Data backup deletion failed",
error,
user_id: user.id,
metadata: { backup_name },
ip_address: grabRequestIP(req),
});
return {
success: false,
msg: error.message,
};
}
};
+57
View File
@@ -0,0 +1,57 @@
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
import deleteDbBackup from "@/src/functions/backend/backups/delete-db-backup";
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
import type { ApiReqParams } from "@/src/types";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
/**
* Deletes one database snapshot from `db/.backups`.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
req,
body,
}) => {
const user = await grabSuperAdminUser({ req });
if (!user?.id) {
return {
success: false,
msg: `Unauthorized`,
};
}
const backup_name = (body as ApiReqParams | undefined)?.backup_name;
try {
deleteDbBackup({ backup_name });
await logEvent({
title: "Database backup deleted",
category: "system",
user_id: user.id,
plain_text: `User #${user.id} deleted database backup ${backup_name}.`,
metadata: { backup_name },
ip_address: grabRequestIP(req),
});
return {
success: true,
};
} catch (error: any) {
await logError({
title: "Database backup deletion failed",
error,
user_id: user.id,
metadata: { backup_name },
ip_address: grabRequestIP(req),
});
return {
success: false,
msg: error.message,
};
}
};
@@ -0,0 +1,43 @@
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
import resolveDataBackupPath from "@/src/functions/backend/backups/resolve-data-backup-path";
import type { ApiReqParams } from "@/src/types";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
import path from "path";
/**
* Streams a whole-data archive back, by name.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
req,
query,
}) => {
const user = await grabSuperAdminUser({ req });
if (!user?.id) {
return {
success: false,
msg: `Unauthorized`,
};
}
try {
const backup_path = resolveDataBackupPath({
backup_name: (query as ApiReqParams | undefined)?.backup_name,
});
return new Response(Bun.file(backup_path), {
headers: {
"Content-Type": "application/gzip",
"Content-Disposition": `attachment; filename="${path.basename(backup_path)}"`,
},
});
} catch (error: any) {
return {
success: false,
msg: error.message,
};
}
};
@@ -0,0 +1,66 @@
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
import restoreDataBackup from "@/src/functions/backend/backups/restore-data-backup";
import restartApp from "@/src/functions/backend/backups/restart-app";
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
import type { ApiReqParams } from "@/src/types";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
/**
* Replaces the whole data volume with a saved archive, then restarts the
* app so it opens the restored database.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
req,
body,
}) => {
const user = await grabSuperAdminUser({ req });
if (!user?.id) {
return {
success: false,
msg: `Unauthorized`,
};
}
const backup_name = (body as ApiReqParams | undefined)?.backup_name;
try {
const result = await restoreDataBackup({ backup_name });
await logEvent({
title: "Data restored",
category: "system",
user_id: user.id,
plain_text: `User #${user.id} restored the data volume from archive ${result.restored_name} (safety copy ${result.safety_name}). The app restarted.`,
metadata: {
backup_name: result.restored_name,
safety_name: result.safety_name,
},
ip_address: grabRequestIP(req),
});
restartApp();
return {
success: true,
};
} catch (error: any) {
await logError({
title: "Data restore failed",
error,
user_id: user.id,
metadata: {
backup_name,
},
ip_address: grabRequestIP(req),
});
return {
success: false,
msg: error.message,
};
}
};
@@ -0,0 +1,82 @@
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
import restoreDataBackup from "@/src/functions/backend/backups/restore-data-backup";
import restartApp from "@/src/functions/backend/backups/restart-app";
import saveUploadedDataBackup from "@/src/functions/backend/backups/save-uploaded-data-backup";
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
import type {
APIResponseObject,
BunextAPIRouteHandler,
BunextServerRouteConfig,
} from "@moduletrace/bunext/types";
export const config: BunextServerRouteConfig = {
max_request_body_mb: 500,
};
/**
* Multipart upload (`file` field) of a whole-data archive. The file is
* saved into the archive directory, then restored, and the app restarts.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
req,
}) => {
const user = await grabSuperAdminUser({ req });
if (!user?.id) {
return {
success: false,
msg: `Unauthorized`,
};
}
let file_name: string | undefined;
let backup_name: string | undefined;
try {
const file = (await req.formData()).get("file");
if (!(file instanceof File)) {
throw new Error(`No archive uploaded`);
}
file_name = file.name;
backup_name = await saveUploadedDataBackup({ file });
const result = await restoreDataBackup({ backup_name });
await logEvent({
title: "Data restored from upload",
category: "system",
user_id: user.id,
plain_text: `User #${user.id} restored the data volume from uploaded archive ${file_name} (saved as ${result.restored_name}, safety copy ${result.safety_name}). The app restarted.`,
metadata: {
file_name,
backup_name: result.restored_name,
safety_name: result.safety_name,
},
ip_address: grabRequestIP(req),
});
restartApp();
return {
success: true,
};
} catch (error: any) {
await logError({
title: "Data restore from upload failed",
error,
user_id: user.id,
metadata: {
file_name,
backup_name,
},
ip_address: grabRequestIP(req),
});
return {
success: false,
msg: error.message,
};
}
};