Ever since I started using GMail as my mail client, I wanted to have an automatically synchronized local (off-line) copy of my GMail account. Such a copy would not only serve as a backup, but would also come in handy when I needed to consult messages when I’m without internet connection. It would also allow me to efficiently check my e-mail when I only have very limited (and expensive) bandwidth, or simply to find out which messages take up most space on my account.
With the arrival of IMAP support for GMail, it has become possible to efficiently synchronize your mailboxes, without having to jump through loops or downloading your whole history over POP3 every time you synchronize. While looking for an IMAP synchronization tool, I stumbled upon a post referring to mbsync, a tool which did exactly what I wanted. The post itself only talked about synchronizing your messages as a flat list, and did not talk about synchronizing labels. When trying to synchronizing all my mail, including labels, I bumped into some caveats of mbsync. Luckily, these problems can easily be fixed.
My first problem was the fact that I use ‘/’ in my GMail labels to create a label hierarchy. It turns out that GMail’s IMAP service uses ‘/’ as its hierarchy delimiter as well, which makes the IMAP mailbox hierarchy the same as my label hierarchy. Moreover, GMail’s internal mail folders (e.g. ‘All Mail’, ‘Sent Mail’, ‘Starred Mail’, …) also reside in a hierarchy under a toplevel mailbox called ‘[Gmail]’. The problem is that mbsync only checks the toplevel mailboxes, despite the fact that it allows you to write mailbox matching patterns based on delimiters. Changing mbsync to check the whole hierarchy of IMAP mailboxes was just a one-character patch.
However, then came the second problem: mailboxes with ‘/’ in their name cannot be written to the filesystem directly: you either have to make sure that all the parent directories of the mailbox exist, or you have to replace the ‘/’ by another character (sequence) that is valid, thus flattening your mailbox hierarchy. Since mbsync uses maildir as its local storage format, and since hierarchical maildirs are not really supported by mail clients, I chose for the latter solution: a flat list of maildir mailboxes for mirroring my hierarchical IMAP mailboxes.
After these changes, it is possible to synchronize a complete GMail account using the following mbsync configuration file:
IMAPAccount gmail Host imap.gmail.com User MY.NAME@gmail.com UseIMAPS yes CertificateFile ~/GMail/gmail.pem IMAPStore gmail-remote Account gmail MaildirStore gmail-local Path ~/GMail/ Inbox ~/GMail/Inbox Channel gmail Master :gmail-remote: Slave :gmail-local: # Exclude everything under the internal [Gmail] folder, except the interesting folders Patterns * ![Gmail]* "[Gmail]/All Mail" Create Slave Expunge Slave Sync Pull
After putting this in ~/.mbsyncrc, you can synchronize your account by running
mbsync gmail
You can also add the following to your configuration file:
Channel gmail-inbox Master :gmail-remote: Slave :gmail-local: Create Slave Expunge Slave Sync Pull
This allows you to only synchronize your inbox (e.g. when on GPRS), by calling
mbsync gmail-inbox
My patch against mbsync CVS can be found here (together with a fix to make it compile on Mac OS X).
Hi Remko, I tried to apply your patch to mbsync-1.0.4 and had to modify it a little bit to squeeze it in… anyway it seams to work still great, thank you very much
You can find the diff containing your changes against the vanilla mbsyinc-1.0.4 here:
http://gist.github.com/50323
sorry, How can I apply your patch?
Thanks
To apply the patch, go into the source directory of mbsync, and type
patch -p0 < /path/to/patch/file.Thanks, another question. How Can I read my local IMAP backup mails?
How Can I configure my mail client for import local mails?
@go3 That’s a good question. You will need a mail client that supports the ‘maildir’ format. The Maildir Wikipedia entry contains a list of such clients. I personally use mutt.
Hi there, i’m unable to sync my mailbox recursively. I applied the original patch to the version of Sat Aug 23 07:54:00 2008 and also tried the new patch against the 1.0.4, but it won’t sync subfolders. Does this only work with GMail? I’m syncing from a regular imap-box. Can this work?
Regards, Peter
[...] some searching, I came across two sites that had instructions using the utility mbsync (formerly isync). I found that following the [...]
@Peter I haven’t tried it with other servers but GMail. I don’t see why other servers shouldn’t work theoretically. Odd.
I had to apply Stephan’s patch against isync 1.0.4 with “--ignore-whitespace” because the were some changes from tabs to spaces.