Backing up email from any IMAP server (plus: syncing and migrating)

I recently migrated email providers (several times, actually) and came across the fantastic tool mbsync. It’s included in most GNU/Linux distributions, though sometimes called by its old name, “isync”.

mbsync is very powerful, it can not only sync between a remote IMAP server and e.g. a local on-disk mirror that you want to keep so you can back it up, it can also sync between two remote IMAP servers directly.

Many very advanced configurations are possible from your own personal .mbsyncrc file. But since it took some time to puzzle together a good config from the semi-cryptic manpage, here are two configs I used for two typical situations.

Migrating from one IMAP server to another

IMAPStore   oldserver 
Host        mail.myoldserver.com
User        myusername 
Pass        mypassword
 
IMAPStore   newserver 
Host        mail.newserver.org  
User        myusername 
Pass        mypassword
 
Channel   migrate 
Master    :oldserver: 
Slave     :newserver: 
Patterns  * !Trash 
Create    Slave 
Expunge   Slave 
Sync      Pull

Calling mbsync migrate will migrate your email from your old server to the new one, creating any new directories and messages on the new server and not deleting anything on the old server. You can run this multiple times in case new messages are still arriving on your old server.

Mirroring your IMAP mail to a local directory

This is useful for backing up your email. Particularly nice if you’re on a free or cheap email provider that does not offer backups.

 
IMAPStore yourserver
Host      mail.yourserver.io
User      yourusername
Pass      yourpassword
 
MaildirStore mirror 
AltMap       yes 
Path         ~/mailmirror/ 
 
Channel   mirror 
Master    :yourserver: 
Slave     :mirror: 
Patterns  * !Trash 
Create    Slave 
Expunge   None 
Sync      Pull

Then run mbsync mirror to pull your email. You can run this multiple times, it will not delete messages in the mirror directory. If you want messages to be deleted there, set Expunge to slave. Make sure to back up both the mailmirror directory and the Maildir directory, which holds your INBOX.

A note about AltMap: This particular option was required so that I could mirror to remotely mounted NFS shares. It will make mbsync create its UID validity data in a database instead of in a .uidvalidity file. For whatever reason, it seems that writing the .uidvalidity file stalls forever on NFS shares. If you’re not using an NFS share, you can try without that option.

In both examples, the Trash directory is ignored.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s