我希望每次使用我安装在 Ubuntu 16.04 服务器上的 BackupPC 成功进行备份时都会收到电子邮件通知。除非备份失败,否则 BackupPC 不会通过电子邮件通知您。当我运行以下脚本时,我收到一封电子邮件,提示其“失败”。
我在备份设置中放置了以下内容:
$Conf{DumpPostUserCmd} =
/var/lib/backuppc/backuppc_notification_email.sh $xferOK $host $type
$client $hostIP $share $XferMethod $sshPath $cmdType
这是 shell 脚本:
#!/bin/bash
# script to send simple email
# Email To ?
EMAIL="[email protected]"
# Email text/message
EMAILMESSAGE="/var/lib/backuppc/emailmessage.txt"
# 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"
else
STATUS="has FAILED"
fi
# email subject
SUBJECT="[BackupPC] $STATUS for host: $client"
# Email text/message
echo "The filesystem backup for $host $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
# send an email using /bin/mail
/usr/sbin/sendmail -v "$EMAIL" "$SUBJECT" < $EMAILMESSAGE
以下是我收到的电子邮件:
The filesystem backup for has FAILED
-----------------------------------------
Type:
Client:
Host:
Host IP:
Share:
XferMethod:
sshPath:
cmdType:
我想知道为什么电子邮件没有我的 shell 脚本中所述的相关信息,以及为什么它显示“失败”?
答案1
尝试引用命令并在末尾添加分号:
$Conf{DumpPostUserCmd} = '/var/lib/backuppc/backuppc_notification_email.sh $xferOK $host $type $client $hostIP $share $XferMethod $sshPath $cmdType';
从备份电脑文档:
配置文件是一个由 BackupPC 执行的 perl 脚本,因此在编辑时应小心保留文件语法(标点符号、引号等)。建议您使用 CVS、RCS 或其他源代码控制方法来更改 config.pl。
另外,您可以使用这里的文档用于创建电子邮件:
cat << _EOF_ > "$EMAILMESSAGE"
The filesystem backup for $host $STATUS
-----------------------------------------
Type: $type
Client: $client
Host: $host
Host IP: $hostIP
Share: $share
XferMethod: $XferMethod
sshPath: $sshPath
cmdType: $cmdType
_EOF_