How to Back Up Email with Getmail


2013-08-31

It's pretty important to have a backup of your email. Seriously, what would you do if you lost those cat gifs that your friend sent you? I use a program called getmail and a Raspberry Pi to back up my email automatically. This guide shows you how to use getmail to back up email from a Gmail account. It's also focused on using a version of Debian GNU/Linux, but anything Debian or Ubuntu-based will likely work with these directions.

First, you need to install getmail:

sudo apt-get install getmail4

Next, set up your getmail configuraton. You can edit the file with:

nano ~/.getmail/getmailrc

This getmailrc configuration file needs to have something like this in it:

# More configuration options here:
# http://pyropus.ca/software/getmail/configuration.html
[retriever]
type = SimpleIMAPSSLRetriever
server = imap.gmail.com
mailboxes = ("Inbox",)
username = yourgmailuser
password = yourpassword

[destination]
type = Mboxrd
path = ~/mail.mbox

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

# do not alter messages
delivered_to = false
received = false

# retrieve only new messages
# if set to true it will re-download ALL messages every time!
read_all = false

It's not very wise to have your Gmail password in plaintext in a file on your computer. You can use Gmail's application specific password to make this a little more secure, but not much. You can read about other password storing options here, but they will reduce your ability to run this automatically. After you set your configuration, you can test it buy entering the command getmail in a terminal.

I use a cron job to automatically back up my email every hour. Type this in a terminal to open your crontab:

crontab -e

Then add this at the bottom:

0 * * * * getmail

That will run getmail every hour and retrieve any new emails. The emails are stored in the file name mail.mbox in your home directory. You can read this file and all the email in it with mutt.