feat: allow stopping an ongoing backup

This commit is contained in:
Nicolas Meienberger
2025-11-08 23:26:53 +01:00
parent 3abf8ab12d
commit 5f620b4c45
11 changed files with 258 additions and 97 deletions

View File

@@ -10,7 +10,7 @@ interface ServerEvents {
scheduleId: number;
volumeName: string;
repositoryName: string;
status: "success" | "error";
status: "success" | "error" | "stopped";
}) => void;
"volume:mounted": (data: { volumeName: string }) => void;
"volume:unmounted": (data: { volumeName: string }) => void;

View File

@@ -8,6 +8,7 @@ import {
getBackupScheduleForVolumeDto,
listBackupSchedulesDto,
runBackupNowDto,
stopBackupDto,
updateBackupScheduleDto,
updateBackupScheduleBody,
type CreateBackupScheduleDto,
@@ -16,6 +17,7 @@ import {
type GetBackupScheduleForVolumeResponseDto,
type ListBackupSchedulesResponseDto,
type RunBackupNowDto,
type StopBackupDto,
type UpdateBackupScheduleDto,
} from "./backups.dto";
import { backupsService } from "./backups.service";
@@ -69,4 +71,11 @@ export const backupScheduleController = new Hono()
});
return c.json<RunBackupNowDto>({ success: true }, 200);
})
.post("/:scheduleId/stop", stopBackupDto, async (c) => {
const scheduleId = c.req.param("scheduleId");
await backupsService.stopBackup(Number(scheduleId));
return c.json<StopBackupDto>({ success: true }, 200);
});

View File

@@ -223,3 +223,31 @@ export const runBackupNowDto = describeRoute({
},
},
});
/**
* Stop a running backup
*/
export const stopBackupResponse = type({
success: "boolean",
});
export type StopBackupDto = typeof stopBackupResponse.infer;
export const stopBackupDto = describeRoute({
description: "Stop a backup that is currently in progress",
operationId: "stopBackup",
tags: ["Backups"],
responses: {
200: {
description: "Backup stopped successfully",
content: {
"application/json": {
schema: resolver(stopBackupResponse),
},
},
},
409: {
description: "No backup is currently running for this schedule",
},
},
});

View File

@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";
import cron from "node-cron";
import { CronExpressionParser } from "cron-parser";
import { NotFoundError, BadRequestError } from "http-errors-enhanced";
import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced";
import { db } from "../../db/db";
import { backupSchedulesTable, repositoriesTable, volumesTable } from "../../db/schema";
import { restic } from "../../utils/restic";
@@ -11,6 +11,8 @@ import type { CreateBackupScheduleBody, UpdateBackupScheduleBody } from "./backu
import { toMessage } from "../../utils/errors";
import { serverEvents } from "../../core/events";
const runningBackups = new Map<number, AbortController>();
const calculateNextRun = (cronExpression: string): number => {
try {
const interval = CronExpressionParser.parse(cronExpression, {
@@ -198,6 +200,9 @@ const executeBackup = async (scheduleId: number, manual = false) => {
.set({ lastBackupStatus: "in_progress", updatedAt: Date.now() })
.where(eq(backupSchedulesTable.id, scheduleId));
const abortController = new AbortController();
runningBackups.set(scheduleId, abortController);
try {
const volumePath = getVolumePath(volume);
@@ -205,8 +210,10 @@ const executeBackup = async (scheduleId: number, manual = false) => {
exclude?: string[];
include?: string[];
tags?: string[];
signal?: AbortSignal;
} = {
tags: [schedule.id.toString()],
signal: abortController.signal,
};
if (schedule.excludePatterns && schedule.excludePatterns.length > 0) {
@@ -264,6 +271,8 @@ const executeBackup = async (scheduleId: number, manual = false) => {
});
throw error;
} finally {
runningBackups.delete(scheduleId);
}
};
@@ -293,6 +302,34 @@ const getScheduleForVolume = async (volumeId: number) => {
return schedule ?? null;
};
const stopBackup = async (scheduleId: number) => {
const schedule = await db.query.backupSchedulesTable.findFirst({
where: eq(backupSchedulesTable.id, scheduleId),
});
if (!schedule) {
throw new NotFoundError("Backup schedule not found");
}
await db
.update(backupSchedulesTable)
.set({
lastBackupStatus: "error",
lastBackupError: "Backup was stopped by user",
updatedAt: Date.now(),
})
.where(eq(backupSchedulesTable.id, scheduleId));
const abortController = runningBackups.get(scheduleId);
if (!abortController) {
throw new ConflictError("No backup is currently running for this schedule");
}
logger.info(`Stopping backup for schedule ${scheduleId}`);
abortController.abort();
};
export const backupsService = {
listSchedules,
getSchedule,
@@ -302,4 +339,5 @@ export const backupsService = {
executeBackup,
getSchedulesToExecute,
getScheduleForVolume,
stopBackup,
};

View File

@@ -23,7 +23,7 @@ export const eventsController = new Hono().get("/", (c) => {
scheduleId: number;
volumeName: string;
repositoryName: string;
status: "success" | "error";
status: "success" | "error" | "stopped";
}) => {
stream.writeSSE({
data: JSON.stringify(data),

View File

@@ -1,6 +1,7 @@
import crypto from "node:crypto";
import fs from "node:fs/promises";
import path from "node:path";
import { spawn } from "node:child_process";
import type { RepositoryConfig } from "@ironmount/schemas/restic";
import { type } from "arktype";
import { $ } from "bun";
@@ -114,7 +115,7 @@ const init = async (config: RepositoryConfig) => {
const backup = async (
config: RepositoryConfig,
source: string,
options?: { exclude?: string[]; include?: string[]; tags?: string[] },
options?: { exclude?: string[]; include?: string[]; tags?: string[]; signal?: AbortSignal },
) => {
const repoUrl = buildRepoUrl(config);
const env = await buildEnv(config);
@@ -148,32 +149,68 @@ const backup = async (
args.push("--json");
// await $`restic unlock --repo ${repoUrl}`.env(env).nothrow();
const res = await $`restic ${args}`.env(env).nothrow();
return new Promise((resolve, reject) => {
const child = spawn("restic", args, {
env: { ...process.env, ...env },
signal: options?.signal,
});
if (includeFile) {
await fs.unlink(includeFile).catch(() => {});
}
let stdout = "";
let stderr = "";
if (res.exitCode !== 0) {
logger.error(`Restic backup failed: ${res.stderr}`);
throw new Error(`Restic backup failed: ${res.stderr}`);
}
child.stdout.on("data", (data) => {
stdout += data.toString();
});
// res is a succession of JSON objects, we need to parse the last one which contains the summary
const stdout = res.text();
const outputLines = stdout.trim().split("\n");
const lastLine = outputLines[outputLines.length - 1];
const resSummary = JSON.parse(lastLine ?? "{}");
child.stderr.on("data", (data) => {
stderr += data.toString();
});
const result = backupOutputSchema(resSummary);
child.on("error", async (error) => {
if (includeFile) {
await fs.unlink(includeFile).catch(() => {});
}
if (result instanceof type.errors) {
logger.error(`Restic backup output validation failed: ${result}`);
throw new Error(`Restic backup output validation failed: ${result}`);
}
if (error.name === "AbortError") {
logger.info("Restic backup process was aborted");
reject(error);
} else {
logger.error(`Restic backup process error: ${error.message}`);
reject(new Error(`Restic backup process error: ${error.message}`));
}
});
return result;
child.on("close", async (code) => {
if (includeFile) {
await fs.unlink(includeFile).catch(() => {});
}
if (code !== 0) {
logger.error(`Restic backup failed with exit code ${code}: ${stderr}`);
reject(new Error(`Restic backup failed: ${stderr}`));
return;
}
try {
const outputLines = stdout.trim().split("\n");
const lastLine = outputLines[outputLines.length - 1];
const resSummary = JSON.parse(lastLine ?? "{}");
const result = backupOutputSchema(resSummary);
if (result instanceof type.errors) {
logger.error(`Restic backup output validation failed: ${result}`);
reject(new Error(`Restic backup output validation failed: ${result}`));
return;
}
resolve(result);
} catch (error) {
logger.error(`Failed to parse restic backup output: ${error}`);
reject(new Error(`Failed to parse restic backup output: ${error}`));
}
});
});
};
const restoreOutputSchema = type({