Backup Gmail Email in Linux

By | 2011/02/28

How can I backup my gmail email in Linux?

On Linux (and OS X) you can use the command line program getmail.

This how-to uses IMAP to connect to your gmail account. If you haven’t already, make sure to enable IMAP in the gmail settings!


1. Ok now install getmail in Ubuntu:

sudo apt-get install getmail4

2. Next make the configuration directory .getmail in your home directory:

mkdir ~/.getmail

3. Inside that directory, create the following config file:

cd .getmail
nano gmailimap.getmail

Here is my example config file:

[retriever]
type = SimpleIMAPSSLRetriever
server = imap.gmail.com
username = youremailaddress@gmail.com
password = yourgmailpassword
mailboxes = ("Inbox", "[Gmail]/Sent Mail") # optional - leave this line out to just grab inbox

[destination]
type = Mboxrd
path = ~/.getmail/gmail-backup.mbox

[options]
verbose = 2
message_log = ~/.getmail/getmail.log

4. Close and save that file. One last thing:

touch ~/.getmail/gmail-backup.mbox

5. Now run getmail. This may take a long time (30 minutes or more) depending on how much mail you have.

getmail -r ~/.getmail/gmailimap.getmail

How do I backup my sent mail separately? Or another folder?
Making a separate config file with this, for example, backs up only your sent folder:

[retriever]
type = SimpleIMAPSSLRetriever
server = imap.gmail.com
username = youremailaddress@gmail.com
password = yourgmailpassword
mailboxes = ("[Gmail]/Sent Mail",)

[destination]
type = Mboxrd
path = ~/.getmail/gmail-backup-sent.mbox

[options]
verbose = 2
message_log = ~/.getmail/gmail.log

For an alternative config, you could as well just grab the ‘All Mail’ folder to get everything in one big blob:

mailboxes = ("[Gmail]/All Mail",)

Cool!