mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
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:
@@ -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!");
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import type { Destination } from "@dokploy/server/services/destination";
|
||||
import type { Mongo } from "@dokploy/server/services/mongo";
|
||||
import { getS3Credentials } from "../backups/utils";
|
||||
import {
|
||||
getRemoteServiceContainer,
|
||||
getServiceContainer,
|
||||
} from "../docker/utils";
|
||||
import { getServiceContainer } from "../docker/utils";
|
||||
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
||||
import { getMongoRestoreCommand } from "./utils";
|
||||
|
||||
export const restoreMongoBackup = async (
|
||||
mongo: Mongo,
|
||||
@@ -21,14 +19,18 @@ export const restoreMongoBackup = 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);
|
||||
|
||||
// For MongoDB, we need to first download the backup file since mongorestore expects a directory
|
||||
const tempDir = "/tmp/dokploy-restore";
|
||||
const fileName = backupFile.split("/").pop() || "backup.dump.gz";
|
||||
const decompressedName = fileName.replace(".gz", "");
|
||||
const restoreCommand = getMongoRestoreCommand(
|
||||
containerId,
|
||||
database,
|
||||
databaseUser,
|
||||
databasePassword || "",
|
||||
);
|
||||
|
||||
const downloadCommand = `\
|
||||
rm -rf ${tempDir} && \
|
||||
@@ -36,7 +38,7 @@ mkdir -p ${tempDir} && \
|
||||
rclone copy ${rcloneFlags.join(" ")} "${backupPath}" ${tempDir} && \
|
||||
cd ${tempDir} && \
|
||||
gunzip -f "${fileName}" && \
|
||||
docker exec -i ${containerName} mongorestore --username ${databaseUser} --password ${databasePassword} --authenticationDatabase admin --db ${database} --archive < "${decompressedName}" && \
|
||||
${restoreCommand} < "${decompressedName}" && \
|
||||
rm -rf ${tempDir}`;
|
||||
|
||||
emit("Starting restore...");
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import type { Destination } from "@dokploy/server/services/destination";
|
||||
import type { MySql } from "@dokploy/server/services/mysql";
|
||||
import { getS3Credentials } from "../backups/utils";
|
||||
import {
|
||||
getRemoteServiceContainer,
|
||||
getServiceContainer,
|
||||
} from "../docker/utils";
|
||||
import { getServiceContainer } from "../docker/utils";
|
||||
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
||||
import { getMysqlRestoreCommand } from "./utils";
|
||||
|
||||
export const restoreMySqlBackup = async (
|
||||
mysql: MySql,
|
||||
@@ -21,22 +19,26 @@ export const restoreMySqlBackup = 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} mysql -u root -p${databaseRootPassword} ${database}
|
||||
const restoreCommand = getMysqlRestoreCommand(
|
||||
containerId,
|
||||
database,
|
||||
databaseRootPassword || "",
|
||||
);
|
||||
|
||||
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!");
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import type { Destination } from "@dokploy/server/services/destination";
|
||||
import type { Postgres } from "@dokploy/server/services/postgres";
|
||||
import { getS3Credentials } from "../backups/utils";
|
||||
import {
|
||||
getRemoteServiceContainer,
|
||||
getServiceContainer,
|
||||
} from "../docker/utils";
|
||||
import { getServiceContainer } from "../docker/utils";
|
||||
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
||||
import { getPostgresRestoreCommand } from "./utils";
|
||||
|
||||
export const restorePostgresBackup = async (
|
||||
postgres: Postgres,
|
||||
@@ -22,15 +20,19 @@ export const restorePostgresBackup = async (
|
||||
|
||||
const backupPath = `${bucketPath}/${backupFile}`;
|
||||
|
||||
const { Id: containerName } = serverId
|
||||
? await getRemoteServiceContainer(serverId, appName)
|
||||
: await getServiceContainer(appName);
|
||||
const { Id: containerId } = await getServiceContainer(appName, serverId);
|
||||
|
||||
emit("Starting restore...");
|
||||
emit(`Backup path: ${backupPath}`);
|
||||
|
||||
const restoreCommand = getPostgresRestoreCommand(
|
||||
containerId,
|
||||
database,
|
||||
databaseUser,
|
||||
);
|
||||
|
||||
const command = `\
|
||||
rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | docker exec -i ${containerName} pg_restore -U ${databaseUser} -d ${database} --clean --if-exists`;
|
||||
rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | ${restoreCommand}`;
|
||||
|
||||
emit(`Executing command: ${command}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user