Print of www.smithonline.id.au on Friday 08Dec2023 Page : Postfix Mail Archiver
Postfix Mail Archiver
Sometimes it's important to maintain a log or copy of mail going through a mail server. Fortunately, Postfix makes this easy by simply adding:
always_bcc = mailstore@your.domain
to your main.cf postfix configuration file, and adding an account "mailstore" to receive a copy of emails going through the system. However, mailstore's inbox will grow very large quite quickly. So I use the script below to move mails from mailstore's inbox (/var/spool/mail/mailstore) to a mail folder (e.g. /home/mailstore/Mail/Mail20070823) every night. Note that my system uses standard Unix style mail files (a single text file for each folder with emails separated by a blank line followed by a line starting with "From"). The script is run by crontab at 23:55 each night.
#!/bin/bash
#
# Script : mailstore_daily
#
# This script moves the mailstore inbox (/var/spool/mail/mailstore)
# to the mail directory (/home/mailstore/Mail/) with a file
# name of MailROOT_UID=0
#
SUFFIX=`date +%Y%m%d`
LOGFILE="./var/log/mail_store.log"
# Start message
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME Starting mailstore_daily" >> $LOGFILE
logger mystuff - mailstore_daily starting
# Must be root
if [ "$UID" -ne "$ROOT_UID" ]then
echo "$DATETIME Must be root to run this script" >> $LOGFILE
exit 1
fi
cd /var/spool/mail
# Stop postfix
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME Stopping Postfix" >> $LOGFILE
/etc/init.d/postfix stop >> $LOGFILE
sleep 20
sync
# Test for regular file exists
if [ -a "mailstore" ]then
FILESIZE=`stat -c %s mailstore`
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME mailstore inbox = $FILESIZE bytes" >> $LOGFILE
mv mailstore /home/mailstore/Mail/Mail${SUFFIX}
chown mailstore:mail /home/mailstore/Mail/Mail${SUFFIX}
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME Mailstore inbox moved to Mail${SUFFIX}" >> $LOGFILE
sleep 5
# create new inbox for mailstore
touch mailstore
chown mailstore:mail mailstore
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME New inbox created" >> $LOGFILE
logger mystuff - mailstore_daily completed move of file $FILESIZE bytes
else
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME Error: Mailstore does not exist" >> $LOGFILE
echo " " >> $LOGFILE
exit 2
fi
#
# Restart postfix
#
sleep 10
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME Starting Postfix" >> $LOGFILE
/etc/init.d/postfix start >> $LOGFILE
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME mailstore_daily completed" >> $LOGFILE
echo " " >> $LOGFILE
logger mystuff - mailstore_daily completed
logger mystuff - mailstore_daily actions logged to $LOGFILE
exit
It's also useful to store away the postfix log files for each day as well - so you end up with both the emails and mail log for each day. The following script is run every day at about 00:15 and stores a copy of the postfix log file in /home/mailstore/Maillogs/
#!/bin/bash
#
# File : maillog_daily
# This script copies maillog entries for yesterday to a file
# in /home/mailstore/Maillog with a filename of Maillog
#
# It would normally be run early in the morning to collect
# maillog entries from the previous day
#
ROOT_UID=0
FILESUFFIX=`date --date yesterday +%Y%m%d`
SEARCHDATE="`date --date yesterday +%b` `date --date yesterday +%e`"
LOGFILE="./var/log/mail_store.log"
# Start message
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME Starting maillog_daily" >> $LOGFILE
logger mystuff - maillog_daily starting
# Must be root
if [ "$UID" -ne "$ROOT_UID" ]then
echo "Must be root to run this script" >> $LOGFILE
exit 1
fi
#
# Get the mail log entries and put into new file dated yesterday
#
# Write a header for the new file
echo "#### Log created $DATETIME by /usr/local/bin/maillog_daily ####"
> /home/mailstore/Maillogs/Maillog$FILESUFFIX
echo " " >> /home/mailstore/Maillogs/Maillog$FILESUFFIX
# Get records for yesterday from the last two log files
cat /var/log/maillog.1 | grep "^$SEARCHDATE" >> /home/mailstore/Maillogs/Maillog$FILESUFFIX
cat /var/log/maillog | grep "^$SEARCHDATE" >> /home/mailstore/Maillogs/Maillog$FILESUFFIX
echo "Maillog file created : Maillog$FILESUFFIX" >> $LOGFILE
chown mailstore:mailstore /home/mailstore/Maillogs/*
chmod 400 /home/mailstore/Maillogs/*
echo "Maillogs chown and chmod completed" >> $LOGFILE
DATETIME=`date +%H:%M:%S %d%b%Y`
echo "$DATETIME maillog_daily completed" >> $LOGFILE
echo " " >> $LOGFILE
logger mystuff - maillog_daily completed
logger mystuff - maillog_daily actions logged to $LOGFILE
exit
Print of www.smithonline.id.au on Friday 08Dec2023