mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
- Updated import statements to maintain consistency and clarity. - Refactored components to enhance readability and organization. - Ensured proper usage of type imports and removed unnecessary comments. - Improved user feedback mechanisms in forms and alerts for better user experience.
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
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 { execAsync, execAsyncRemote } from "../process/execAsync";
|
|
|
|
export const restorePostgresBackup = async (
|
|
postgres: Postgres,
|
|
destination: Destination,
|
|
database: string,
|
|
backupFile: string,
|
|
emit: (log: string) => void,
|
|
) => {
|
|
try {
|
|
const { appName, databaseUser, serverId } = postgres;
|
|
|
|
const rcloneFlags = getS3Credentials(destination);
|
|
const bucketPath = `:s3:${destination.bucket}`;
|
|
|
|
const backupPath = `${bucketPath}/${backupFile}`;
|
|
|
|
const { Id: containerName } = serverId
|
|
? await getRemoteServiceContainer(serverId, appName)
|
|
: await getServiceContainer(appName);
|
|
|
|
emit("Starting restore...");
|
|
emit(`Backup path: ${backupPath}`);
|
|
|
|
const command = `\
|
|
rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip | docker exec -i ${containerName} pg_restore -U ${databaseUser} -d ${database} --clean --if-exists`;
|
|
|
|
emit(`Executing command: ${command}`);
|
|
|
|
if (serverId) {
|
|
const { stdout, stderr } = await execAsyncRemote(serverId, command);
|
|
emit(stdout);
|
|
emit(stderr);
|
|
} else {
|
|
const { stdout, stderr } = await execAsync(command);
|
|
console.log("stdout", stdout);
|
|
console.log("stderr", stderr);
|
|
emit(stdout);
|
|
emit(stderr);
|
|
}
|
|
|
|
emit("Restore completed successfully!");
|
|
} catch (error) {
|
|
emit(
|
|
`Error: ${
|
|
error instanceof Error
|
|
? error.message
|
|
: "Error restoring postgres backup"
|
|
}`,
|
|
);
|
|
throw error;
|
|
}
|
|
};
|