Mail compression with Dovecot

The popular IMAP server Dovecot supports compression incoming Mails in order to save disk space on the server.
This feature can be enabled by using the zlib plugin.

Installation and configuration is very simple, just add “zlib” to the mail_plugins configuration.
If you want you can specify the level of compression too – but the default value is just fine.

mail_plugins = $mail_plugins zlib

Only new Mails get compressed upon arrival. Existing Mails stay as they are.
As soon as a new Mail arrives, it gets compressed and is stored in your mailbox. If you have a closer look on the actual mail file you see that it is not a ordinary text file but a compressed file.

[root@server cur]# file 1398234.M33854922198.mail.server.com,S=2420,W=2465:2,S
1398234.M33854922198.mail.server.com,S=2420,W=2465:2,S: gzip compressed data, from Unix

Additional information can be found in the Dovecot Wiki.

One thought on “Mail compression with Dovecot

  1. NoOne

    To compress existing mails I use a compress.sh script.


    ./compress.sh /home/noone/Maildir


    #!/bin/bash
    if [[ $1 == "" ]]; then
    echo $0 " "
    exit -1
    fi

    tocompress=`cd "$1/cur/" && find . -iname '*,S=*[^Z]' -exec file {} \; | grep -v 'gzip compressed data' | sed 's/: /!/' | cut -d '!' -f 1`

    for mail in $tocompress; do
    echo "gzipping $1/cur/$mail to $1/tmp/${mail}Z"
    gzip -S Z "$1/cur/$mail" -c > "$1/tmp/${mail}Z"
    touch -r "$1/cur/$mail" "$1/tmp/${mail}Z"
    chown vmail:vmail "$1/tmp/${mail}Z"
    chmod 600 "$1/tmp/${mail}Z"
    done

    echo "aquiring maildirlock"
    if PID=`/usr/lib/dovecot/maildirlock "$1/cur" 20`; then
    for mail in $tocompress; do
    mv "$1/tmp/${mail}Z" "$1/cur/"
    rm "$1/cur/${mail}"
    done
    kill $PID
    else
    echo "lock failed"
    exit -1
    fi

Comments are closed.