refactor(backups): tag snapshots by backup id and run forget by grouping first by tags

This commit is contained in:
Nicolas Meienberger
2025-11-04 20:09:38 +01:00
parent ecd517341c
commit 01c2a3669c
12 changed files with 66 additions and 268 deletions

View File

@@ -8,10 +8,8 @@ import {
getBackupScheduleForVolumeDto,
listBackupSchedulesDto,
runBackupNowDto,
updateBackupScheduleBody,
updateBackupScheduleDto,
upsertBackupScheduleBody,
upsertBackupScheduleDto,
updateBackupScheduleBody,
type CreateBackupScheduleDto,
type DeleteBackupScheduleDto,
type GetBackupScheduleDto,
@@ -19,7 +17,6 @@ import {
type ListBackupSchedulesResponseDto,
type RunBackupNowDto,
type UpdateBackupScheduleDto,
type UpsertBackupScheduleDto,
} from "./backups.dto";
import { backupsService } from "./backups.service";
@@ -57,13 +54,6 @@ export const backupScheduleController = new Hono()
return c.json<UpdateBackupScheduleDto>(schedule, 200);
})
.put("/upsert", upsertBackupScheduleDto, validator("json", upsertBackupScheduleBody), async (c) => {
const body = c.req.valid("json");
const schedule = await backupsService.upsertSchedule(body);
return c.json<UpsertBackupScheduleDto>(schedule, 200);
})
.delete("/:scheduleId", deleteBackupScheduleDto, async (c) => {
const scheduleId = c.req.param("scheduleId");
@@ -74,7 +64,7 @@ export const backupScheduleController = new Hono()
.post("/:scheduleId/run", runBackupNowDto, async (c) => {
const scheduleId = c.req.param("scheduleId");
backupsService.executeBackup(Number(scheduleId)).catch((error) => {
backupsService.executeBackup(Number(scheduleId), true).catch((error) => {
console.error("Backup execution failed:", error);
});

View File

@@ -143,9 +143,9 @@ export const createBackupScheduleDto = describeRoute({
* Update a backup schedule
*/
export const updateBackupScheduleBody = type({
repositoryId: "string?",
repositoryId: "string",
enabled: "boolean?",
cronExpression: "string?",
cronExpression: "string",
retentionPolicy: retentionPolicySchema.optional(),
excludePatterns: "string[]?",
includePatterns: "string[]?",
@@ -174,42 +174,6 @@ export const updateBackupScheduleDto = describeRoute({
},
});
/**
* Upsert a backup schedule (create or update)
*/
export const upsertBackupScheduleBody = type({
volumeId: "number",
repositoryId: "string",
enabled: "boolean",
cronExpression: "string",
retentionPolicy: retentionPolicySchema.optional(),
excludePatterns: "string[]?",
includePatterns: "string[]?",
tags: "string[]?",
});
export type UpsertBackupScheduleBody = typeof upsertBackupScheduleBody.infer;
export const upsertBackupScheduleResponse = backupScheduleSchema.omit("volume", "repository");
export type UpsertBackupScheduleDto = typeof upsertBackupScheduleResponse.infer;
export const upsertBackupScheduleDto = describeRoute({
description: "Create or update a backup schedule for a volume",
operationId: "upsertBackupSchedule",
tags: ["Backups"],
responses: {
200: {
description: "Backup schedule upserted successfully",
content: {
"application/json": {
schema: resolver(upsertBackupScheduleResponse),
},
},
},
},
});
/**
* Delete a backup schedule
*/

View File

@@ -7,7 +7,7 @@ import { backupSchedulesTable, repositoriesTable, volumesTable } from "../../db/
import { restic } from "../../utils/restic";
import { logger } from "../../utils/logger";
import { getVolumePath } from "../volumes/helpers";
import type { CreateBackupScheduleBody, UpdateBackupScheduleBody, UpsertBackupScheduleBody } from "./backups.dto";
import type { CreateBackupScheduleBody, UpdateBackupScheduleBody } from "./backups.dto";
import { toMessage } from "../../utils/errors";
const calculateNextRun = (cronExpression: string): number => {
@@ -109,14 +109,12 @@ const updateSchedule = async (scheduleId: number, data: UpdateBackupScheduleBody
throw new BadRequestError("Invalid cron expression");
}
if (data.repositoryId) {
const repository = await db.query.repositoriesTable.findFirst({
where: eq(repositoriesTable.id, data.repositoryId),
});
const repository = await db.query.repositoriesTable.findFirst({
where: eq(repositoriesTable.id, data.repositoryId),
});
if (!repository) {
throw new NotFoundError("Repository not found");
}
if (!repository) {
throw new NotFoundError("Repository not found");
}
const cronExpression = data.cronExpression ?? schedule.cronExpression;
@@ -147,7 +145,7 @@ const deleteSchedule = async (scheduleId: number) => {
await db.delete(backupSchedulesTable).where(eq(backupSchedulesTable.id, scheduleId));
};
const executeBackup = async (scheduleId: number) => {
const executeBackup = async (scheduleId: number, manual = false) => {
const schedule = await db.query.backupSchedulesTable.findFirst({
where: eq(backupSchedulesTable.id, scheduleId),
});
@@ -156,7 +154,7 @@ const executeBackup = async (scheduleId: number) => {
throw new NotFoundError("Backup schedule not found");
}
if (!schedule.enabled) {
if (!schedule.enabled && !manual) {
logger.info(`Backup schedule ${scheduleId} is disabled. Skipping execution.`);
return;
}
@@ -190,7 +188,9 @@ const executeBackup = async (scheduleId: number) => {
exclude?: string[];
include?: string[];
tags?: string[];
} = {};
} = {
tags: [schedule.id.toString()],
};
if (schedule.excludePatterns && schedule.excludePatterns.length > 0) {
backupOptions.exclude = schedule.excludePatterns;
@@ -203,7 +203,7 @@ const executeBackup = async (scheduleId: number) => {
await restic.backup(repository.config, volumePath, backupOptions);
if (schedule.retentionPolicy) {
await restic.forget(repository.config, schedule.retentionPolicy);
await restic.forget(repository.config, schedule.retentionPolicy, { tag: schedule.id.toString() });
}
const nextBackupAt = calculateNextRun(schedule.cronExpression);
@@ -262,77 +262,6 @@ const getScheduleForVolume = async (volumeId: number) => {
return schedule ?? null;
};
const upsertSchedule = async (data: UpsertBackupScheduleBody) => {
if (!cron.validate(data.cronExpression)) {
throw new BadRequestError("Invalid cron expression");
}
const volume = await db.query.volumesTable.findFirst({
where: eq(volumesTable.id, data.volumeId),
});
if (!volume) {
throw new NotFoundError("Volume not found");
}
const repository = await db.query.repositoriesTable.findFirst({
where: eq(repositoriesTable.id, data.repositoryId),
});
if (!repository) {
throw new NotFoundError("Repository not found");
}
const existingSchedule = await db.query.backupSchedulesTable.findFirst({
where: eq(backupSchedulesTable.volumeId, data.volumeId),
});
const nextBackupAt = calculateNextRun(data.cronExpression);
if (existingSchedule) {
const [updated] = await db
.update(backupSchedulesTable)
.set({
repositoryId: data.repositoryId,
enabled: data.enabled,
cronExpression: data.cronExpression,
retentionPolicy: data.retentionPolicy ?? null,
excludePatterns: data.excludePatterns ?? [],
includePatterns: data.includePatterns ?? [],
nextBackupAt: nextBackupAt,
updatedAt: Date.now(),
})
.where(eq(backupSchedulesTable.id, existingSchedule.id))
.returning();
if (!updated) {
throw new Error("Failed to update backup schedule");
}
return updated;
}
const [newSchedule] = await db
.insert(backupSchedulesTable)
.values({
volumeId: data.volumeId,
repositoryId: data.repositoryId,
enabled: data.enabled,
cronExpression: data.cronExpression,
retentionPolicy: data.retentionPolicy ?? null,
excludePatterns: data.excludePatterns ?? [],
includePatterns: data.includePatterns ?? [],
nextBackupAt: nextBackupAt,
})
.returning();
if (!newSchedule) {
throw new Error("Failed to create backup schedule");
}
return newSchedule;
};
export const backupsService = {
listSchedules,
getSchedule,
@@ -342,5 +271,4 @@ export const backupsService = {
executeBackup,
getSchedulesToExecute,
getScheduleForVolume,
upsertSchedule,
};

View File

@@ -49,9 +49,9 @@ export const repositoriesController = new Hono()
})
.get("/:name/snapshots", listSnapshotsDto, validator("query", listSnapshotsFilters), async (c) => {
const { name } = c.req.param();
const { volumeId } = c.req.valid("query");
const { backupId } = c.req.valid("query");
const res = await repositoriesService.listSnapshots(name, Number(volumeId));
const res = await repositoriesService.listSnapshots(name, backupId);
const snapshots = res.map((snapshot) => {
const { summary } = snapshot;

View File

@@ -144,7 +144,7 @@ const listSnapshotsResponse = snapshotSchema.array();
export type ListSnapshotsDto = typeof listSnapshotsResponse.infer;
export const listSnapshotsFilters = type({
volumeId: "string?",
backupId: "string?",
});
export const listSnapshotsDto = describeRoute({

View File

@@ -8,7 +8,6 @@ import { repositoriesTable, volumesTable } from "../../db/schema";
import { toMessage } from "../../utils/errors";
import { restic } from "../../utils/restic";
import { cryptoUtils } from "../../utils/crypto";
import { getVolumePath } from "../volumes/helpers";
const listRepositories = async () => {
const repositories = await db.query.repositoriesTable.findMany({});
@@ -106,7 +105,15 @@ const deleteRepository = async (name: string) => {
await db.delete(repositoriesTable).where(eq(repositoriesTable.name, name));
};
const listSnapshots = async (name: string, volumeId?: number) => {
/**
* List snapshots for a given repository
* If backupId is provided, filter snapshots by that backup ID (tag)
* @param name Repository name
* @param backupId Optional backup ID to filter snapshots for a specific backup schedule
*
* @returns List of snapshots
*/
const listSnapshots = async (name: string, backupId?: string) => {
const repository = await db.query.repositoriesTable.findFirst({
where: eq(repositoriesTable.name, name),
});
@@ -115,20 +122,12 @@ const listSnapshots = async (name: string, volumeId?: number) => {
throw new NotFoundError("Repository not found");
}
let snapshots = await restic.snapshots(repository.config);
let snapshots = [];
if (volumeId) {
const volume = await db.query.volumesTable.findFirst({
where: eq(volumesTable.id, volumeId),
});
if (!volume) {
throw new NotFoundError("Volume not found");
}
snapshots = snapshots.filter((snapshot) => {
return snapshot.paths.some((path) => path.includes(getVolumePath(volume.name)));
});
if (backupId) {
snapshots = await restic.snapshots(repository.config, { tags: [backupId.toString()] });
} else {
snapshots = await restic.snapshots(repository.config);
}
return snapshots;

View File

@@ -114,13 +114,19 @@ const init = async (config: RepositoryConfig) => {
const backup = async (
config: RepositoryConfig,
source: string,
options?: { exclude?: string[]; include?: string[] },
options?: { exclude?: string[]; include?: string[]; tags?: string[] },
) => {
const repoUrl = buildRepoUrl(config);
const env = await buildEnv(config);
const args: string[] = ["--repo", repoUrl, "backup", "--one-file-system"];
if (options?.tags && options.tags.length > 0) {
for (const tag of options.tags) {
args.push("--tag", tag);
}
}
let includeFile: string | null = null;
if (options?.include && options.include.length > 0) {
const tmp = await fs.mkdtemp("restic-include");
@@ -263,11 +269,23 @@ const restore = async (
return result;
};
const snapshots = async (config: RepositoryConfig) => {
const snapshots = async (config: RepositoryConfig, options: { tags?: string[] } = {}) => {
const { tags } = options;
const repoUrl = buildRepoUrl(config);
const env = await buildEnv(config);
const res = await $`restic --repo ${repoUrl} snapshots --json`.env(env).nothrow();
const args = ["--repo", repoUrl, "snapshots"];
if (tags && tags.length > 0) {
for (const tag of tags) {
args.push("--tag", tag);
}
}
args.push("--json");
const res = await $`restic ${args}`.env(env).nothrow();
if (res.exitCode !== 0) {
logger.error(`Restic snapshots retrieval failed: ${res.stderr}`);
@@ -284,11 +302,11 @@ const snapshots = async (config: RepositoryConfig) => {
return result;
};
const forget = async (config: RepositoryConfig, options: RetentionPolicy) => {
const forget = async (config: RepositoryConfig, options: RetentionPolicy, extra: { tag: string }) => {
const repoUrl = buildRepoUrl(config);
const env = await buildEnv(config);
const args: string[] = ["--repo", repoUrl, "forget"];
const args: string[] = ["--repo", repoUrl, "forget", "--group-by", "tags", "--tag", extra.tag];
if (options.keepLast) {
args.push("--keep-last", String(options.keepLast));
@@ -322,7 +340,6 @@ const forget = async (config: RepositoryConfig, options: RetentionPolicy) => {
throw new Error(`Restic forget failed: ${res.stderr}`);
}
logger.info("Restic forget completed successfully");
return { success: true };
};