How to backup your IMAP mail account

01 Nov 2009

Since about a year IMAP has become widely available with free email providers. See GMX and gmail. That’s why I decided to abandon my old payed email account at fusemail. Now to backup your emails there a couple of open-source projects that store your emails (archivemail, imapbackup.py and offlineIMAP). I finally settled for offlineIMAP because it has the most interface options and supports incremental backups. Incremental backups were very important to me because the other programs seemed to hang from time to time and had to restart the whole backup process all over again.

This might be due to my flaky Wifi connection; YMMV.

Offlineimap only backups in Maildir format. Unfortunately most GUI email clients like Mail.app or Thunderbird by default only support mbox format for importing mails. I found a small script that converts mails from Maildir to mbox format using the command formail from the projekt procmail.

Using this command the conversion is extremely easy. I’ve written a small script that should be executed from the parent directory of the backed up Maildir folder with maildir2mbox.sh <foldername>:

for i in $1/cur/*;

do formail -I "Status: RO" <"$i" >>$1-mbox;

done

for i in $1/new/*;

do formail -I Status: <"$i" >>$1-mbox;

done

for i in $1/cur/;
do formail -I “Status: RO” <“$i” >>$1-mbox;
done
for i in $1/new/;
do formail -I Status: <“$i” >>$1-mbox;
done

The resulting <folder-name>.mbox files can be easily imported into either Thunderbird or Mail.app. Another possiblity for import to Thunderbird.app is the Add-on ImportExportTools. Which recognizes the mails from Offlineimap if you add an .eml extension to their filenames.

There remains the problem that mails for special folders like drafts or templates are imported like normail emails in e.g. Mail.app. A simple solution to this problem is to drag them to the drafts folder in a local POP3 mailbox or on another remote IMAP mailbox.