Backing up your Exchange e-mail account

At my previous employer we were using a custom Exchange server to handle e-mail and calendars (I don’t know if it was a whitelabelled Office 365 or what, I’m no expert in these Microsoft technologies). It now came time to backup all e-mails because the service will not be used in the future.

I’m using Thunderbird for my e-mail, but its built-in Exchange support could not handle our special snowflake mail server. Thankfully there’s Davmail, which acts as a local IMAP/POP3/CalDav proxy to an external Exchange server. With it it was a breeze to configure Thunderbird and I never had an issue with Davmail in the last two years.

The Plan

For creating a backup of all my mails, I wanted something independent. Having the mails in Thunderbird is too fragile for my taste and using a third-party addon to export the mails is also not want I wanted to do.

There are a number of different IMAP sync/backup scripts out there. I settled for imapbackup, a tiny Python script that creates one mbox file per IMAP folder.

Setup

I’m using Windows at home and have no Python environment set up. So I opted to use my Linux server to handle the backup for me. Davmail can run in a headless mode and is – just as imapbackup is – available as a Docker image. Putting these two together is easy as pie. We’re using Docker Compose to define our containers like so:

version: "3"
services:
  davmail:
    image: "kayvan/davmail:latest"
    volumes:
      - "./davmail:/etc/davmail"

  imapbackup:
    image: "nasacct/imapbackup:latest"
    command: "-s davmail:1143"
    volumes:
      - "./imap:/imap"

Then we create a new davmail/davmail.properties file and fill it according to the documentation. Make sure to allow remote connections (davmail.allowRemote=true):

davmail.server=true
davmail.enableEws=auto
davmail.url=https://he.amiconsult.de/
davmail.imapPort=1143
davmail.allowRemote=true
log4j.logger.davmail=INFO
log4j.logger.httpclient.wire=INFO
log4j.logger.org.apache.commons.httpclient=INFO
log4j.rootLogger=INFO

(I’m leaving the original server name in the example in the hopes that if someone using the same hosted service is having the same issues, they will find this post.)

Making it Happen

Now we can just start the davmail IMAP proxy by doing a

$ docker-compose up -d davmail

… and then perform the actual backup by running

$ docker-compose run --rm imapbackup

This will interactively ask your for your login credentials and then scan the entire mailbox for folders. You can run this again and it will extend the mbox files it created during the previous run.

Once you’re done, you’re left with a directory containing a nice bundle of mbox files.