mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
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:
@@ -8,7 +8,7 @@ import {
|
|||||||
runMariadbBackup,
|
runMariadbBackup,
|
||||||
runMongoBackup,
|
runMongoBackup,
|
||||||
runMySqlBackup,
|
runMySqlBackup,
|
||||||
runPostgresBackup
|
runPostgresBackup,
|
||||||
} from "@dokploy/server";
|
} from "@dokploy/server";
|
||||||
import { db } from "@dokploy/server/dist/db";
|
import { db } from "@dokploy/server/dist/db";
|
||||||
import { backups, server } from "@dokploy/server/dist/db/schema";
|
import { backups, server } from "@dokploy/server/dist/db/schema";
|
||||||
@@ -31,6 +31,7 @@ export const runJobs = async (job: QueueJob) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await runPostgresBackup(postgres, backup);
|
await runPostgresBackup(postgres, backup);
|
||||||
|
await keepLatestNBackups(backup, server.serverId);
|
||||||
} else if (databaseType === "mysql" && mysql) {
|
} else if (databaseType === "mysql" && mysql) {
|
||||||
const server = await findServerById(mysql.serverId as string);
|
const server = await findServerById(mysql.serverId as string);
|
||||||
if (server.serverStatus === "inactive") {
|
if (server.serverStatus === "inactive") {
|
||||||
@@ -38,6 +39,7 @@ export const runJobs = async (job: QueueJob) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await runMySqlBackup(mysql, backup);
|
await runMySqlBackup(mysql, backup);
|
||||||
|
await keepLatestNBackups(backup, server.serverId);
|
||||||
} else if (databaseType === "mongo" && mongo) {
|
} else if (databaseType === "mongo" && mongo) {
|
||||||
const server = await findServerById(mongo.serverId as string);
|
const server = await findServerById(mongo.serverId as string);
|
||||||
if (server.serverStatus === "inactive") {
|
if (server.serverStatus === "inactive") {
|
||||||
@@ -45,6 +47,7 @@ export const runJobs = async (job: QueueJob) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await runMongoBackup(mongo, backup);
|
await runMongoBackup(mongo, backup);
|
||||||
|
await keepLatestNBackups(backup, server.serverId);
|
||||||
} else if (databaseType === "mariadb" && mariadb) {
|
} else if (databaseType === "mariadb" && mariadb) {
|
||||||
const server = await findServerById(mariadb.serverId as string);
|
const server = await findServerById(mariadb.serverId as string);
|
||||||
if (server.serverStatus === "inactive") {
|
if (server.serverStatus === "inactive") {
|
||||||
@@ -52,9 +55,8 @@ export const runJobs = async (job: QueueJob) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await runMariadbBackup(mariadb, backup);
|
await runMariadbBackup(mariadb, backup);
|
||||||
|
await keepLatestNBackups(backup, server.serverId);
|
||||||
}
|
}
|
||||||
|
|
||||||
await keepLatestNBackups(backup);
|
|
||||||
}
|
}
|
||||||
if (job.type === "server") {
|
if (job.type === "server") {
|
||||||
const { serverId } = job;
|
const { serverId } = job;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import { runMySqlBackup } from "./mysql";
|
|||||||
import { runPostgresBackup } from "./postgres";
|
import { runPostgresBackup } from "./postgres";
|
||||||
import { findAdmin } from "../../services/admin";
|
import { findAdmin } from "../../services/admin";
|
||||||
import { getS3Credentials } from "./utils";
|
import { getS3Credentials } from "./utils";
|
||||||
import { execAsync } from "../process/execAsync";
|
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
||||||
|
|
||||||
import type { BackupSchedule } from "@dokploy/server/services/backup";
|
import type { BackupSchedule } from "@dokploy/server/services/backup";
|
||||||
import { startLogCleanup } from "../access-log/handler";
|
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
|
// 0 also immediately returns which is good as the empty "keep latest" field in the UI
|
||||||
// is saved as 0 in the database
|
// is saved as 0 in the database
|
||||||
if (!backup.keepLatestCount) return;
|
if (!backup.keepLatestCount) return;
|
||||||
@@ -202,8 +205,11 @@ export const keepLatestNBackups = async (backup: BackupSchedule) => {
|
|||||||
|
|
||||||
const rcloneCommand = `${rcloneList} | ${sortAndPickUnwantedBackups} ${rcloneDelete}`;
|
const rcloneCommand = `${rcloneList} | ${sortAndPickUnwantedBackups} ${rcloneDelete}`;
|
||||||
|
|
||||||
// we can execute this command on any server it doesn't matter
|
if (serverId) {
|
||||||
await execAsync(rcloneCommand);
|
await execAsyncRemote(serverId, rcloneCommand);
|
||||||
|
} else {
|
||||||
|
await execAsync(rcloneCommand);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -13,15 +13,17 @@ export const scheduleBackup = (backup: BackupSchedule) => {
|
|||||||
scheduleJob(backupId, schedule, async () => {
|
scheduleJob(backupId, schedule, async () => {
|
||||||
if (databaseType === "postgres" && postgres) {
|
if (databaseType === "postgres" && postgres) {
|
||||||
await runPostgresBackup(postgres, backup);
|
await runPostgresBackup(postgres, backup);
|
||||||
|
await keepLatestNBackups(backup, postgres.serverId);
|
||||||
} else if (databaseType === "mysql" && mysql) {
|
} else if (databaseType === "mysql" && mysql) {
|
||||||
await runMySqlBackup(mysql, backup);
|
await runMySqlBackup(mysql, backup);
|
||||||
|
await keepLatestNBackups(backup, mysql.serverId);
|
||||||
} else if (databaseType === "mongo" && mongo) {
|
} else if (databaseType === "mongo" && mongo) {
|
||||||
await runMongoBackup(mongo, backup);
|
await runMongoBackup(mongo, backup);
|
||||||
|
await keepLatestNBackups(backup, mongo.serverId);
|
||||||
} else if (databaseType === "mariadb" && mariadb) {
|
} else if (databaseType === "mariadb" && mariadb) {
|
||||||
await runMariadbBackup(mariadb, backup);
|
await runMariadbBackup(mariadb, backup);
|
||||||
|
await keepLatestNBackups(backup, mariadb.serverId);
|
||||||
}
|
}
|
||||||
|
|
||||||
await keepLatestNBackups(backup);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user