mirror of
https://github.com/andwati/dokploy.git
synced 2026-10-28 15:52:16 +00:00
Enhance backup command generation and logging for database backups
- Refactored backup utilities for PostgreSQL, MySQL, MariaDB, and MongoDB to streamline command generation and improve logging. - Introduced a unified `getBackupCommand` function to encapsulate backup command logic, enhancing maintainability. - Improved error handling and logging during backup processes, providing clearer feedback on command execution and success/failure states. - Updated container retrieval methods to return null instead of throwing errors when no containers are found, improving robustness.
This commit is contained in:
@@ -2,21 +2,12 @@ import type { BackupSchedule } from "@dokploy/server/services/backup";
|
||||
import type { Compose } from "@dokploy/server/services/compose";
|
||||
import { findProjectById } from "@dokploy/server/services/project";
|
||||
import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
|
||||
import { execAsyncRemote, execAsyncStream } from "../process/execAsync";
|
||||
import {
|
||||
getMariadbBackupCommand,
|
||||
getMysqlBackupCommand,
|
||||
getMongoBackupCommand,
|
||||
getPostgresBackupCommand,
|
||||
getS3Credentials,
|
||||
normalizeS3Path,
|
||||
} from "./utils";
|
||||
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
||||
import { getS3Credentials, normalizeS3Path, getBackupCommand } from "./utils";
|
||||
import {
|
||||
createDeploymentBackup,
|
||||
updateDeploymentStatus,
|
||||
} from "@dokploy/server/services/deployment";
|
||||
import { createWriteStream } from "node:fs";
|
||||
import { getComposeContainer } from "../docker/utils";
|
||||
|
||||
export const runComposeBackup = async (
|
||||
compose: Compose,
|
||||
@@ -24,7 +15,7 @@ export const runComposeBackup = async (
|
||||
) => {
|
||||
const { projectId, name } = compose;
|
||||
const project = await findProjectById(projectId);
|
||||
const { prefix, database } = backup;
|
||||
const { prefix } = backup;
|
||||
const destination = backup.destination;
|
||||
const backupFileName = `${new Date().toISOString()}.dump.gz`;
|
||||
const bucketDestination = `${normalizeS3Path(prefix)}${backupFileName}`;
|
||||
@@ -37,74 +28,17 @@ export const runComposeBackup = async (
|
||||
try {
|
||||
const rcloneFlags = getS3Credentials(destination);
|
||||
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
|
||||
|
||||
const { Id: containerId } = await getComposeContainer(
|
||||
compose,
|
||||
backup.serviceName || "",
|
||||
);
|
||||
|
||||
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
|
||||
let backupCommand = "";
|
||||
|
||||
if (backup.databaseType === "postgres") {
|
||||
backupCommand = getPostgresBackupCommand(
|
||||
containerId,
|
||||
database,
|
||||
backup.metadata?.postgres?.databaseUser || "",
|
||||
);
|
||||
} else if (backup.databaseType === "mariadb") {
|
||||
backupCommand = getMariadbBackupCommand(
|
||||
containerId,
|
||||
database,
|
||||
backup.metadata?.mariadb?.databaseUser || "",
|
||||
backup.metadata?.mariadb?.databasePassword || "",
|
||||
);
|
||||
} else if (backup.databaseType === "mysql") {
|
||||
backupCommand = getMysqlBackupCommand(
|
||||
containerId,
|
||||
database,
|
||||
backup.metadata?.mysql?.databaseRootPassword || "",
|
||||
);
|
||||
} else if (backup.databaseType === "mongo") {
|
||||
backupCommand = getMongoBackupCommand(
|
||||
containerId,
|
||||
database,
|
||||
backup.metadata?.mongo?.databaseUser || "",
|
||||
backup.metadata?.mongo?.databasePassword || "",
|
||||
);
|
||||
}
|
||||
const backupCommand = getBackupCommand(
|
||||
backup,
|
||||
rcloneCommand,
|
||||
deployment.logPath,
|
||||
);
|
||||
if (compose.serverId) {
|
||||
await execAsyncRemote(
|
||||
compose.serverId,
|
||||
`
|
||||
set -e;
|
||||
echo "Running command." >> ${deployment.logPath};
|
||||
export RCLONE_LOG_LEVEL=DEBUG;
|
||||
${backupCommand} | ${rcloneCommand} >> ${deployment.logPath} 2>> ${deployment.logPath} || {
|
||||
echo "❌ Command failed" >> ${deployment.logPath};
|
||||
exit 1;
|
||||
}
|
||||
echo "✅ Command executed successfully" >> ${deployment.logPath};
|
||||
`,
|
||||
);
|
||||
await execAsyncRemote(compose.serverId, backupCommand);
|
||||
} else {
|
||||
const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
|
||||
await execAsyncStream(
|
||||
`${backupCommand} | ${rcloneCommand}`,
|
||||
(data) => {
|
||||
if (writeStream.writable) {
|
||||
writeStream.write(data);
|
||||
}
|
||||
},
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
RCLONE_LOG_LEVEL: "DEBUG",
|
||||
},
|
||||
},
|
||||
);
|
||||
writeStream.write("Backup done✅");
|
||||
writeStream.end();
|
||||
await execAsync(backupCommand);
|
||||
}
|
||||
|
||||
await sendDatabaseBackupNotifications({
|
||||
|
||||
Reference in New Issue
Block a user