Refactor backup and restore utilities for improved container handling

- Removed the `getRemoteServiceContainer` function and updated the `getServiceContainer` function to handle both local and remote service containers more efficiently.
- Refactored backup and restore commands for MariaDB, MongoDB, MySQL, and PostgreSQL to utilize new utility functions for generating backup and restore commands, enhancing code clarity and maintainability.
- Streamlined command execution logic in backup and restore processes, ensuring consistent handling of container IDs across different database types.
This commit is contained in:
Mauricio Siu
2025-05-03 13:24:05 -06:00
parent 0690f07262
commit 89306a7619
9 changed files with 115 additions and 142 deletions
+15 -12
View File
@@ -1,11 +1,9 @@
import type { Destination } from "@dokploy/server/services/destination";
import type { Mariadb } from "@dokploy/server/services/mariadb";
import { getS3Credentials } from "../backups/utils";
import {
getRemoteServiceContainer,
getServiceContainer,
} from "../docker/utils";
import { getServiceContainer } from "../docker/utils";
import { execAsync, execAsyncRemote } from "../process/execAsync";
import { getMariadbRestoreCommand } from "./utils";
export const restoreMariadbBackup = async (
mariadb: Mariadb,
@@ -21,22 +19,27 @@ export const restoreMariadbBackup = async (
const bucketPath = `:s3:${destination.bucket}`;
const backupPath = `${bucketPath}/${backupFile}`;
const { Id: containerName } = serverId
? await getRemoteServiceContainer(serverId, appName)
: await getServiceContainer(appName);
const { Id: containerId } = await getServiceContainer(appName, serverId);
const restoreCommand = `
rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | docker exec -i ${containerName} mariadb -u ${databaseUser} -p${databasePassword} ${database}
const restoreCommand = getMariadbRestoreCommand(
containerId,
database,
databaseUser,
databasePassword || "",
);
const command = `
rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | ${restoreCommand}
`;
emit("Starting restore...");
emit(`Executing command: ${restoreCommand}`);
emit(`Executing command: ${command}`);
if (serverId) {
await execAsyncRemote(serverId, restoreCommand);
await execAsyncRemote(serverId, command);
} else {
await execAsync(restoreCommand);
await execAsync(command);
}
emit("Restore completed successfully!");