What can I use to automate backups of Gmail?

I'd like to automate the process of locally backing up my Gmail account, and am looking for a solution that:

  • can be scheduled to run in the background
  • does not cause messages to be marked as read

What resources are available for doing this in Ubuntu?


Solution 1:

offlineimap Install offlineimap is a popular solution for this.

Enable IMAP in GMail

(Google's instructions)

  1. Sign in to Gmail.
  2. Click Settings at the top of any Gmail page.
  3. Click Forwarding and POP/IMAP.
  4. Select Enable IMAP.
  5. Configure your IMAP client and click Save Changes.

Setup offlineimap

After you install it create a .offlineimaprc

[general]
accounts = GMail
maxsyncaccounts = 3

[Account GMail]
localrepository = Local
remoterepository = Remote
autorefresh = 10  #refresh every 10 minutes
quick = 5         #Quick-syncs do not update if the only changes were to IMAP flags

[Repository Local]
type = Maildir
localfolders = /whatever/directory

[Repository Remote]
type = IMAP
remotehost = imap.gmail.com
remoteuser = [email protected]
remotepass = your_password
ssl = yes
maxconnections = 1

#Setting realdelete = yes will Really Delete email from the server.
#Otherwise "deleting" a message will just remove any labels and 
#retain the message in the All Mail folder.
realdelete = no 

Then you need to run offlineimap.

Automating offlineimap with cron

In some cases having offlineimap running all the time can consume large amounts of memory, especially if you have a large mailbox, so you can add an offlineimap check into cron to just check it and then exit, here's a daily backup:

 @daily offlineimap -u Noninteractive.Basic
  • How do I set up a Cron job?
  • cron

Backing up offlineimap

The files will be in whatever you define in the localfolders line as a Maildir. You can back this up using whatever backup method you use:

  • Comparison of backup tools
  • backup

References

  • http://www.enigmacurry.com/category/gmail/
  • http://blog.nixternal.com/2010.10.18/offlineimap-and-byobu-hacks/
  • https://wiki.archlinux.org/index.php/OfflineIMAP
  • Man Page
  • Fully documented offlineimaprc configuration file

Solution 2:

An alternative would be to use fetchmail. fetchmail can use both pop3s and imap4s to access Google. There are online tutorials on how to configure.