Chat with us, powered by LiveChat
Home > Technical Blog > Send email based on backup joblog keyword
Updated 15th June 2023, Rob Morrison

Here’s a handy tip that you may find useful: In order to grep for a key word in the backup joblog and send an email to a selected recipient, we suggest using a script that will grep a keyword (in this example we use the “Will not descend” keyword). Then send the e-mail (in this case to the recipient specified in the “Mail” directive) or any specific e-mail you wish to use.

For example, in the Messages resource, you should have two “MailCommand” directives. One of them will call the script:

—–8<—–
Messages {
Name = Default
MailCommand = “/opt/bacula/scripts/send_email.sh %j %t %e %c %l %r”
MailCommand = “/opt/bacula/bin/bsmtp -h localhost -f \”\(Bacula\) \<%r\>\” -s \”Bacula: %t %e of %c %l\” %r”
OperatorCommand = “/opt/bacula/bin/bsmtp -h localhost -f \”\(Bacula\) \<%r\>\” -s \”Bacula: Intervention needed for %j\” %r”
Mail = root@localhost,xx.yyyy@baculasystems.com = All, !Skipped
Operator = root@localhost = Mount
Console = All, !Skipped, !Saved
Append = “/opt/bacula/log/bacula.log” = All, !Skipped
Catalog = All
}

******************

# cat ../scripts/send_email.sh
#!/bin/bash

jobid=$1
jobtype=$2
jobexitstatus=$3
jobclient=$4
joblevel=$5
recipient=$6

joblog=$(echo “llist joblog jobid=$jobid” | /opt/bacula/bin/bconsole)
has_warning=$(echo “$joblog” | grep “Will not descend”)
echo “$has_warning”
if [[ ! -z “$has_warning” ]]; then
echo “$joblog” | /opt/bacula/bin/bsmtp -h localhost -s “Backup reported \”Will not descend\”: $jobtype $jobexitstatus of $jobclient $joblevel” $recipient
fi
—–8<—–

If you wish to send these e-mails to a different recipient, you can simply add it in the MailCommand line as an argument to the script:

—–8<—–

MailCommand = “/opt/bacula/scripts/send_email.sh %j %t %e %c %l support@example.com”
—–8<—–

All the above can be customized with any option for character substitution.

Hope that is helpful. Next technical blog coming your way soon!

About the author
Rob Morrison
Rob Morrison is the marketing director at Bacula Systems. He started his IT marketing career with Silicon Graphics in Switzerland, performing strongly in various marketing management roles for almost 10 years. In the next 10 years Rob also held various marketing management positions in JBoss, Red Hat and Pentaho ensuring market share growth for these well-known companies. He is a graduate of Plymouth University and holds an Honours Digital Media and Communications degree, and completed an Overseas Studies Program.
Leave a comment

Your email address will not be published. Required fields are marked *