feat(backups): improve backup retention across different database types

- Add serverId parameter to keepLatestNBackups function
- Execute backup retention commands on the specific server for each database type
- Remove global backup retention call in favor of per-database type retention
This commit is contained in:
Mauricio Siu 2025-03-09 11:54:36 -06:00
parent 688601107c
commit 6467ce0a24
3 changed files with 19 additions and 9 deletions

View File

@ -8,7 +8,7 @@ import {
runMariadbBackup,
runMongoBackup,
runMySqlBackup,
runPostgresBackup
runPostgresBackup,
} from "@dokploy/server";
import { db } from "@dokploy/server/dist/db";
import { backups, server } from "@dokploy/server/dist/db/schema";
@ -31,6 +31,7 @@ export const runJobs = async (job: QueueJob) => {
return;
}
await runPostgresBackup(postgres, backup);
await keepLatestNBackups(backup, server.serverId);
} else if (databaseType === "mysql" && mysql) {
const server = await findServerById(mysql.serverId as string);
if (server.serverStatus === "inactive") {
@ -38,6 +39,7 @@ export const runJobs = async (job: QueueJob) => {
return;
}
await runMySqlBackup(mysql, backup);
await keepLatestNBackups(backup, server.serverId);
} else if (databaseType === "mongo" && mongo) {
const server = await findServerById(mongo.serverId as string);
if (server.serverStatus === "inactive") {
@ -45,6 +47,7 @@ export const runJobs = async (job: QueueJob) => {
return;
}
await runMongoBackup(mongo, backup);
await keepLatestNBackups(backup, server.serverId);
} else if (databaseType === "mariadb" && mariadb) {
const server = await findServerById(mariadb.serverId as string);
if (server.serverStatus === "inactive") {
@ -52,9 +55,8 @@ export const runJobs = async (job: QueueJob) => {
return;
}
await runMariadbBackup(mariadb, backup);
await keepLatestNBackups(backup, server.serverId);
}
await keepLatestNBackups(backup);
}
if (job.type === "server") {
const { serverId } = job;

View File

@ -14,7 +14,7 @@ import { runMySqlBackup } from "./mysql";
import { runPostgresBackup } from "./postgres";
import { findAdmin } from "../../services/admin";
import { getS3Credentials } from "./utils";
import { execAsync } from "../process/execAsync";
import { execAsync, execAsyncRemote } from "../process/execAsync";
import type { BackupSchedule } from "@dokploy/server/services/backup";
import { startLogCleanup } from "../access-log/handler";
@ -180,7 +180,10 @@ export const initCronJobs = async () => {
}
};
export const keepLatestNBackups = async (backup: BackupSchedule) => {
export const keepLatestNBackups = async (
backup: BackupSchedule,
serverId?: string | null,
) => {
// 0 also immediately returns which is good as the empty "keep latest" field in the UI
// is saved as 0 in the database
if (!backup.keepLatestCount) return;
@ -202,8 +205,11 @@ export const keepLatestNBackups = async (backup: BackupSchedule) => {
const rcloneCommand = `${rcloneList} | ${sortAndPickUnwantedBackups} ${rcloneDelete}`;
// we can execute this command on any server it doesn't matter
await execAsync(rcloneCommand);
if (serverId) {
await execAsyncRemote(serverId, rcloneCommand);
} else {
await execAsync(rcloneCommand);
}
} catch (error) {
console.error(error);
throw error;

View File

@ -13,15 +13,17 @@ export const scheduleBackup = (backup: BackupSchedule) => {
scheduleJob(backupId, schedule, async () => {
if (databaseType === "postgres" && postgres) {
await runPostgresBackup(postgres, backup);
await keepLatestNBackups(backup, postgres.serverId);
} else if (databaseType === "mysql" && mysql) {
await runMySqlBackup(mysql, backup);
await keepLatestNBackups(backup, mysql.serverId);
} else if (databaseType === "mongo" && mongo) {
await runMongoBackup(mongo, backup);
await keepLatestNBackups(backup, mongo.serverId);
} else if (databaseType === "mariadb" && mariadb) {
await runMariadbBackup(mariadb, backup);
await keepLatestNBackups(backup, mariadb.serverId);
}
await keepLatestNBackups(backup);
});
};