mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-04 03:02: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:
@@ -1,33 +1,131 @@
|
||||
import {
|
||||
getComposeContainerCommand,
|
||||
getServiceContainerCommand,
|
||||
} from "../backups/utils";
|
||||
|
||||
export const getPostgresRestoreCommand = (
|
||||
containerId: string,
|
||||
database: string,
|
||||
databaseUser: string,
|
||||
) => {
|
||||
return `docker exec -i ${containerId} sh -c "pg_restore -U ${databaseUser} -d ${database} --clean --if-exists"`;
|
||||
return `docker exec -i $CONTAINER_ID sh -c "pg_restore -U ${databaseUser} -d ${database} --clean --if-exists"`;
|
||||
};
|
||||
|
||||
export const getMariadbRestoreCommand = (
|
||||
containerId: string,
|
||||
database: string,
|
||||
databaseUser: string,
|
||||
databasePassword: string,
|
||||
) => {
|
||||
return `docker exec -i ${containerId} sh -c "mariadb -u ${databaseUser} -p${databasePassword} ${database}"`;
|
||||
return `docker exec -i $CONTAINER_ID sh -c "mariadb -u ${databaseUser} -p${databasePassword} ${database}"`;
|
||||
};
|
||||
|
||||
export const getMysqlRestoreCommand = (
|
||||
containerId: string,
|
||||
database: string,
|
||||
databasePassword: string,
|
||||
) => {
|
||||
return `docker exec -i ${containerId} sh -c "mysql -u root -p${databasePassword} ${database}"`;
|
||||
return `docker exec -i $CONTAINER_ID sh -c "mysql -u root -p${databasePassword} ${database}"`;
|
||||
};
|
||||
|
||||
export const getMongoRestoreCommand = (
|
||||
containerId: string,
|
||||
database: string,
|
||||
databaseUser: string,
|
||||
databasePassword: string,
|
||||
) => {
|
||||
return `docker exec -i ${containerId} sh -c "mongorestore --username ${databaseUser} --password ${databasePassword} --authenticationDatabase admin --db ${database} --archive"`;
|
||||
return `docker exec -i $CONTAINER_ID sh -c "mongorestore --username ${databaseUser} --password ${databasePassword} --authenticationDatabase admin --db ${database} --archive"`;
|
||||
};
|
||||
|
||||
export const getComposeSearchCommand = (
|
||||
appName: string,
|
||||
type: "stack" | "docker-compose" | "database",
|
||||
serviceName?: string,
|
||||
) => {
|
||||
if (type === "database") {
|
||||
return getServiceContainerCommand(appName || "");
|
||||
}
|
||||
return getComposeContainerCommand(appName || "", serviceName || "", type);
|
||||
};
|
||||
|
||||
interface DatabaseCredentials {
|
||||
database: string;
|
||||
databaseUser?: string;
|
||||
databasePassword?: string;
|
||||
}
|
||||
|
||||
const generateRestoreCommand = (
|
||||
type: "postgres" | "mariadb" | "mysql" | "mongo",
|
||||
credentials: DatabaseCredentials,
|
||||
) => {
|
||||
const { database, databaseUser, databasePassword } = credentials;
|
||||
switch (type) {
|
||||
case "postgres":
|
||||
return getPostgresRestoreCommand(database, databaseUser || "");
|
||||
case "mariadb":
|
||||
return getMariadbRestoreCommand(
|
||||
database,
|
||||
databaseUser || "",
|
||||
databasePassword || "",
|
||||
);
|
||||
case "mysql":
|
||||
return getMysqlRestoreCommand(database, databasePassword || "");
|
||||
case "mongo":
|
||||
return getMongoRestoreCommand(
|
||||
database,
|
||||
databaseUser || "",
|
||||
databasePassword || "",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const getMongoSpecificCommand = (
|
||||
rcloneCommand: string,
|
||||
restoreCommand: string,
|
||||
backupFile: string,
|
||||
): string => {
|
||||
const tempDir = "/tmp/dokploy-restore";
|
||||
const fileName = backupFile.split("/").pop() || "backup.dump.gz";
|
||||
const decompressedName = fileName.replace(".gz", "");
|
||||
return `
|
||||
rm -rf ${tempDir} && \
|
||||
mkdir -p ${tempDir} && \
|
||||
${rcloneCommand} ${tempDir} && \
|
||||
cd ${tempDir} && \
|
||||
gunzip -f "${fileName}" && \
|
||||
${restoreCommand} < "${decompressedName}" && \
|
||||
rm -rf ${tempDir}
|
||||
`;
|
||||
};
|
||||
|
||||
interface RestoreOptions {
|
||||
appName: string;
|
||||
type: "postgres" | "mariadb" | "mysql" | "mongo";
|
||||
restoreType: "stack" | "docker-compose" | "database";
|
||||
credentials: DatabaseCredentials;
|
||||
serviceName?: string;
|
||||
rcloneCommand: string;
|
||||
backupFile?: string;
|
||||
}
|
||||
|
||||
export const getRestoreCommand = ({
|
||||
appName,
|
||||
type,
|
||||
restoreType,
|
||||
credentials,
|
||||
serviceName,
|
||||
rcloneCommand,
|
||||
backupFile,
|
||||
}: RestoreOptions) => {
|
||||
const containerSearch = getComposeSearchCommand(
|
||||
appName,
|
||||
restoreType,
|
||||
serviceName,
|
||||
);
|
||||
const restoreCommand = generateRestoreCommand(type, credentials);
|
||||
let cmd = `CONTAINER_ID=$(${containerSearch})`;
|
||||
|
||||
if (type !== "mongo") {
|
||||
cmd += ` && ${rcloneCommand} | ${restoreCommand}`;
|
||||
} else {
|
||||
cmd += ` && ${getMongoSpecificCommand(rcloneCommand, restoreCommand, backupFile || "")}`;
|
||||
}
|
||||
|
||||
return cmd;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user