Backup IMAP account

The need behind this scenario is very simple – make sure I have up to date backup of my IMAP account as sometimes it does happen that access to the account might be lost for whatever reason.

Additional requirement is to have it “just working” without any activity from my side.

What didn’t work very well was Thunderbird synchronizing all emails for offline use. The reason behind was that structure of the account had moving parts – new subfolders appearing and being moved to other locations. With Thunderbird it did require manual selection of “synchronized” folders which was a daunting task.

The solution is to use mbsync, synchronizing from remote IMAP server to local folder which is then exposed via local dovecot for use with any IMAP client. In this particular situation Thunderbird is used, though is set to not cache any emails older than 1 day (minimum setting within Thunderbird).

How to set it up

mbsync

In my particular situation, I’ve had to build isync on my own, as the repository contained old (v1.1) version vs currently available (v1.3).

To build isync following steps were used:

This built our needed isync_1.3.0-1_amd64.deb

File ~/.mbsyncrc was created with following

The aim was simple – dump and maintain copy if IMAP and follow any removals of emails, etc.

Folder itself is backed up incrementally with borg backup for history, just in case if someone would remove all content on remote side. This would propagate nicely removing all emails on local side and borg backup allows us to revert to previous stage.

Mbsync finishes its task once goes through all folders and does not maintain connection for updates. Therefore we need to run it periodically from cront

With this we will have periodically synchronized offline repository of emails (one way – to our local folder only).

Dovecot

Dovecot will serve our local folder via IMAP to any client. This is dictated by the fact that Thunderbird doesn’t play well with maildir folder and I couldn’t get it to discover new emails appearing in folders. I would have to have a dirty solution to remove “.msf” files as it forced TB to discover new files. Though problem did persist with discovering folder structure.

Dovecot installation and configuration is very simple:

Update /etc/dovecot/dovecot.conf :

Update /etc/dovecot/conf.d/10-mail.conf

 

Update /etc/dovecot/conf.d/20-imap.conf in section

New dovecot packages uses upstart, hence “start/stop/restart dovecot” commands need to be used to restart process instead of /etc/init.d/dovecot restart, etc.

Thunderbird

Standard Thunderbird setup with just one minor modification.

Deselect in Server Settings -> Advanced -> Show only subscribed folders

For whatever reason the subfolders are not under Inbox. It has something to do with Dovecot configuration, but since this worked for me I didn’t waste more time to investigation. I didn’t need to subscribe to all folders as I was accessing my local offline copy of files already.

Just to be on safe side and avoid email duplication under Account -> Synchronization & Storage -> Keep messages for this account on this computer” has been unchecked.

Leave a Reply

Your email address will not be published. Required fields are marked *