diff --git a/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx b/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx index f43ba51ca..ea42ac45a 100644 --- a/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx +++ b/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx @@ -49,6 +49,7 @@ const notificationBaseSchema = z.object({ appDeploy: z.boolean().default(false), appBuildError: z.boolean().default(false), databaseBackup: z.boolean().default(false), + volumeBackup: z.boolean().default(false), dokployRestart: z.boolean().default(false), dockerCleanup: z.boolean().default(false), serverThreshold: z.boolean().default(false), @@ -221,6 +222,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: notification.appDeploy, dokployRestart: notification.dokployRestart, databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, dockerCleanup: notification.dockerCleanup, webhookUrl: notification.slack?.webhookUrl, channel: notification.slack?.channel || "", @@ -234,6 +236,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: notification.appDeploy, dokployRestart: notification.dokployRestart, databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, botToken: notification.telegram?.botToken, messageThreadId: notification.telegram?.messageThreadId || "", chatId: notification.telegram?.chatId, @@ -248,6 +251,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: notification.appDeploy, dokployRestart: notification.dokployRestart, databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, type: notification.notificationType, webhookUrl: notification.discord?.webhookUrl, decoration: notification.discord?.decoration || undefined, @@ -261,6 +265,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: notification.appDeploy, dokployRestart: notification.dokployRestart, databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, type: notification.notificationType, smtpServer: notification.email?.smtpServer, smtpPort: notification.email?.smtpPort, @@ -278,6 +283,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: notification.appDeploy, dokployRestart: notification.dokployRestart, databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, type: notification.notificationType, appToken: notification.gotify?.appToken, decoration: notification.gotify?.decoration || undefined, @@ -292,6 +298,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: notification.appDeploy, dokployRestart: notification.dokployRestart, databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, type: notification.notificationType, accessToken: notification.ntfy?.accessToken, topic: notification.ntfy?.topic, @@ -321,6 +328,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy, dokployRestart, databaseBackup, + volumeBackup, dockerCleanup, serverThreshold, } = data; @@ -331,6 +339,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: appDeploy, dokployRestart: dokployRestart, databaseBackup: databaseBackup, + volumeBackup: volumeBackup, webhookUrl: data.webhookUrl, channel: data.channel, name: data.name, @@ -345,6 +354,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: appDeploy, dokployRestart: dokployRestart, databaseBackup: databaseBackup, + volumeBackup: volumeBackup, botToken: data.botToken, messageThreadId: data.messageThreadId || "", chatId: data.chatId, @@ -360,6 +370,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: appDeploy, dokployRestart: dokployRestart, databaseBackup: databaseBackup, + volumeBackup: volumeBackup, webhookUrl: data.webhookUrl, decoration: data.decoration, name: data.name, @@ -374,6 +385,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: appDeploy, dokployRestart: dokployRestart, databaseBackup: databaseBackup, + volumeBackup: volumeBackup, smtpServer: data.smtpServer, smtpPort: data.smtpPort, username: data.username, @@ -392,6 +404,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: appDeploy, dokployRestart: dokployRestart, databaseBackup: databaseBackup, + volumeBackup: volumeBackup, serverUrl: data.serverUrl, appToken: data.appToken, priority: data.priority, @@ -407,6 +420,7 @@ export const HandleNotifications = ({ notificationId }: Props) => { appDeploy: appDeploy, dokployRestart: dokployRestart, databaseBackup: databaseBackup, + volumeBackup: volumeBackup, serverUrl: data.serverUrl, accessToken: data.accessToken, topic: data.topic, @@ -1072,6 +1086,27 @@ export const HandleNotifications = ({ notificationId }: Props) => { )} /> + ( + +
+ Volume Backup + + Trigger the action when a volume backup is created. + +
+ + + +
+ )} + /> + { const volumeBackup = await db.query.volumeBackups.findFirst({ where: eq(volumeBackups.volumeBackupId, volumeBackupId), with: { - application: true, - postgres: true, - mysql: true, - mariadb: true, - mongo: true, - redis: true, - compose: true, + application: { + with: { + environment: { + with: { + project: true, + }, + }, + }, + }, + postgres: { + with: { + environment: { + with: { + project: true, + }, + }, + }, + }, + mysql: { + with: { + environment: { + with: { + project: true, + }, + }, + }, + }, + mariadb: { + with: { + environment: { + with: { + project: true, + }, + }, + }, + }, + mongo: { + with: { + environment: { + with: { + project: true, + }, + }, + }, + }, + redis: { + with: { + environment: { + with: { + project: true, + }, + }, + }, + }, + compose: { + with: { + environment: { + with: { + project: true, + }, + }, + }, + }, destination: true, }, }); diff --git a/packages/server/src/utils/notifications/volume-backup.ts b/packages/server/src/utils/notifications/volume-backup.ts new file mode 100644 index 000000000..f95ecbf39 --- /dev/null +++ b/packages/server/src/utils/notifications/volume-backup.ts @@ -0,0 +1,265 @@ +import { db } from "@dokploy/server/db"; +import { notifications } from "@dokploy/server/db/schema"; +import { renderAsync } from "@react-email/components"; +import { format } from "date-fns"; +import { and, eq } from "drizzle-orm"; +import { + sendDiscordNotification, + sendEmailNotification, + sendGotifyNotification, + sendNtfyNotification, + sendSlackNotification, + sendTelegramNotification, +} from "./utils"; + +export const sendVolumeBackupNotifications = async ({ + projectName, + applicationName, + volumeName, + serviceType, + type, + errorMessage, + organizationId, + backupSize, +}: { + projectName: string; + applicationName: string; + volumeName: string; + serviceType: "application" | "postgres" | "mysql" | "mongodb" | "mariadb" | "redis" | "compose"; + type: "error" | "success"; + organizationId: string; + errorMessage?: string; + backupSize?: string; +}) => { + const date = new Date(); + const unixDate = ~~(Number(date) / 1000); + const notificationList = await db.query.notifications.findMany({ + where: and( + eq(notifications.volumeBackup, true), + eq(notifications.organizationId, organizationId), + ), + with: { + email: true, + discord: true, + telegram: true, + slack: true, + gotify: true, + ntfy: true, + }, + }); + + for (const notification of notificationList) { + const { email, discord, telegram, slack, gotify, ntfy } = notification; + + if (email) { + const subject = `Volume Backup ${type === "success" ? "Successful" : "Failed"} - ${applicationName}`; + const htmlContent = ` +
+

+ ${type === "success" ? "✅" : "❌"} Volume Backup ${type === "success" ? "Successful" : "Failed"} +

+
+

Project: ${projectName}

+

Application: ${applicationName}

+

Volume Name: ${volumeName}

+

Service Type: ${serviceType}

+ ${backupSize ? `

Backup Size: ${backupSize}

` : ""} +

Date: ${date.toLocaleString()}

+ ${type === "error" && errorMessage ? `

Error: ${errorMessage}

` : ""} +
+

+ This notification was sent by Dokploy Volume Backup System. +

+
+ `; + await sendEmailNotification(email, subject, htmlContent); + } + + if (discord) { + const decorate = (decoration: string, text: string) => + `${discord.decoration ? decoration : ""} ${text}`.trim(); + + await sendDiscordNotification(discord, { + title: + type === "success" + ? decorate(">", "`✅` Volume Backup Successful") + : decorate(">", "`❌` Volume Backup Failed"), + color: type === "success" ? 0x57f287 : 0xed4245, + fields: [ + { + name: decorate("`🛠️`", "Project"), + value: projectName, + inline: true, + }, + { + name: decorate("`⚙️`", "Application"), + value: applicationName, + inline: true, + }, + { + name: decorate("`💾`", "Volume Name"), + value: volumeName, + inline: true, + }, + { + name: decorate("`🔧`", "Service Type"), + value: serviceType, + inline: true, + }, + ...(backupSize ? [{ + name: decorate("`📊`", "Backup Size"), + value: backupSize, + inline: true, + }] : []), + { + name: decorate("`📅`", "Date"), + value: ``, + inline: true, + }, + { + name: decorate("`⌚`", "Time"), + value: ``, + inline: true, + }, + { + name: decorate("`❓`", "Type"), + value: type + .replace("error", "Failed") + .replace("success", "Successful"), + inline: true, + }, + ...(type === "error" && errorMessage + ? [ + { + name: decorate("`⚠️`", "Error Message"), + value: `\`\`\`${errorMessage}\`\`\``, + }, + ] + : []), + ], + timestamp: date.toISOString(), + footer: { + text: "Dokploy Volume Backup Notification", + }, + }); + } + + if (gotify) { + const decorate = (decoration: string, text: string) => + `${gotify.decoration ? decoration : ""} ${text}\n`; + + await sendGotifyNotification( + gotify, + decorate( + type === "success" ? "✅" : "❌", + `Volume Backup ${type === "success" ? "Successful" : "Failed"}`, + ), + `${decorate("🛠️", `Project: ${projectName}`)}` + + `${decorate("⚙️", `Application: ${applicationName}`)}` + + `${decorate("💾", `Volume Name: ${volumeName}`)}` + + `${decorate("🔧", `Service Type: ${serviceType}`)}` + + `${backupSize ? decorate("📊", `Backup Size: ${backupSize}`) : ""}` + + `${decorate("🕒", `Date: ${date.toLocaleString()}`)}` + + `${type === "error" && errorMessage ? decorate("❌", `Error:\n${errorMessage}`) : ""}`, + ); + } + + if (ntfy) { + await sendNtfyNotification( + ntfy, + `Volume Backup ${type === "success" ? "Successful" : "Failed"}`, + `${type === "success" ? "white_check_mark" : "x"}`, + "", + `🛠️Project: ${projectName}\n` + + `⚙️Application: ${applicationName}\n` + + `💾Volume Name: ${volumeName}\n` + + `🔧Service Type: ${serviceType}\n` + + `${backupSize ? `📊Backup Size: ${backupSize}\n` : ""}` + + `🕒Date: ${date.toLocaleString()}\n` + + `${type === "error" && errorMessage ? `❌Error:\n${errorMessage}` : ""}`, + ); + } + + if (telegram) { + const isError = type === "error" && errorMessage; + + const statusEmoji = type === "success" ? "✅" : "❌"; + const typeStatus = type === "success" ? "Successful" : "Failed"; + const errorMsg = isError + ? `\n\nError:\n
${errorMessage}
` + : ""; + const sizeInfo = backupSize ? `\nBackup Size: ${backupSize}` : ""; + + const messageText = `${statusEmoji} Volume Backup ${typeStatus}\n\nProject: ${projectName}\nApplication: ${applicationName}\nVolume Name: ${volumeName}\nService Type: ${serviceType}${sizeInfo}\nDate: ${format(date, "PP")}\nTime: ${format(date, "pp")}${isError ? errorMsg : ""}`; + + await sendTelegramNotification(telegram, messageText); + } + + if (slack) { + const { channel } = slack; + await sendSlackNotification(slack, { + channel: channel, + attachments: [ + { + color: type === "success" ? "#00FF00" : "#FF0000", + pretext: + type === "success" + ? ":white_check_mark: *Volume Backup Successful*" + : ":x: *Volume Backup Failed*", + fields: [ + ...(type === "error" && errorMessage + ? [ + { + title: "Error Message", + value: errorMessage, + short: false, + }, + ] + : []), + { + title: "Project", + value: projectName, + short: true, + }, + { + title: "Application", + value: applicationName, + short: true, + }, + { + title: "Volume Name", + value: volumeName, + short: true, + }, + { + title: "Service Type", + value: serviceType, + short: true, + }, + ...(backupSize ? [{ + title: "Backup Size", + value: backupSize, + short: true, + }] : []), + { + title: "Time", + value: date.toLocaleString(), + short: true, + }, + { + title: "Type", + value: type, + short: true, + }, + { + title: "Status", + value: type === "success" ? "Successful" : "Failed", + short: true, + }, + ], + }, + ], + }); + } + } +}; diff --git a/packages/server/src/utils/volume-backups/utils.ts b/packages/server/src/utils/volume-backups/utils.ts index b6a34e2aa..59fb3f6fa 100644 --- a/packages/server/src/utils/volume-backups/utils.ts +++ b/packages/server/src/utils/volume-backups/utils.ts @@ -11,6 +11,7 @@ import { } from "@dokploy/server/utils/process/execAsync"; import { scheduledJobs, scheduleJob } from "node-schedule"; import { getS3Credentials, normalizeS3Path } from "../backups/utils"; +import { sendVolumeBackupNotifications } from "../notifications/volume-backup"; import { backupVolume } from "./backup"; export const scheduleVolumeBackup = async (volumeBackupId: string) => { @@ -77,6 +78,41 @@ export const runVolumeBackup = async (volumeBackupId: string) => { } await updateDeploymentStatus(deployment.deploymentId, "done"); + + // Send success notification + try { + const projectName = volumeBackup.application?.environment?.project?.name || + volumeBackup.compose?.environment?.project?.name || + volumeBackup.postgres?.environment?.project?.name || + volumeBackup.mysql?.environment?.project?.name || + volumeBackup.mariadb?.environment?.project?.name || + volumeBackup.mongo?.environment?.project?.name || + volumeBackup.redis?.environment?.project?.name || + "Unknown Project"; + + const organizationId = volumeBackup.application?.environment?.project?.organizationId || + volumeBackup.compose?.environment?.project?.organizationId || + volumeBackup.postgres?.environment?.project?.organizationId || + volumeBackup.mysql?.environment?.project?.organizationId || + volumeBackup.mariadb?.environment?.project?.organizationId || + volumeBackup.mongo?.environment?.project?.organizationId || + volumeBackup.redis?.environment?.project?.organizationId || + ""; + + // Map service type to match notification function expectations + const mappedServiceType = volumeBackup.serviceType === "mongo" ? "mongodb" : volumeBackup.serviceType; + + await sendVolumeBackupNotifications({ + projectName, + applicationName: volumeBackup.name, + volumeName: volumeBackup.volumeName, + serviceType: mappedServiceType as "application" | "postgres" | "mysql" | "mongodb" | "mariadb" | "redis" | "compose", + type: "success", + organizationId, + }); + } catch (notificationError) { + console.error("Failed to send volume backup success notification:", notificationError); + } } catch (error) { const { VOLUME_BACKUPS_PATH } = paths(!!serverId); const volumeBackupPath = path.join( @@ -92,6 +128,42 @@ export const runVolumeBackup = async (volumeBackupId: string) => { } await updateDeploymentStatus(deployment.deploymentId, "error"); + // Send error notification + try { + const projectName = volumeBackup.application?.environment?.project?.name || + volumeBackup.compose?.environment?.project?.name || + volumeBackup.postgres?.environment?.project?.name || + volumeBackup.mysql?.environment?.project?.name || + volumeBackup.mariadb?.environment?.project?.name || + volumeBackup.mongo?.environment?.project?.name || + volumeBackup.redis?.environment?.project?.name || + "Unknown Project"; + + const organizationId = volumeBackup.application?.environment?.project?.organizationId || + volumeBackup.compose?.environment?.project?.organizationId || + volumeBackup.postgres?.environment?.project?.organizationId || + volumeBackup.mysql?.environment?.project?.organizationId || + volumeBackup.mariadb?.environment?.project?.organizationId || + volumeBackup.mongo?.environment?.project?.organizationId || + volumeBackup.redis?.environment?.project?.organizationId || + ""; + + // Map service type to match notification function expectations + const mappedServiceType = volumeBackup.serviceType === "mongo" ? "mongodb" : volumeBackup.serviceType; + + await sendVolumeBackupNotifications({ + projectName, + applicationName: volumeBackup.name, + volumeName: volumeBackup.volumeName, + serviceType: mappedServiceType as "application" | "postgres" | "mysql" | "mongodb" | "mariadb" | "redis" | "compose", + type: "error", + organizationId, + errorMessage: error instanceof Error ? error.message : String(error), + }); + } catch (notificationError) { + console.error("Failed to send volume backup error notification:", notificationError); + } + console.error(error); } };