Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.


Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.


Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.


Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Postfix

Saving Postfix Messages

Submitted by on June 3, 2015 – 3:08 pm

Sometimes for testing purposes you need to save all messages going through Postfix active queue. There are a couple of ways of doing this.

The simplest method is to simply add the “always_bcc” option to the This will cause Postfix to send a blind copy of every message to the pre-defined “catch-all” address. A more advanced take on this approach is to create a new domain specifically for BCC mail. The new domain will be defined using sender_bcc_maps and recipient_bcc_maps with regular expressions for more granular control over what is being BCC’d and where. For example, you can add this to

sender_bcc_maps = regexp:/etc/postfix/bcc_sender_domains
recipient_bcc_maps = regexp:/etc/postfix/bcc_recpipient_domains

Then you create bcc_sender_domains and bcc_recpipient_domains to BCC all incoming and outgoing mail to/from the same username on the BCC domain:
/^([^@]+)\@primarydomain\.com$/    $

A different approach is to simply copy everything in /var/spool/postfix/active to an archive location in real time. There are several utilities that allow you to do this. The one I use is lsyncd. You will need lsyncd version 2.1.4 or newer. Check out this article on the basics of lsyncd installation and configuration. Here’s a more specific example of the /etc/lsyncd.conf specifically for the purpose of saving Postfix active queue:
settings {
        logfile = "/var/log/lsyncd/lsyncd.log",
        statusFile = "/var/log/lsyncd/lsyncd.status",
        nodaemon = false,
        maxDelays = 3,
        onStartup = true,
        onAttrib = true,
        onCreate = true,
        onDelete = false,
        onModify = true,
        onMove = true,
        delete = false,

sync {
        delete = false,
        source = "/var/spool/postfix/active/",
        host = "remote_server",
        targetdir = "/target_home/active/",
        rsync = {
                        owner = true,
                        perms = true,
                        checksum = true,
                        compress = false,
                        acls = true,
                        verbose = true,
                        _extra = {"-Kx"},

The “delete = false” option will ensure lsyncd will not remove anything from the target directory. However, as you may imagine, after a period of time you may end up with a huge number of files on the <target_server>. Here’s a simple script that will monitor the target directory and, as soon as the number of files reaches 5,000, the script will move them to a subfolder with the current Unix epoch time:
find /target_home/var/spool/postfix -maxdepth 1 -mindepth 1 -type d | sort -u | while read line
if [ `\ls "${line}" | wc -l` -gt 5000 ]
	d=$(date +'%s')
	mkdir -p "${line}/${d}"
	find "${line}" -maxdepth 1 -mindepth 1 -type f -exec /bin/mv -f {} "${line}/${d}/" \;

Just add that to a crontab to run, say, every half hour for a moderately busy mail server.

Print Friendly, PDF & Email

Leave a Reply