2024-11-21 10:07:11 +00:00
import nodemailer from "nodemailer" ;
import * as Constants from "./constants" ;
import * as utils from "./utils" ;
import * as logger from "./logger" ;
2022-07-07 05:49:25 +00:00
2024-11-21 10:07:11 +00:00
export async function sendBackupErrorToAdmins ( err , backupTimestamp ) {
2024-11-23 11:39:55 +00:00
const mailEnabled = process . env . APPSMITH_MAIL_ENABLED ;
const mailFrom = process . env . APPSMITH_MAIL_FROM ;
const mailHost = process . env . APPSMITH_MAIL_HOST ;
const mailPort = process . env . APPSMITH_MAIL_PORT ;
const mailUser = process . env . APPSMITH_MAIL_USERNAME ;
const mailPass = process . env . APPSMITH_MAIL_PASSWORD ;
const mailTo = process . env . APPSMITH_ADMIN_EMAILS ;
2024-11-19 05:41:21 +00:00
console . log ( "Sending Error mail to admins." ) ;
2022-07-07 05:49:25 +00:00
try {
2024-11-19 05:41:21 +00:00
if (
! mailEnabled ||
! mailFrom ||
! mailHost ||
! mailPort ||
! mailUser ||
! mailPass
) {
throw new Error (
"Failed to send error mail. Email provider is not configured, please refer to https://docs.appsmith.com/setup/instance-configuration/email to configure it." ,
) ;
} else if ( ! mailTo ) {
throw new Error (
"Failed to send error mail. Admin email(s) not configured, please refer to https://docs.appsmith.com/setup/instance-configuration/disable-user-signup#administrator-emails to configure it." ,
) ;
} else if ( ! mailEnabled ) {
throw new Error (
"Mail not sent! APPSMITH_MAIL_ENABLED env val is disabled, please refer to https://docs.appsmith.com/setup/instance-configuration/email to enable it." ,
) ;
} else {
2022-07-07 05:49:25 +00:00
const backupFiles = await utils . listLocalBackupFiles ( ) ;
const lastBackupfile = backupFiles . pop ( ) ;
2024-11-19 05:41:21 +00:00
const lastBackupTimestamp = lastBackupfile . match (
/appsmith-backup-(.*)\.tar.gz/ ,
) [ 1 ] ;
const lastBackupPath = Constants . BACKUP_PATH + "/" + lastBackupfile ;
2022-07-07 05:49:25 +00:00
const domainName = process . env . APPSMITH_CUSTOM_DOMAIN ;
const instanceName = process . env . APPSMITH_INSTANCE_NAME ;
2024-11-19 05:41:21 +00:00
let text =
"Appsmith backup did not complete successfully.\n\n " +
"Backup timestamp: " +
backupTimestamp +
"\n\n" +
"Last Successful Backup timestamp: " +
lastBackupTimestamp +
"\n" +
"Last Successful Backup location: " +
lastBackupPath +
"\n\n" ;
2022-07-07 05:49:25 +00:00
if ( instanceName ) {
2024-11-19 05:41:21 +00:00
text = text + "Appsmith instance name: " + instanceName + "\n" ;
2022-07-07 05:49:25 +00:00
}
2024-11-22 10:27:56 +00:00
2022-07-07 05:49:25 +00:00
if ( domainName ) {
2024-11-19 05:41:21 +00:00
text =
text +
"Link to Appsmith admin settings: " +
"http://" +
domainName +
"/settings/general" +
"\n" ;
2022-07-07 05:49:25 +00:00
}
2024-11-22 10:27:56 +00:00
2024-11-19 05:41:21 +00:00
text = text + "\n" + err . stack ;
2022-07-07 05:49:25 +00:00
const transporter = nodemailer . createTransport ( {
host : mailHost ,
port : mailPort ,
auth : {
user : mailUser ,
2024-11-19 05:41:21 +00:00
pass : mailPass ,
} ,
2024-11-21 10:07:11 +00:00
} as any ) ;
2022-07-07 05:49:25 +00:00
await transporter . sendMail ( {
from : mailFrom ,
to : mailTo ,
2024-11-19 05:41:21 +00:00
subject : "[Appsmith] ERROR: Backup Failed" ,
text : text ,
2022-07-07 05:49:25 +00:00
} ) ;
}
} catch ( err ) {
await logger . backup_error ( err . stack ) ;
}
}