refactor(multi-server): add rclone to multi server

This commit is contained in:
Mauricio Siu
2024-09-18 00:40:52 -06:00
parent f001a50278
commit a46e7759b2
18 changed files with 368 additions and 32 deletions

View File

@@ -5,7 +5,7 @@ import type { Postgres } from "@/server/api/services/postgres";
import { findProjectById } from "@/server/api/services/project";
import { getServiceContainer } from "../docker/utils";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { execAsync } from "../process/execAsync";
import { execAsync, execAsyncRemote } from "../process/execAsync";
import { uploadToS3 } from "./utils";
export const runPostgresBackup = async (
@@ -57,3 +57,57 @@ export const runPostgresBackup = async (
// Restore
// /Applications/pgAdmin 4.app/Contents/SharedSupport/pg_restore --host "localhost" --port "5432" --username "mauricio" --no-password --dbname "postgres" --verbose "/Users/mauricio/Downloads/_databases_2024-04-12T07_02_05.234Z.sql"
export const runRemotePostgresBackup = async (
postgres: Postgres,
backup: BackupSchedule,
) => {
const { appName, databaseUser, name, projectId } = postgres;
const project = await findProjectById(projectId);
const { prefix, database } = backup;
const destination = backup.destination;
const backupFileName = `${new Date().toISOString()}.sql.gz`;
const bucketDestination = path.join(prefix, backupFileName);
const containerPath = `/backup/${backupFileName}`;
const hostPath = `./${backupFileName}`;
try {
const { Id: containerId } = await getServiceContainer(appName);
const pgDumpCommand = `pg_dump -Fc --no-acl --no-owner -h localhost -U ${databaseUser} --no-password '${database}' | gzip`;
// const rcloneCommand = `rclone rcat --buffer-size 16M ${rcloneDestination}`;
// const command = `
// // docker exec ${containerId} /bin/bash -c "${pgDumpCommand} | ${rcloneCommand}"
// `;
await execAsyncRemote(
postgres.serverId,
`docker exec ${containerId} /bin/bash -c "rm -rf /backup && mkdir -p /backup"`,
);
// await execAsync(
// `docker exec ${containerId} sh -c "pg_dump -Fc --no-acl --no-owner -h localhost -U ${databaseUser} --no-password '${database}' | gzip > ${containerPath}"`,
// );
await execAsync(`docker cp ${containerId}:${containerPath} ${hostPath}`);
await uploadToS3(destination, bucketDestination, hostPath);
await sendDatabaseBackupNotifications({
applicationName: name,
projectName: project.name,
databaseType: "postgres",
type: "success",
});
} catch (error) {
await sendDatabaseBackupNotifications({
applicationName: name,
projectName: project.name,
databaseType: "postgres",
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
});
throw error;
} finally {
await unlink(hostPath);
}
};

View File

@@ -1,5 +1,3 @@
import type { Mongo } from "@/server/api/services/mongo";
import type { Mount } from "@/server/api/services/mount";
import type { CreateServiceOptions } from "dockerode";
import {
calculateResources,

View File

@@ -15,10 +15,12 @@ export const execAsyncRemote = async (
const keys = await readSSHKey(server.sshKeyId);
const conn = new Client();
let stdout = "";
let stderr = "";
return new Promise((resolve, reject) => {
const conn = new Client();
sleep(1000);
conn
.once("ready", () => {
console.log("Client :: ready");
@@ -57,3 +59,7 @@ export const execAsyncRemote = async (
});
});
};
export const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

View File

@@ -74,6 +74,7 @@ const connectToServer = async (serverId: string, logPath: string) => {
command_exists() {
command -v "$@" > /dev/null 2>&1
}
${installRClone()}
${installDocker()}
${setupSwarm()}
${setupNetwork()}
@@ -235,6 +236,10 @@ export const createDefaultMiddlewares = () => {
return command;
};
export const installRClone = () => `
curl https://rclone.org/install.sh | sudo bash
`;
export const createTraefikInstance = () => {
const command = `
# Check if dokpyloy-traefik exists