Backup Email with OfflineIMAP

I previously blogged about my HP ProLiant MicroServer which I’m playing with for doing backups of remote data and other “small/home office” tasks. I’ve decided to backup my GMail-based email using OfflineIMAP, which is awesome.

Offlineimap is in the Ubuntu repository and is pretty easy to setup once you know the format of the configuration file ‘~/.offlineimaprc’. Something like this is required at the top where the ‘accounts’ line can specify multiple accounts separated by commas.

Note: All of the sections below are in one file – ~/.offlineimaprc

[general]
accounts = alan_example,clare_example
ui = Curses.Blinkenlights
maxsyncaccount = 2

The “ui” parameter defines the look of offlineimap when it’s running. “Curses.Blinkenlights” gives a funky (but sometimes unstable) interface which is useful to use initially because it clearly shows offlineimap working. There’s a screenshot of this running below.

Next we need a section for each account using the name we used in the ‘general’ section above:-

[Account alan_example]
localrepository = gmailLocal_example_alan
remoterepository = gmailRemote_example_alan
autorefresh = 15

[Account clare_example]
localrepository = gmailLocal_example_clare
remoterepository = gmailRemote_example_clare
autorefresh = 60

Next a section to define where the local mail is stored:-

[Repository gmailLocal_example_alan]
type = Maildir
localfolders = /srv/offlineimap/example/alan

[Repository gmailLocal_example_clare]
type = Maildir
localfolders = /srv/offlineimap/example/clare

Finally the settings for the remote repository (your mail store) which in my case is GMail.

[Repository gmailRemote_example_alan]
type = IMAP
remotehost = imap.gmail.com
ssl = yes
remoteport = 993
remoteuser = alan@example.com
remotepass = password
maxconnections = 1
realdelete = no

[Repository gmailRemote_example_clare]
type = IMAP
remotehost = imap.gmail.com
ssl = yes
remoteport = 993
remoteuser = clare@example.com
remotepass = password
maxconnections = 1
realdelete = no

Of course your settings will differ for remoteuser and remotepass, but the rest should work fine. I then ssh to my server and run offlineimap inside Byobu (which I love) and that’s basically it.

Sometimes offlineimap crashes – especially if my internet connection goes down. When offlineimap crashes – or indeed when I quit it nicely, I sometimes get this:-

I then have to issue the reset command because the terminal session becomes unusable.

To avoid this, and to enable me to run offlineimap in the background (once I’ve proved it works okay) I simply change the setting for “ui” in the ~/.offlineimap file from:-

ui = Curses.Blinkenlights

to

ui = Noninteractive.Quiet

Which will cause offlineimap to run and then quit when it's finished. There's no need for the 'autorefresh' options above now, and I can schedule offlineimap to run via the following crontab entry so if my net connection drops it will restart sometime after it comes back up. It will also respawn a new offlineimap 30 mins after the hour if one isn't already running.

*/30 * * * * /usr/bin/offlineimap >/dev/null 2>&1

Now that's all setup I have a full backup of my gmail accounts on local storage. The really cunning thing is that I could point an email client at that store and access my mail locally, even if my Internet connection or GMail goes down.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
This entry was posted in Ubuntu. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Posted December 30, 2010 at 7:05 pm | Permalink

    First link (“blogged”) is truncated.

      More from author
  2. Posted January 3, 2011 at 4:03 pm | Permalink

    Nice post, Popey. I use almost an identical setup, and use sup-mail for my client. Thanks for posting!

    Dustin

      More from author
  3. Posted January 5, 2011 at 12:00 pm | Permalink

    Great tip! Always looking for new tasks to throw at my p4 home server/basement heater.

      More from author
  4. Posted February 8, 2011 at 9:36 pm | Permalink

    Under [general], shouldn’t it be maxsyncaccounts, rather than maxsyncaccount?

      More from author
  5. Posted May 18, 2011 at 4:57 pm | Permalink

    Awesome post only got to backing mine up today..
    Another nice thing you might want to add to your global or per account is

    folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail’,
    ‘[Gmail]/Trash’,’[Gmail]/Spam’,’[Gmail]/Starred’,’[Gmail]/Important’]

    This way you don’t duplicate ALL your emails by getting “All Mail” every thing in there is already in another folder.

    I added Important, Spam and Trash because honestly I don’t need a copy of those. (important because it will be another another folder as well)

      More from author

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType