Implement enhanced backup logging and deployment tracking for database backups

- Refactored backup utilities for MariaDB, MongoDB, MySQL, and PostgreSQL to include detailed logging of command execution, improving traceability and debugging.
- Introduced deployment tracking for backups, allowing for better management of backup statuses and notifications.
- Updated backup command execution to utilize writable streams, ensuring efficient data handling during backup processes.
- Enhanced error handling and logging for backup operations, providing clearer feedback on success or failure.
This commit is contained in:
Mauricio Siu
2025-05-03 13:33:37 -06:00
parent 89306a7619
commit a8159e5f99
6 changed files with 201 additions and 24 deletions
+50 -7
View File
@@ -3,8 +3,17 @@ import type { Mariadb } from "@dokploy/server/services/mariadb";
import { findProjectById } from "@dokploy/server/services/project";
import { getServiceContainer } from "../docker/utils";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { execAsync, execAsyncRemote } from "../process/execAsync";
import { getS3Credentials, normalizeS3Path } from "./utils";
import { execAsyncRemote, execAsyncStream } from "../process/execAsync";
import {
getMariadbBackupCommand,
getS3Credentials,
normalizeS3Path,
} from "./utils";
import {
createDeploymentBackup,
updateDeploymentStatus,
} from "@dokploy/server/services/deployment";
import { createWriteStream } from "node:fs";
export const runMariadbBackup = async (
mariadb: Mariadb,
@@ -16,7 +25,11 @@ export const runMariadbBackup = async (
const destination = backup.destination;
const backupFileName = `${new Date().toISOString()}.sql.gz`;
const bucketDestination = `${normalizeS3Path(prefix)}${backupFileName}`;
const deployment = await createDeploymentBackup({
backupId: backup.backupId,
title: "MariaDB Backup",
description: "MariaDB Backup",
});
try {
const rcloneFlags = getS3Credentials(destination);
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
@@ -27,15 +40,43 @@ export const runMariadbBackup = async (
);
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
const command = getMariadbBackupCommand(
containerId,
database,
databaseUser,
databasePassword || "",
);
if (mariadb.serverId) {
const mariadbDumpCommand = `docker exec ${containerId} sh -c "mariadb-dump --user='${databaseUser}' --password='${databasePassword}' --databases ${database} | gzip"`;
await execAsyncRemote(
mariadb.serverId,
`${mariadbDumpCommand} | ${rcloneCommand}`,
`
set -e;
echo "Running command." >> ${deployment.logPath};
export RCLONE_LOG_LEVEL=DEBUG;
${command} | ${rcloneCommand} >> ${deployment.logPath} 2>> ${deployment.logPath} || {
echo "❌ Command failed" >> ${deployment.logPath};
exit 1;
}
echo "✅ Command executed successfully" >> ${deployment.logPath};
`,
);
} else {
const mariadbDumpCommand = `docker exec ${containerId} sh -c "mariadb-dump --user='${databaseUser}' --password='${databasePassword}' --databases ${database} | gzip"`;
await execAsync(`${mariadbDumpCommand} | ${rcloneCommand}`);
const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
await execAsyncStream(
`${command} | ${rcloneCommand}`,
(data) => {
if (writeStream.writable) {
writeStream.write(data);
}
},
{
env: {
...process.env,
RCLONE_LOG_LEVEL: "DEBUG",
},
},
);
}
await sendDatabaseBackupNotifications({
@@ -45,6 +86,7 @@ export const runMariadbBackup = async (
type: "success",
organizationId: project.organizationId,
});
await updateDeploymentStatus(deployment.deploymentId, "done");
} catch (error) {
console.log(error);
await sendDatabaseBackupNotifications({
@@ -56,6 +98,7 @@ export const runMariadbBackup = async (
errorMessage: error?.message || "Error message not provided",
organizationId: project.organizationId,
});
await updateDeploymentStatus(deployment.deploymentId, "error");
throw error;
}
};