mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-11 17:05:25 +02:00
- 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.
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import type { Destination } from "@dokploy/server/services/destination";
|
|
import type { MySql } from "@dokploy/server/services/mysql";
|
|
import { getS3Credentials } from "../backups/utils";
|
|
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
|
import { getRestoreCommand } from "./utils";
|
|
import type { apiRestoreBackup } from "@dokploy/server/db/schema";
|
|
import type { z } from "zod";
|
|
|
|
export const restoreMySqlBackup = async (
|
|
mysql: MySql,
|
|
destination: Destination,
|
|
backupInput: z.infer<typeof apiRestoreBackup>,
|
|
emit: (log: string) => void,
|
|
) => {
|
|
try {
|
|
const { appName, databaseRootPassword, serverId } = mysql;
|
|
|
|
const rcloneFlags = getS3Credentials(destination);
|
|
const bucketPath = `:s3:${destination.bucket}`;
|
|
const backupPath = `${bucketPath}/${backupInput.backupFile}`;
|
|
|
|
const rcloneCommand = `rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip`;
|
|
|
|
const command = getRestoreCommand({
|
|
appName,
|
|
type: "mysql",
|
|
credentials: {
|
|
database: backupInput.databaseName,
|
|
databasePassword: databaseRootPassword,
|
|
},
|
|
restoreType: "database",
|
|
rcloneCommand,
|
|
});
|
|
|
|
emit("Starting restore...");
|
|
|
|
emit(`Executing command: ${command}`);
|
|
|
|
if (serverId) {
|
|
await execAsyncRemote(serverId, command);
|
|
} else {
|
|
await execAsync(command);
|
|
}
|
|
|
|
emit("Restore completed successfully!");
|
|
} catch (error) {
|
|
console.error(error);
|
|
emit(
|
|
`Error: ${
|
|
error instanceof Error ? error.message : "Error restoring mysql backup"
|
|
}`,
|
|
);
|
|
throw new Error(
|
|
error instanceof Error ? error.message : "Error restoring mysql backup",
|
|
);
|
|
}
|
|
};
|