mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
feat(server): function to keep only the latest N backups
This commit is contained in:
@@ -4,10 +4,11 @@ import {
|
|||||||
cleanUpUnusedImages,
|
cleanUpUnusedImages,
|
||||||
findBackupById,
|
findBackupById,
|
||||||
findServerById,
|
findServerById,
|
||||||
|
keepLatestNBackups,
|
||||||
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";
|
||||||
@@ -52,6 +53,8 @@ export const runJobs = async (job: QueueJob) => {
|
|||||||
}
|
}
|
||||||
await runMariadbBackup(mariadb, backup);
|
await runMariadbBackup(mariadb, backup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await keepLatestNBackups(backup);
|
||||||
}
|
}
|
||||||
if (job.type === "server") {
|
if (job.type === "server") {
|
||||||
const { serverId } = job;
|
const { serverId } = job;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import path from "node:path";
|
||||||
import { getAllServers } from "@dokploy/server/services/server";
|
import { getAllServers } from "@dokploy/server/services/server";
|
||||||
import { scheduleJob } from "node-schedule";
|
import { scheduleJob } from "node-schedule";
|
||||||
import { db } from "../../db/index";
|
import { db } from "../../db/index";
|
||||||
@@ -12,6 +13,10 @@ import { runMongoBackup } from "./mongo";
|
|||||||
import { runMySqlBackup } from "./mysql";
|
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 { execAsync } from "../process/execAsync";
|
||||||
|
|
||||||
|
import type { BackupSchedule } from "@dokploy/server/services/backup";
|
||||||
|
|
||||||
export const initCronJobs = async () => {
|
export const initCronJobs = async () => {
|
||||||
console.log("Setting up cron jobs....");
|
console.log("Setting up cron jobs....");
|
||||||
@@ -169,3 +174,30 @@ export const initCronJobs = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const keepLatestNBackups = async (backup: BackupSchedule) => {
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const rcloneFlags = getS3Credentials(backup.destination);
|
||||||
|
const backupFilesPath = path.join(`:s3:${backup.destination.bucket}`, backup.prefix);
|
||||||
|
|
||||||
|
// --include "*.sql.gz" ensures nothing else other than the db backup files are touched by rclone
|
||||||
|
const rcloneList = `rclone lsf ${rcloneFlags.join(" ")} --include "*.sql.gz" ${backupFilesPath}`;
|
||||||
|
// when we pipe the above command with this one, we only get the list of files we want to delete
|
||||||
|
const sortAndPickUnwantedBackups = `sort -r | tail -n +$((${backup.keepLatestCount}+1)) | xargs -I{}`;
|
||||||
|
// this command deletes the files
|
||||||
|
// to test the deletion before actually deleting we can add --dry-run before ${backupFilesPath}/{}
|
||||||
|
const rcloneDelete = `rclone delete ${rcloneFlags.join(" ")} ${backupFilesPath}/{}`;
|
||||||
|
|
||||||
|
const rcloneCommand = `${rcloneList} | ${sortAndPickUnwantedBackups} ${rcloneDelete}`;
|
||||||
|
|
||||||
|
// we can execute this command on any server it doesn't matter
|
||||||
|
await execAsync(rcloneCommand);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { runMariadbBackup } from "./mariadb";
|
|||||||
import { runMongoBackup } from "./mongo";
|
import { runMongoBackup } from "./mongo";
|
||||||
import { runMySqlBackup } from "./mysql";
|
import { runMySqlBackup } from "./mysql";
|
||||||
import { runPostgresBackup } from "./postgres";
|
import { runPostgresBackup } from "./postgres";
|
||||||
|
import { keepLatestNBackups } from ".";
|
||||||
|
|
||||||
export const scheduleBackup = (backup: BackupSchedule) => {
|
export const scheduleBackup = (backup: BackupSchedule) => {
|
||||||
const { schedule, backupId, databaseType, postgres, mysql, mongo, mariadb } =
|
const { schedule, backupId, databaseType, postgres, mysql, mongo, mariadb } =
|
||||||
@@ -19,6 +20,8 @@ export const scheduleBackup = (backup: BackupSchedule) => {
|
|||||||
} else if (databaseType === "mariadb" && mariadb) {
|
} else if (databaseType === "mariadb" && mariadb) {
|
||||||
await runMariadbBackup(mariadb, backup);
|
await runMariadbBackup(mariadb, backup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await keepLatestNBackups(backup);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user