Backing up gmail to Dropbox using getmail on Linux

What if gmail went away tomorrow, or at least deleted all your mail? It may not be as far fetched as you think. It's always prudent to have a backup.

On Linux, you can use the excellent getmail program to backup any POP3 or IMAP account. For gmail in particular, the first step is to enable IMAP access.

Installing and configuring getmail is simple.

sudo apt-get install getmail

# decide where you want you mail to live, and create an empty file there
touch ~/Dropbox/backup/gmail.mbox

mkdir ~/.getmail
vim ~/.getmail/getmailrc

# add the following
type = SimpleIMAPSSLRetriever
server =
mailboxes = ("[Gmail]/All Mail",)

type = Mboxrd
path = ~/Dropbox/backup/gmail.mbox

read_all = False # just get the new messages on subsequent runs
message_log_syslog = True

You can test whether your configuration working by running getmail manually from the command line. The initial download can take a while to begin if you have thousands of messages. The actual download will likewise take a long time for large mailboxes. While it's in progress, you will get a series of log messages on the console (and in syslog).


... several minutes later ...
  msg      1/105702 (325 bytes) delivered
  msg      2/105702 (335 bytes) delivered
  msg      3/105702 (363 bytes) delivered
  msg      4/105702 (180479 bytes) delivered
  msg      5/105702 (311 bytes) delivered
  msg      6/105702 (1057 bytes) delivered
  msg      7/105702 (818 bytes) delivered
  msg      8/105702 (331 bytes) delivered
  msg      9/105702 (321 bytes) delivered
  msg     10/105702 (882 bytes) delivered
  msg     11/105702 (1201 bytes) delivered
  msg     12/105702 (1614 bytes) delivered
  msg     13/105702 (359 bytes) delivered
  msg     14/105702 (359 bytes) delivered

I choose to configure getmail to use a single mbox file, but you can also use one file per message. In addition to this large file, getmail will keep a smaller index file in ~/.getmail.

If you want, you can setup a cron job to update your backup nightly.

crontab -e

# add the following line, which will run the backup at 1AM every day
* 1 * * * /usr/bin/getmail >/dev/null 2>&1