Debian

LDAP 備份腳本僅在出錯時發送電子郵件

  • September 27, 2011

我正在使用此腳本通過 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

只有當作業有任何 stderr 或 stdout 輸出時,Cron 才會發送電子郵件。您已經將大部分輸出回顯到日誌文件,但是這 4 行沒有被重定向,因此您將始終獲得 cron 報告。

我會移動這個塊

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

進入您的“其他”條件,以便它僅echo在錯誤狀態期間發生,然後刪除在您定義之前發生的兩個迴聲LOGFILE(或將這兩條消息儲存起來並在您定義後將它們放入日誌文件中。

只要成功將所有輸出重定向到日誌文件,那麼只有在出現錯誤時才會收到郵件。

引用自:https://serverfault.com/questions/315848