I used to keep a local backup of my Gmail mails with OfflineImap . But I never felt comfortable with this solution because of OfflineImap being fully bidirectional. Which means my mails in the cloud are not protected from me messing with their local versions.

In the mean time I found out about mbsync (from the isync project ). It particularly features fine-grained options to let you defined which source is authoritative, thus restricting the synchronization to one direction.

Let’s install mbsync and its dependencies!

1$ sudo aptitude install isync ca-certificates

Just in case, don’t forget to enable IMAP access to you Gmail account  .

Create a new destination directory and an empty configuration file:

1$ mkdir -p ~/gmail-backup
2$ touch ~/.mbsyncrc

Then add the following parameters in ~/.mbsyncrc :

IMAPAccount      gmail
Host             imap.gmail.com
User             [email protected]
Pass             xxxxxxxxxxxxxx
UseIMAPS         yes
CertificateFile  ~/gmail-backup/gmail.crt
CertificateFile  ~/gmail-backup/google.crt
CertificateFile  /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt

IMAPStore  gmail-cloud
Account    gmail

MaildirStore  gmail-backup
Path          ~/gmail-backup/
Inbox         ~/gmail-backup/Inbox

Channel   gmail
Master    :gmail-cloud:
Slave     :gmail-backup:
Create    Slave
Expunge   Slave
Sync      Pull
# Exclude everything under the internal [Gmail] folder, except archived mails
Patterns  * ![Gmail]* "[Gmail]/All Mail"

Before going further we need to fetch Gmail’s certificates:

1$ openssl s_client -connect imap.gmail.com:993 -showcerts 2>&1 < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sed -ne '1,/-END CERTIFICATE-/p' > ~/gmail-backup/gmail.crt
2$ openssl s_client -connect imap.gmail.com:993 -showcerts 2>&1 < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | tac | sed -ne '1,/-BEGIN CERTIFICATE-/p' | tac > ~/gmail-backup/google.crt

Then all you have to do is to launch mbsync itself:

 1$ mbsync gmail
 2Reading configuration file ~/.mbsyncrc
 3Resolving imap.gmail.com... ok
 4Connecting to 173.194.78.108:993... ok
 5Connection is now encrypted
 6Logging in...
 7Channel gmail
 8Selecting slave MyLabel... Maildir notice: cannot read UIDVALIDITY, creating new.
 90 messages, 0 recent
10Selecting master MyLabel... 77 messages, 0 recent
11Synchronizing
12Pulling new messages........................................................

Now to keep your local backup fresh don’t forget to launch mbsync regularly in the background.

4 archived comments

Comments are closed. These were posted on Disqus, and are archived here.

  1. Which version of isync are you using?

    I'm using 1.0.5 but it doesn't support Gmail correctly (Gmail support is supposed to have improved in master branch in git)

    Apparently the use of Patterns like you suggest, or even Patterns [Gmail]/All Mail doesn't work in 1.0.4 or 1.0.5.

    1. I'm using the following .mbsyncrc with isync 1.1.0

      IMAPAccount gmail
      Host imap.gmail.com
      User [email protected]
      UseIMAPS yes
      CertificateFile ~/gmail/gmail.crt
      
      IMAPStore gmail-imap
      Account gmail
      
      MaildirStore gmail-local
      Path ~/gmail/
      Inbox ~/gmail/[Gmail]
      
      Channel gmail
      Master :gmail-imap:
      Slave :gmail-local:
      Create Slave
      Expunge Slave
      Sync Pull
      SyncState *
      Patterns *
      

      I'm ending up having ~/gmail/[Gmail]/cur, ~/gmail/[Gmail]/new ~/gmail/[Gmail]/tmp for Inbox.

      Gmail All Mail ends up in ~/gmail/[Gmail]/.All Mail
      etc

      1. Kevin Deldycke Author

        I'm using isync 1.0.4, the version distributed whith Ubuntu 12.04.

        The thing is, as I explained in a recent post, I discovered after writting this article that mbsync doesn’t support sub-folders/tags.

  2. Set "readonly = true" for your remote repository, and offlineimap will be one-way sync only.