mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-04 11:12:16 +00:00
Implement restore functionality for various database types
- Added `apiRestoreBackup` schema to define input requirements for restore operations. - Refactored restore utilities for PostgreSQL, MySQL, MariaDB, and MongoDB to utilize a unified command generation approach, enhancing maintainability. - Improved logging during restore processes to provide clearer feedback on command execution and success/failure states. - Streamlined the handling of database credentials and backup file paths across different database types, ensuring consistency and reducing redundancy.
This commit is contained in:
@@ -2,76 +2,84 @@ import type { Destination } from "@dokploy/server/services/destination";
|
||||
import type { Compose } from "@dokploy/server/services/compose";
|
||||
import { getS3Credentials } from "../backups/utils";
|
||||
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
||||
import type { Backup } from "@dokploy/server/services/backup";
|
||||
import { getComposeContainer } from "../docker/utils";
|
||||
import {
|
||||
getMariadbRestoreCommand,
|
||||
getMongoRestoreCommand,
|
||||
getMysqlRestoreCommand,
|
||||
getPostgresRestoreCommand,
|
||||
} from "./utils";
|
||||
import { getRestoreCommand } from "./utils";
|
||||
import type { apiRestoreBackup } from "@dokploy/server/db/schema";
|
||||
import type { z } from "zod";
|
||||
|
||||
interface DatabaseCredentials {
|
||||
databaseUser?: string;
|
||||
databasePassword?: string;
|
||||
}
|
||||
|
||||
export const restoreComposeBackup = async (
|
||||
compose: Compose,
|
||||
destination: Destination,
|
||||
database: string,
|
||||
backupFile: string,
|
||||
metadata: Backup["metadata"] & { serviceName: string },
|
||||
backupInput: z.infer<typeof apiRestoreBackup>,
|
||||
emit: (log: string) => void,
|
||||
) => {
|
||||
try {
|
||||
const { serverId } = compose;
|
||||
if (backupInput.databaseType === "web-server") {
|
||||
return;
|
||||
}
|
||||
const { serverId, appName, composeType } = compose;
|
||||
|
||||
const rcloneFlags = getS3Credentials(destination);
|
||||
const bucketPath = `:s3:${destination.bucket}`;
|
||||
const backupPath = `${bucketPath}/${backupFile}`;
|
||||
const backupPath = `${bucketPath}/${backupInput.backupFile}`;
|
||||
let rcloneCommand = `rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip`;
|
||||
|
||||
const { Id: containerId } = await getComposeContainer(
|
||||
compose,
|
||||
metadata.serviceName || "",
|
||||
);
|
||||
let restoreCommand = "";
|
||||
|
||||
if (metadata.postgres) {
|
||||
restoreCommand = `rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | ${getPostgresRestoreCommand(containerId, database, metadata.postgres.databaseUser)}`;
|
||||
} else if (metadata.mariadb) {
|
||||
restoreCommand = `
|
||||
rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | ${getMariadbRestoreCommand(containerId, database, metadata.mariadb.databaseUser, metadata.mariadb.databasePassword)}
|
||||
`;
|
||||
} else if (metadata.mysql) {
|
||||
restoreCommand = `
|
||||
rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | ${getMysqlRestoreCommand(containerId, database, metadata.mysql.databaseRootPassword)}
|
||||
`;
|
||||
} else if (metadata.mongo) {
|
||||
const tempDir = "/tmp/dokploy-restore";
|
||||
const fileName = backupFile.split("/").pop() || "backup.dump.gz";
|
||||
const decompressedName = fileName.replace(".gz", "");
|
||||
restoreCommand = `\
|
||||
rm -rf ${tempDir} && \
|
||||
mkdir -p ${tempDir} && \
|
||||
rclone copy ${rcloneFlags.join(" ")} "${backupPath}" ${tempDir} && \
|
||||
cd ${tempDir} && \
|
||||
gunzip -f "${fileName}" && \
|
||||
${getMongoRestoreCommand(containerId, database, metadata.mongo.databaseUser, metadata.mongo.databasePassword)} < "${decompressedName}" && \
|
||||
rm -rf ${tempDir}`;
|
||||
if (backupInput.metadata?.mongo) {
|
||||
rcloneCommand = `rclone copy ${rcloneFlags.join(" ")} "${backupPath}"`;
|
||||
}
|
||||
|
||||
let credentials: DatabaseCredentials;
|
||||
|
||||
switch (backupInput.databaseType) {
|
||||
case "postgres":
|
||||
credentials = {
|
||||
databaseUser: backupInput.metadata?.postgres?.databaseUser,
|
||||
};
|
||||
break;
|
||||
case "mariadb":
|
||||
credentials = {
|
||||
databaseUser: backupInput.metadata?.mariadb?.databaseUser,
|
||||
databasePassword: backupInput.metadata?.mariadb?.databasePassword,
|
||||
};
|
||||
break;
|
||||
case "mysql":
|
||||
credentials = {
|
||||
databasePassword: backupInput.metadata?.mysql?.databaseRootPassword,
|
||||
};
|
||||
break;
|
||||
case "mongo":
|
||||
credentials = {
|
||||
databaseUser: backupInput.metadata?.mongo?.databaseUser,
|
||||
databasePassword: backupInput.metadata?.mongo?.databasePassword,
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
const restoreCommand = getRestoreCommand({
|
||||
appName: appName,
|
||||
serviceName: backupInput.metadata?.serviceName,
|
||||
type: backupInput.databaseType,
|
||||
credentials: {
|
||||
database: backupInput.databaseName,
|
||||
...credentials,
|
||||
},
|
||||
restoreType: composeType,
|
||||
rcloneCommand,
|
||||
});
|
||||
|
||||
emit("Starting restore...");
|
||||
emit(`Backup path: ${backupPath}`);
|
||||
|
||||
emit(`Executing command: ${restoreCommand}`);
|
||||
|
||||
if (serverId) {
|
||||
const { stdout, stderr } = await execAsyncRemote(
|
||||
serverId,
|
||||
restoreCommand,
|
||||
);
|
||||
emit(stdout);
|
||||
emit(stderr);
|
||||
await execAsyncRemote(serverId, restoreCommand);
|
||||
} else {
|
||||
const { stdout, stderr } = await execAsync(restoreCommand);
|
||||
emit(stdout);
|
||||
emit(stderr);
|
||||
await execAsync(restoreCommand);
|
||||
}
|
||||
|
||||
emit("Restore completed successfully!");
|
||||
|
||||
Reference in New Issue
Block a user