LDAP 备份脚本仅在出现错误时发送电子邮件

LDAP 备份脚本仅在出现错误时发送电子邮件

我正在使用此脚本通过 cronjob 每天备份 LDAP。是否可以修改脚本以仅在出现错误时发送电子邮件。

每日发送的电子邮件副本

* Create data directory: /var/backups/ldap/ldap/2011.09/27.
* Backup completed successfully.
    + Data: /var/backups/ldap/ldap/2011.09/27/2011.09.27.04.01.01.ldif*
    + Log: /var/backups/ldap/logs/2011.09/ldap-2011.09.27.04.01.01.log

LDAP 脚本

#########################################################
# Modify below variables to fit your need ----
#########################################################

# Where to store backup copies.
#BACKUP_ROOTDIR='/backup'
BACKUP_ROOTDIR='/var/backups/ldap'

# Compress plain SQL file: YES, NO.
COMPRESS="YES"

# Delete plain LDIF files after compressed. Compressed copy will be remained.
DELETE_PLAIN_LDIF_FILE="YES"

#########################################################
# You do *NOT* need to modify below lines.
#########################################################

export PATH="$PATH:/usr/sbin:/usr/local/sbin/"

# Commands.
CMD_DATE='/bin/date'
CMD_DU='du -sh'
CMD_COMPRESS='bzip2 -9'

if [ -f /etc/ldap/slapd.conf ]; then
    export CMD_SLAPCAT='slapcat -f /etc/ldap/slapd.conf'
elif [ -f /etc/openldap/slapd.conf ]; then
    export CMD_SLAPCAT='slapcat -f /etc/openldap/slapd.conf'
elif [ -f /usr/local/etc/openldap/slapd.conf ]; then
    export CMD_SLAPCAT='slapcat -f /usr/local/etc/openldap/slapd.conf'
else
    export CMD_SLAPCAT='slapcat'
fi

# Date.
export MONTH="$(${CMD_DATE} +%Y.%m)"
export DAY="$(${CMD_DATE} +%d)"
export DATE="$(${CMD_DATE} +%Y.%m.%d.%H.%M.%S)"

export BACKUP_SUCCESS='NO'

#########
# Define, check, create directories.
#
# Backup directory.
export BACKUP_DIR="${BACKUP_ROOTDIR}/ldap/${MONTH}/${DAY}"
export BACKUP_FILE="${BACKUP_DIR}/${DATE}.ldif"

# Logfile directory. Default is /backup/logs/YYYY.MM/.
export LOG_DIR="${BACKUP_ROOTDIR}/logs/${MONTH}"

# Check and create directories.
if [ ! -d ${BACKUP_DIR} ]; then
    echo "* Create data directory: ${BACKUP_DIR}."
    mkdir -p ${BACKUP_DIR}
fi

if [ ! -d ${LOG_DIR} ]; then
    echo "* Create log directory: ${LOG_DIR}."
    mkdir -p ${LOG_DIR} 2>/dev/null
fi

# Log file. Default is /backup/logs/YYYY.MM/mysql-YYYY.MM.DD.log.
LOGFILE="${LOG_DIR}/ldap-${DATE}.log"

############
# Initialize log file.
#
echo "* Starting backup: ${DATE}." >${LOGFILE}
echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE}
echo "* Log file: ${LOGFILE}." >>${LOGFILE}

##############
# Backing up
#

echo "* Dumping LDAP data into file: ${BACKUP_FILE}..." >>${LOGFILE}
${CMD_SLAPCAT} > ${BACKUP_FILE}
if [ X"$?" == X"0" ]; then
    export BACKUP_SUCCESS='YES'
fi

# Compress plain SQL file.
if [ X"${COMPRESS}" == X"YES" ]; then
    echo "* Compressing LDIF file with command: '${CMD_COMPRESS}' ..." >> ${LOGFILE}
    ${CMD_COMPRESS} ${BACKUP_FILE} >>${LOGFILE} 2>&1

    if [ X"$?" == X"0" ]; then
        echo "* [DONE]" >>${LOGFILE}

        # Delete plain LDIF file after compressed.
        if [ X"${DELETE_PLAIN_LDIF_FILE}" == X"YES" -a -f ${BACKUP_FILE} ]; then
            echo -n "* Removing plain LDIF file: ${BACKUP_FILE}..." >>${LOGFILE}
            rm -f ${BACKUP_DIR}/*.ldif >>${LOGFILE} 2>&1
            [ X"$?" == X"0" ] && echo -e "\t[DONE]" >>${LOGFILE}

        fi
    fi
fi


# Append file size of backup files.
echo "* File size:" >>${LOGFILE}
echo "=================" >>${LOGFILE}
${CMD_DU} ${BACKUP_FILE}* >>${LOGFILE}
echo "=================" >>${LOGFILE}

echo "* Backup completed (Successfully: ${BACKUP_SUCCESS})." >>${LOGFILE}

if [ X"${BACKUP_SUCCESS}" == X"YES" ]; then
    cat <<EOF
* Backup completed successfully.
EOF
else
    echo -e "\n* Backup completed with !!!ERRORS!!!.\n" 1>&2
fi

cat << EOF
    + Data: ${BACKUP_FILE}*
    + Log: ${LOGFILE}
EOF

答案1

仅当作业有任何 stderr 或 stdout 输出时,Cron 才会发送电子邮件。您已经将大部分输出回显到日志文件,但那 4 行不会被重定向,因此您始终会收到 cron 报告。

我会移动这个区块

cat << EOF
    + Data: ${BACKUP_FILE}*
    + Log: ${LOGFILE}
EOF

进入你的“else”条件,这样它只echo在错误状态期间出现,然后删除你定义之前发生的两个回声LOGFILE(或者存储这两条消息并在你定义它之后将它们放到日志文件中)。

只要成功重定向全部输出到日志文件,那么只有出现错误时您才会收到邮件。

相关内容