GMail IMAP Backup With mbsync on Ubuntu

Well, it sure has been a while since my last post on here. So I thought I’d kick it off with a discussion of how I went about getting my email backed up.

First, a description of my situation. I run all my email through GMail. I enjoy the interface and the fact that it is a cloud service; I can access my email seamlessly on my phone, my home computer, my work computer, some other computer, etc. However, I don’t want to lose all that information. Google is great, but who is to say that something terrible won’t happen and some (or all) of my mail is lost? So I wanted to setup some sort of backup. And then once I got that setup, make it automated.

At home, I run an Ubuntu box, that I just upgraded to 9.04, Jaunty Jackalope. This machine primarily serves as a media box, hosting video that streams to my Tivo off the 1.5TB RAID 5 array. I also use it as a network mounted TimeMachine box as well. Since I have extra storage on it, I figured I’d get something to sync my mail over IMAP periodically, and then I have a nice little backup.

After some searching, I came across two sites that had instructions using the utility mbsync (formerly isync). I found that following the instructions worked pretty well, though I had to customize the patch provided to get it to work with the version provided by Ubuntu.  And then I thought I’d detail my steps here for others to see.

  1. First, enable IMAP in your GMail account.
  2. Install the dependencies for mbsync:
    sudo apt-get install libc6 libdb4.8 libdb-dev libdb4.8-dev libssl0.9.8 libssl-dev
  3. Get the source for mbsync on Ubuntu with:
    sudo apt-get source isync

    This will download the source for mbsync and create a directory (in your current directory) named isync-1.0.4 (Yes, I know it is called isync. That is a legacy name).

  4. Now the normal mbsync install works just fine. However, it doesn’t support recursive directories. I use GMail labels setup to provide a hierarchy using forward slashes. It turns out that the fix to get mbsync to support directories like this is really easy to do and someone went through the trouble to get it to work. However, it doesn’t quite work on the version of the code packaged with Ubuntu. So I had to modify the patch a bit. The patch I created can be found here. Download it and put it somewhere on your system. To apply the patch, change directories into the isync-1.0.4 directory that was just downloaded and run:
    patch -p1 < /path/to/patch/file/recursive_imap_ubuntu.patch

    Once you’ve patched the directory, configure (./configure), build (make) and install (sudo make install).

  5. Now you should have an executable mbsync in your path. So it is time to start preparing to do the initial sync. Choose a place to store your backups. I chose
    /export/backups/mail/
  6. We want to connect to Google securely which means you’ll need the latest SSL certificates. To get those, use the openssl client on your machine. Run:
    openssl s_client -connect imap.gmail.com:993 -showcerts

    which should show two blocks of

    -----BEGIN CERTIFICATE-----
    ...
    -----END CERTIFICATE-----

    in the output. You’ll want to take each block (including the BEGIN/END CERTIFICATE lines), and put each of them into their own file. I put the first one in a file gmail.crt and the second one in the file google.crt (since the first signs imap.gmail.com which is signed by Google Internet Authority, the second certificate).

  7. The second certificate, the one for the Google Internet Authority, is signed by Equifax. So we’ll need Equifax’s certificate also. An as it turns out, Ubuntu has a copy of Equifax’s certificate already sitting in the repositories. Just run
    sudo apt-get install ca-certificates

    to get the latest CA certificates. After installing the CAs, Equifax’s CA sits at /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt, which we’ll need in the configuration file in the next step.

  8. Now we can write the configuration file we are going to use. Here is a copy of mine:
    IMAPAccount gmail
    Host imap.gmail.com
    User yourusername@gmail.com
    UseIMAPS yes
    CertificateFile /export/backups/mail/gmail.crt
    CertificateFile /export/backups/mail/google.crt
    CertificateFile /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt 
    
    IMAPStore gmail-remote
    Account gmail
    
    MaildirStore gmail-local
    Path /export/backups/mail/gmail/
    Inbox /export/backups/mail/gmail/Inbox
    
    
    Channel gmail
    Master :gmail-remote:
    Slave :gmail-local:
    # Exclude everything under the internal [Gmail] folder, except the interesting folders
    Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail"
    Create Slave
    Sync Pull
    SyncState *

    Check out the Patterns line. That is where you would include or exclude various labels. All lables are stored at the root of the hierarchy, with the special directory [Gmail] having things like ‘Sent Mail’, ‘Spam’, ‘Starred’, etc in it. I wanted to exclude all the items in the [Gmail] directory except for the ones listed. The ‘*’ at the beginning includes all other labels. You will also want to change the Path and Inbox lines to point to your mail location, as well as the first two CertificateFile lines. Also, be sure to enter your actual GMail login on the User line. Now save this file somewhere. Note: saving it as ~/.mbsyncrc will cause it to be automatically loaded when mbsync is run, meaning you don’t need to specify which config file with the -c option.

  9. Now go ahead and test it out by listing the labels in your account with the command mbsync -l -c /path/to/the/configfile.rc gmail. Running it will look like this and ask you for your password:
    [streeter@scout]:~$ mbsync -l -c ~/.mbsyncrc gmail
    Reading configuration file /home/streeter/.mbsyncrc
    Resolving imap.gmail.com... ok
    Connecting to 209.85.199.109:993... ok
    Connection is now encrypted
    Logging in...
    Password (yourusername@gmail.com@imap.gmail.com):
    Channel gmail
    lists/code
    bills
    archive/cron
    archive/classes
    archive/work
    [Gmail]/Starred
    [Gmail]/Sent Mail
    [Gmail]/All Mail
    INBOX
    @followup
    [streeter@scout]:~$

    If you see something like this, then it worked! Now just go ahead and start your first mail download with mbsync -c /path/to/the/configfile.rc gmail And then you get to wait while it finishes which can take a while depending on how much mail you have.

You can go an extra step and save your password in the file. If you add the line Pass yourpasswordhere right after the User line, you won’t be asked for your password and can then setup a cronjob to automatically keep mail locally on a schedule. However, your password for your Google Account then is stored in cleartext on your machine. So only do this is you absolutely know what you are doing and who can access the machine.

Hopefully this becomes useful for people.

Update 4/18/2011:

I just updated the directions slightly for Ubuntu 10.10 Maverick Meerkat. For Maverick, I had to also install libssl-dev to get it to work with recursive directories.

  • http://www.facebook.com/people/Michael-Justman/1656050848 Michael Justman

    Looking for exactly this guide a few days ago. Thank you.

    Wondering if you could create a tutorial for accessing the stored emails via IMAP with a client locally.

  • Time132

    Thank you very much

  • http://www.chrisstreeter.com Streeter

    Michael, I’ll look into that some point. Right now, I just access the mail with mutt. If I’ve got my mail at

    /export/backups/mail/gmail/Inbox

    Then I run:

    mutt -f /export/backups/mail/gmail/Inbox

    and I can view the mail. You can actually delete and stuff, but I believe that mbsync will just overwrite the changes since the local mailbox is marked as a slave.

  • Brandon Hall

    You don’t need to download the Google certificates or specify them in the configuration file; it’s sufficient to just supply the certificate for the root authority (in this case, Equifax).

  • http://da-y.com/ dmitri yakimov

    very useful, thanks.
    had a problem with cerificates, now resolved.

  • Alexei

    Excellent steps. Thanks !
    Small comment:
    Step 2 should also include libdb4.2-dev, otherwise ./configure will fail

  • JW

    Hi Chris,

    From your searching, did you come across anything for a migration? For example, I want to switch web hosts and would like a tool which allows me to to backup/restore locally, without all the extras. I have no need to use local readers:

    imap-tool –backup –dir ~/My–Messages –server mail.example.com –user …

    Later, after I switch web hosts (and re-create the users):

    imap-tool –restore –dir ~/My-Messages –server mail.example.com –user …

    Everything I’ve seen, from how to use imapcopy and imapsync, to this article, seems *really* complex. It seems to be this process should be much easier. Am I being too unreasonable?

    JW

  • Pete

    Using Ubuntu 10 I could not get ./configure step to work. I could not get libdb4.2. Not being skilled at things UNIX I got stuck there. However I found another program called offlineimap which was an easy setup and is now working. Here’s a link to instructions:

    http://pklaus.blogspot.com/2010/02/offlineimap-to-have-local-copy-of-your.html

    • http://www.linux-prof.nl remke

      I have installed isync using ubuntu 10.10:
      A)
      sudo aptitude search libdb-dev
      -> sudo aptitude install libdb-dev
      B)
      sudo aptitude search libssl
      -> sudo aptitude install libssl-dev
      C)
      sudo apt-get source isync

      wget http://www.chrisstreeter.com/wp-content/uploads/2009/04/recursive_imap_ubuntu.patch
      tar -xvf isync_1.0.4.orig.tar.gz
      cd isync-1.0.4/
      sudo patch -p1 < ../recursive_imap_ubuntu.patch
      D)
      sudo ./configure && sudo make && sudo make install
      E)
      sudo mkdir -p /export/backups/mail

      (my user is member of the group users …); you need an writable location for the user performing the gmail backup !
      sudo chown -R :users /export/backups

      sudo chmod g+s /export/backups/mail/


      continue with step (5)
      :) ~remke

    • Philipp

      Hello Pete,
      as you refered to my blog post, I want to inform you that my blog moved. The post is now online at the new address:

      http://blog.philippklaus.de/2010/02/offlineimap-have-a-local-copy-of-your-mail/

      Hope it is of some use.
      Philipp

  • KR

    @Pete

    I just did .configure && make without problems on ubuntu 10 – you need the dev versions of libdb4.#, # being 7 or 8 or thereabouts

  • Pingback: Getting mails via IMAP with mbsync « 503: Service Unavailable

  • Pingback: Kbart.org » Blog Archive » Backup Gmail using IMAP and mbsync

  • Guy

    Thanks, Chris. This worked really well for me. Exactly what I was wanting to do.

  • Pingback: My quest to setup a good gmail archive system | JCStorm

  • http://www.bettssoftware.com Craig Betts

    With the big scare that hit Google today with a mass of missing emails, I was able to use your setup with no hiccups! Apparently mbsync didn’t need the patch for the recursive directories. I am running Ubuntu 10.04 LTS and loaded the isync package as is.

    Thank

  • ehznewehznew

    I’m not scared at all.

    Aperture sucks!

  • Muc

    Great post! Thank you very much. Just followed the steps and worked like a charm :)

    I had a little trouble, for I’m using linux mint Julia, which apparently doesn’t include any sources source (no deb sources in sources.list), so I had to manually download the sources for mbsync, but other than that, very straightforward.

  • KeithS

    Thanks for this! Your patch worked perfectly against the isync-1.0.4 package available for download from Sourceforge. With a little tweaking to set up the configuration file the way I like it, it’s synchronizing nicely.

  • Mike Smullin

    Thunderbird 3 does this really well. Just add your IMAP account. The default under Storage for each account is to download all content and cache locally forever. Then go to File > Offline > Download & Sync and it will go through all your accounts and all folders and download them to the local cache. Then you can go into Work Offline mode and read all your email to verify its all there.

    Then as you use Thunderbird its updating its cache, but you can always force a fresh sync with above steps.

  • Jarno Rantanen

    Thanks a lot for a quality post! This got me right on the way for setting up my own local backup of my (non-gmail) IMAP account.

    You might want to update the info about fetching local copies of the certs though; as Brandon Hall pointed out it’s not actually necessary.