Enable compression on Dovecot storage


I’ve just discovered than compression can be applied on Dovecot mail storage. Truth is I used to run the delivery via Postfix. Now that I use OpenSMTPD and Dovecot LMTP to deliver the mail, those can be compressed for write (LMTP) and read (IMAP) process. Here’s how.

For the record, those directions apply to OpenBSD 5.7 running dovecot-2.2.15p0, dovecot-ldap-2.2.15 and bzip2-1.0.6p1.

Tell IMAP process to use compression:

# vi /etc/dovecot/conf.d/20-imap.conf
protocol imap {
  mail_plugins = $mail_plugins zlib

Tell LMTP process to use compression:

# vi /etc/dovecot/conf.d/20-lmtp.conf
protocol lmtp {
  mail_plugins = $mail_plugins zlib

Configure the compression parameters:

# vi /etc/dovecot/conf.d/90-plugin.conf
plugin {
  zlib_save_level = 6
  zlib_save = bz2

Voilà! Note that compression will only apply to “new” emails. In my case, I enabled compression on the new server and used imapsync to transfer every emails ; those were then automatically compressed. There are directions on Dovecot WiKi (Zlib plugin)  that would help you compress already stored emails.

Now, to check compression ratio, I did a few shell magic:

# find /home/vmail -type f -name "*,S=*" | \
  sed -E 's/(^.*,S=)([0-9]+)(,.*$)/\2/' |  \
  awk '{ SUM += $1 } END { print SUM " (~ " SUM/1073741824 " GB)" }'
4134711725 (~ 3.85075 GB)

# find /home/vmail -type f -name "*,S=*" -ls | \
  awk '{ SUM += $7 } END { print SUM " (~ " SUM/1073741824 " GB)" }'
2842227573 (~ 2.64703 GB)

That’s a gain of about 1/3 of the storage. Quite nice. And as it is handled by Dovecot, any MUA will benefit from this without even knowing it.