如何监视PostFix配置变更并自动同步?

Postfix

1 前言

一个问题,一篇文章,一出故事。
PostFix的Access配置文件是该服务的访问控制列表,于是笔者想要简化他的多节点修改操作,想实现修改任意一个节点,配置文件自动同步到其他节点,于是整理此文。

2 最佳实践

2.1 配置环境

2.1.1 配置root的双向的公钥认证

关于公钥认证,请参阅如下章节,

如何部署公钥认证?

2.1.2 安装同步工具

dnf install -y rsync

2.2 创建脚本触发

2.2.1 创建监视脚本

mkdir -p ~/scripts
vim ~/scripts/autoRsyncPostFix.sh

添加如下脚本,

#!/bin/bash

monitorFiles="/etc/postfix/access"
monitorLog="/var/log/autoRsyncPostFix.log"
monitorDate=`date +"%Y-%m-%d %H:%M:%S"`
rsyncHosts="dn2postfix02"

if [ ! -f "$monitorLog" ]; then
        touch "$monitorLog"
fi
if [ ! -f "$monitorLog" ]; then
        echo "$monitorDate"' Failed to create log file!'
        exit 1
fi

for file in $monitorFiles; do
        if [ "$file" == "" ]; then
                echo "$monitorDate"' Variable monitorFiles cannot be empty!' | tee -a "$monitorLog"
                continue
        fi
done

for hostname in $rsyncHosts; do
        if [ "$hostname" == "" ]; then
                echo "$monitorDate"' Variable rsyncHosts cannot be empty!' | tee -a "$monitorLog"
                continue
        fi
done

oldFileMd5=""
newFileMd5=""
for monitorFile in $monitorFiles; do
        if [ "`grep "$monitorFile" "$monitorLog" | wc -l`" != "0" ]; then
                oldFileMd5=`grep "$monitorFile" "$monitorLog" | grep -v "Failed" | cut -d" " -f3 | tail -n 1`
        else
                echo "$monitorDate"' '`md5sum "$monitorFile"` >> $monitorLog
                continue
        fi
        newFileMd5=`md5sum "$monitorFile" | cut -d" " -f1`

        if [ "$oldFileMd5" == "" -o "$newFileMd5" == "" ]; then
                echo "$monitorDate"' Failed to get parameters'" $monitorFile" | tee -a "$monitorLog"
                continue
        fi
        if [ "$oldFileMd5" == "$newFileMd5" ]; then
                continue
        fi
        for hostname in $rsyncHosts; do
                echo "$monitorDate"' '`md5sum "$monitorFile"` >> $monitorLog
                /usr/bin/rsync -azs "$monitorFile" "$hostname":"$monitorFile"
        done
done

2.2.2 创建脚本触发

crontab -e

加入如下配置,

*/2 * * * * sh ~/scripts/autoRsyncPostFix.sh
没有评论

发表回复

Postfix
如何指定PostFix的默认下一跳地址?

1 前言 一个问题,一篇文章,一出故事。 笔者想要指定邮件的默认下一跳地址,以便邮件可以被送到特定的 …

Postfix
如何实现监视PostFix的延迟邮件警告?

1 前言 一个问题,一篇文章,一出故事。 笔者生产中有一套PostFix集群,最近经历了一次邮件延迟 …

Postfix
如何优化PostFix?

1 前言 一个问题,一篇文章,一出故事。 笔者最近生产服务器遇到队列问题,因此需要通过优化PostF …