1

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
---------------------------------
4
  • However, I'm having difficulties - what difficulties? Commented Aug 23, 2017 at 15:38
  • You mean echo "$EMAILMESSAGE"? Commented Aug 23, 2017 at 15:42
  • 1
    tail -n1 "${LOG_FILE}" >> "${EMAILMESSAGE}"? Commented Aug 23, 2017 at 15:45
  • Off-topic: That script could benefit from using a here-document to create the message. Commented Aug 23, 2017 at 16:42

1 Answer 1

1

echo "$LOG_FILE" | sed -n '$p' does not extract the last line of the file $LOG_FILE, it takes the last line of what is sent to its standard input, that is your log file name (/var/lib/backuppc/log/LOG) which should appear in your e-mail.

The correct syntax would be:

sed -n '$p' "$LOG_FILE"

Or, better, since showing the N last lines is the job of tail:

tail -n 1 "$LOG_FILE"
6
  • Thanks for the response. So the correct syntax is....echo tail -n 1 "$LOG_FILE" >>$EMAILMESSAGE ? Commented Aug 24, 2017 at 8:03
  • @Marc, exactly. Commented Aug 24, 2017 at 8:10
  • Just ran the script, and I got: line 34: syntax error near unexpected token tail' and line 34: echo (tail -n 1 $LOG_FILE) >>$EMAILMESSAGE'. Any ideas? Commented Aug 24, 2017 at 14:39
  • That's tail -n 1 "$LOG_FILE" >>$EMAILMESSAGE, no echo (sorry, didn't spot it) and no parentheses Commented Aug 24, 2017 at 15:51
  • That worked perfectly, thank you. Much appreciated. Commented Aug 25, 2017 at 8:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.