Veeam Linux Backup Agent Script with E-mail Notifications


JOBNAME=veeam_backup_job_name
SCRIPT_DIR=/etc/veeam/scripts
LOG_DIR=/var/log/veeam/Backup/$JOBNAME
JOB_INFO=`veeamconfig job info --name $JOBNAME`

#Sendemail Parameters
smtpusername=smtp_username
smtppassword='smtp_password'
smtptls=yes/no/auto
smtphost=smtp.domain.tld
smtpport=587
emailfrom=someone@domain.tld
emailto=someone@domain.tld

#Install sendemail and related packages if necessary
apt install -y rar sendemail libio-socket-ssl-perl libnet-ssleay-perl perl


#start backup job
veeamconfig job start --name "$JOBNAME" 1> /etc/veeam/scripts/$JOBNAME.tmp 2> /etc/veeam/scripts/$JOBNAME.err

SESSION_ID=`grep ID $SCRIPT_DIR/$JOBNAME.tmp | awk  '{print $3}' | sed 's/\[//' | sed 's/\]//' | sed 's/\.//'`
SESSION_LOG_DIR=`grep log $SCRIPT_DIR/$JOBNAME.tmp | awk  '{print $4}' | sed 's/\[//' | sed 's/\]//' | sed 's/\.//'`
LOGFILE=$SESSION_LOG_DIR/Job.log
SESSION_INFO=`veeamconfig session info --id $SESSION_ID | grep -v UUID`

# check if another job is running
if [ -s $JOBNAME.err ]
then
     ERROR_MSG=`grep Error $SCRIPT_DIR/$JOBNAME.err`
     echo "$ERROR_MSG" | /usr/bin/sendemail -f $emailfrom -t $emailto -u "Veam Agent Job $JOBNAME Error" -m "Veam Agent Job $JOBNAME Error" -s $smtphost:$smtpport -o username=$smtpusername -o password=$smtppassword -o tls=$smtptls


fi

#check if job is running
until [ -z `pgrep veeamjobman` ]
do
    echo "job is running" > /dev/null
done

#check and set the exit status of the job
STATUS=`veeamconfig session info --id $SESSION_ID | grep State | awk  '{print $2}'`

if [ "$STATUS" == "Warning" ];
then
     WARN_MSG=`veeamconfig session log --id $SESSION_ID | grep warn | awk '{print $6,$7,$8,$9}'`
     echo -e "Job $JOBNAME Successful with following $STATUS:\n\n$WARN_MSG\n\nJOB INFO:\n$JOB_INFO" | /usr/bin/sendemail -f $emailfrom -t $emailto -u "Veam Agent Job $JOBNAME $STATUS" -m "Veam Agent Job $JOBNAME $STATUS"  -a $LOGFILE -s $smtphost:$smtpport -o username=$smtpusername -o password=$smtppassword -o tls=$smtptls
fi

if [ "$STATUS" == "Failed" ];
then
     echo -e "Job $JOBNAME $STATUS. See attached logfile for error details.\n\nJOB INFO:\n$JOB_INFO" | /usr/bin/sendemail -f $emailfrom -t $emailto -u "Veam Agent Job $JOBNAME $STATUS" -m "Veam Agent Job $JOBNAME $STATUS"  -a $LOGFILE -s $smtphost:$smtpport -o username=$smtpusername -o password=$smtppassword -o tls=$smtptls
fi

if [ "$STATUS" == "Success" ];
then
     echo -e "Job $JOBNAME $STATUS.\n\nJOB INFO:\n$JOB_INFO" | /usr/bin/sendemail -f $emailfrom -t $emailto -u "Veam Agent Job $JOBNAME $STATUS" -m "Veam Agent Job $JOBNAME $STATUS" -s $smtphost:$smtpport -o username=$smtpusername -o password=$smtppassword -o tls=$smtptls
fi

rm -f $SCRIPT_DIR/$JOBNAME.tmp $SCRIPT_DIR/$JOBNAME.err

Revision #3
Created 23 October 2022 13:29:16 by Dino Edwards
Updated 23 October 2022 13:34:54 by Dino Edwards