我正在使用 BackupPC,并且制作了一个脚本,每次备份发生时(无论成功还是失败)都会通过电子邮件向我发送通知。在此脚本中,我想从日志文件中提取最后一条语句。然而,我的脚本正在将一个字符串回显到 BackupPC 中的实际日志文件,我不知道它是如何/为什么这样做的。
#!/bin/bash
# Script to send email notification when BackupPC has performed a backup
sleep 30
# Email To ?
EMAIL="[email protected]"
# Email text/message
EMAILMESSAGE="/var/lib/backuppc/emailmessage.txt"
DATE=$(date +%m%Y)
#Grab the status variables
xferOK=$1
host=$2
type=$3
client=$4
hostIP=$5
share=$6
XferMethod=$7
sshPath=$8
cmdType=$9
#Extract the last line of the log for error reporting
LOG_FILE="/var/lib/backuppc/pc/$host/LOG.$DATE"
# 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 "Retrieving the last statement from $host LOG file..."
tail -n 2 "$LOG_FILE" >>$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 "Retrieving the last statement from $host LOG file..."
tail -n 2 "$LOG_FILE" >>$EMAILMESSAGE
# send an email using sendmail
/usr/sbin/sendmail "$EMAIL" < $EMAILMESSAGE
fi
这是我收到的电子邮件:
uklamp01 backup has been SUCCESSFUL
------------------------------------------------
2017-08-25 08:52:31 incr backup started back to 2017-08-15 09:45:02 (backup #0) for directory /
2017-08-25 09:08:41 Output from DumpPostUserCmd: Retrieving the last statement from uklamp01 LOG file...
------------------------------------------------
Type: incr
Client: uklamp01
Host: uklamp01
Host IP: uklamp01
Share: /
XferMethod: rsync
sshPath: /usr/bin/ssh
cmdType: DumpPostUserCmd
我的脚本中的字符串(“检索...等”)如何存在于 BackupPC 的实际日志文件中?请问有办法解决这个问题吗?