I would like to extract the last line of the log in BackupPC, so if a failed backup occurs, I will know why by the email. I have used the commandsed to get the last line of the log. However, I'm having difficulties echoing that out to the screen. It's showing the FAILED line, but not the error line from the log. How can I fix this, and/or is there a better way to do this?
#!/bin/bash
# script to send simple email
# Email To ?
EMAIL="[email protected]"
# Email text/message
EMAILMESSAGE="/var/lib/backuppc/emailmessage.txt"
#Extract the last line of the log for error reporting
LOG_FILE="/var/lib/backuppc/log/LOG"
#Grab the status variables
xferOK=$1
host=$2
type=$3
client=$4
hostIP=$5
share=$6
XferMethod=$7
sshPath=$8
cmdType=$9
# Check if backup succeeded or not.
if [[ $xferOK == 1 ]]; then
STATUS="has been SUCCESSFUL"
# Email text/message
echo "$client backup $STATUS" > $EMAILMESSAGE
echo "------------------------------------------------------" >>$EMAILMESSAGE
echo "Type: $type" >>$EMAILMESSAGE
echo "Client: $client" >>$EMAILMESSAGE
echo "Host: $host" >>$EMAILMESSAGE
echo "Host IP: $hostIP" >>$EMAILMESSAGE
echo "Share: $share" >>$EMAILMESSAGE
echo "XferMethod: $XferMethod" >>$EMAILMESSAGE
echo "sshPath: $sshPath" >>$EMAILMESSAGE
echo "cmdType: $cmdType" >>$EMAILMESSAGE
/usr/sbin/sendmail "$EMAIL" < $EMAILMESSAGE
else
STATUS="has FAILED"
#If it had failed, send out the error report
# Email text/message
echo "$client backup $STATUS" > $EMAILMESSAGE
echo "---------------------------------" >>$EMAILMESSAGE
echo "$LOG_FILE" | sed -n '$p' >>$EMAILMESSAGE
/usr/sbin/sendmail "$EMAIL" < $EMAILMESSAGE
The output from my emails. No line from the LOG file whatsoever.
ukat2 backup has FAILED
---------------------------------
echo "$EMAILMESSAGE"?tail -n1 "${LOG_FILE}" >> "${EMAILMESSAGE}"?