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
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