A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
import ejs from 'ejs';
import { configService } from '../../../../services/ConfigService';
import { Plan } from '../../../../db/schema/plans';
import { providers } from '../../../../utils/providers';
import { BackupNotificationJSONPayload, BackupTaskStats } from '../../../../types/backups';
import { formatBytes, formatDuration, formatNumberToK } from '../../../../utils/formatter';
import { BaseNotification } from '../../../BaseNotification';
import { loadBackupTemplate } from '../../../templateLoader';
export interface BackupFailedNotificationPayload {
appTitle: string;
deviceName: string;
storageName: string;
storageType: string;
plan: Plan;
startTime: Date;
endTime: Date;
error: string;
stats?: BackupTaskStats;
output?: 'html' | 'json' | 'push' | 'slack' | 'discord';
}
export class BackupFailedNotification extends BaseNotification {
constructor(data: BackupFailedNotificationPayload) {
super();
this.subject = `Backup Failed: ${data.plan.title}`;
this.contentType = data.output || 'html';
this.content = this.buildContent(data);
}
protected buildContent(data: BackupFailedNotificationPayload): string {
if (this.contentType === 'json') {
return this.buildJSONContent(data);
}
if (this.contentType === 'push') {
return this.buildSlackContent(data);
}
if (this.contentType === 'slack') {
return this.buildSlackContent(data);
}
if (this.contentType === 'discord') {
return this.buildDiscordContent(data);
}
return this.buildHTMLContent(data);
}
protected buildJSONContent(data: BackupFailedNotificationPayload): string {
const {
appTitle,
deviceName,
storageName,
storageType,
plan,
stats,
startTime,
endTime,
error,
} = data;
const payload: BackupNotificationJSONPayload = {
appTitle,
deviceName,
storageName,
storageType,
storagePath: plan.storagePath || '',
stats,
startTime,
endTime,
error,
planID: plan.id,
planTitle: plan.title,
planType: plan.method,
planSource: {
included: plan.sourceConfig?.includes.join(', ') || '',
excluded: plan.sourceConfig?.excludes.join(', ') || '',
},
replicationStorages:
plan.settings.replication?.enabled && plan.settings.replication?.storages
? plan.settings.replication.storages.length
: 0,
eventName: 'backup_failed',
};
return JSON.stringify(payload);
}
protected loadHTMLTemplate(): string {
return loadBackupTemplate('BackupFailedNotification.ejs');
}
protected buildHTMLContent(data: BackupFailedNotificationPayload): string {
// Generate the Email Body
const startDate = new Date(data.startTime ? data.startTime : 0).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
});
const endDate = new Date(data.endTime ? data.endTime : 0).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
});
const storageTypeName = data.storageType
? providers[data.storageType]?.name || data.storageType
: '';
const templateString = this.loadHTMLTemplate();
const renderedBody = ejs.render(templateString, {
...data,
appUrl: configService.config.APP_URL,
storageTypeName,
startDate,
endDate,
formatBytes,
formatDuration,
formatNumberToK,
replicationStorages:
data.plan.settings.replication?.enabled && data.plan.settings.replication?.storages
? data.plan.settings.replication.storages.length
: 0,
});
return this.applyEmailTemplate(renderedBody, {
appTitle: configService.config.APP_TITLE || 'Pluton',
preHeader: `Backup Failed: ${data.plan.title}`,
className: 'content--error',
});
}
protected buildSlackContent(data: BackupFailedNotificationPayload): string {
const { appTitle, deviceName, storageName, storageType, plan, error, startTime, endTime } =
data;
const storageTypeName = storageType ? providers[storageType]?.name || storageType : '';
const appUrl = configService.config.APP_URL || '';
const fields: { type: string; text: string }[] = [
{ type: 'mrkdwn', text: `*Plan:* ${plan.title}` },
{ type: 'mrkdwn', text: `*Method:* ${plan.method}` },
{ type: 'mrkdwn', text: `*Device:* ${deviceName}` },
{ type: 'mrkdwn', text: `*Storage:* ${storageName} (${storageTypeName})` },
];
const blocks: Record<string, any>[] = [
{
type: 'header',
text: { type: 'plain_text', text: `❌ Backup Failed: ${plan.title}`, emoji: true },
},
{
type: 'section',
fields,
},
{
type: 'section',
text: { type: 'mrkdwn', text: `*Error:*\n\`\`\`${error}\`\`\`` },
},
];
if (appUrl) {
blocks.push({
type: 'actions',
elements: [
{
type: 'button',
text: { type: 'plain_text', text: 'View Plan', emoji: true },
url: `${appUrl}/plan/${plan.id}`,
},
],
});
}
blocks.push({
type: 'context',
elements: [{ type: 'mrkdwn', text: `${appTitle} • ${new Date(startTime).toLocaleString()}` }],
});
return JSON.stringify({ blocks });
}
protected buildDiscordContent(data: BackupFailedNotificationPayload): string {
const { appTitle, deviceName, storageName, storageType, plan, error, startTime, endTime } =
data;
const storageTypeName = storageType ? providers[storageType]?.name || storageType : '';
const appUrl = configService.config.APP_URL || '';
const fields: { name: string; value: string; inline: boolean }[] = [
{ name: 'Plan', value: plan.title, inline: true },
{ name: 'Method', value: plan.method, inline: true },
{ name: 'Device', value: deviceName, inline: true },
{ name: 'Storage', value: `${storageName} (${storageTypeName})`, inline: true },
{ name: 'Error', value: `\`\`\`${error}\`\`\``, inline: false },
];
const embed: Record<string, any> = {
title: `❌ Backup Failed: ${plan.title}`,
color: 0xef4444, // red
fields,
timestamp: new Date(startTime).toISOString(),
footer: { text: appTitle },
};
if (appUrl) {
embed.url = `${appUrl}/plan/${plan.id}`;
}
return JSON.stringify({ embeds: [embed] });
}
buildPushContent(data: BackupFailedNotificationPayload) {
const { appTitle, deviceName, storageName, storageType, plan } = data;
const sourceCount = plan.sourceConfig.includes.length;
const payload = {
title: `${appTitle}: Backup Failed for Plan "${plan.title}"`,
body: `Failed to backup ${sourceCount} sources from ${deviceName} to ${storageName}(${storageType}).`,
priority: 4,
emoji: 'warning',
buttonUrl: `${configService.config.APP_URL}/plan/${data.plan.id}`,
buttonText: 'View Plan',
};
return JSON.stringify(payload);
}
}