
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!



Oooh nice!
Scott, what do you see as the tradeoffs of pulling them down via IMAP as opposed to just POPing them?
Twitter: scottlinux
Cool yeah I think Gmail throttles or limits POP downloads whereas IMAP connections are wide open. So it would take running something like this several times to grab everything via POP if you are doing a one time backup. And that only gets your inbox as well.
Oh yeah, the throttle! Cool. Yeah, actually the throttle is pretty severe…just a trickle of a couple of messages on each check, so it takes a loooong time to pull down a big inbox.
Hi Scott,
Thanks for this. I was able to run the script using the ‘AllMail’ option. It seems to have worked fine.
However, it retrieved 1597 messages whereas gmail’s ‘All mails’ shows that I have 1727 messages.
Are there any limitations that may have affected this?
Thanks.
Cool, thanks.
Please note everyone that the procedure here described leaves your email credentials vulnerable as the configuration file is accessible to the current user. You’ll have to either “chmod -r” the configuration file and run getmail as superuser, or delete the configuration file upon completion.
Will this download contacts and calendar’s also? Also of simple imap backups miss these.
Twitter: scottlinux
Nope – email only. Cheers,
Thank you for this Scott, very helpful
Is there any way of automating this task?