Backup Gmail with getmail - ArchWiki

Jump to content
From ArchWiki

This article or section is a candidate for merging with getmail.

Notes: Only #Troubleshooting is specific to Gmail. (Discuss in Talk:Backup Gmail with getmail)

We can use getmail to fully backup email messages from a Gmail account.

Emails will be backed-up in Maildir format, meaning that each email will be a separate text file, readable with any email client, or even with a text editor.

## Installing getmail

Install getmailAUR package.

## Creating required files and folders

Getmail reads its configuration from ~/.getmail/getmailrc by default. Unfortunately this directory and file do not exist by default, so we need to create them.

    $ mkdir ~/.getmail
    $ touch ~/.getmail/getmailrc
    $ chmod 700 ~/.getmail

We also need to create the folder where the emails will be backed-up:

    $ mkdir -p ~/bak/mail
    $ cd ~/bak/mail
    $ mkdir cur new tmp

For this example ~/bak/mail was chosen , but it could just as well be ~/mail. The cur, new and tmp folders are required by the Maildir format and by getmail.

## Configuring getmail

Edit the configuration file and add the entries below.

    ~/.getmail/getmailrc

    # More configuration options here:
    # http://pyropus.ca/software/getmail/configuration.html
    [retriever]
    type = SimpleIMAPSSLRetriever
    server = imap.gmail.com
    mailboxes = ("Inbox", "[Gmail]/Sent Mail") # optional - leave this line out to just grab the inbox
    username = USER
    password = PASS

Tip: If the mailbox `[Gmail]/Sent Mail` does not exist, try setting your Gmail's language setting to English. This setting can be different from the language setting of the Google account.

The retriever section tells getmail where to connect. It uses IMAP to connect to the server. For POP3 we can use the type SimplePOP3SSLRetriever, but we will also have to modify the server field. The mailbox which we backup will be All Mail.

Note that Gmail was called Google Mail in some countries like Germany. Even if all GMail accounts are migrated to the new name, the IMAP-Server still assumes Google Mail as the mail box directory. The mailboxes variable must be set to [Google Mail] in those cases. Also, you might need to replace box labels by whatever GMail calls that directory in the language of your Google account.

The username and password fields need to be changed to your own.

    [destination]
    type = Maildir
    path = ~/bak/mail/

The path field is the destination folder (which we created earlier) where the emails will be backed-up. All emails will be placed in 'new', and the cur and tmp folders will be left empty. This is normal, do not delete cur and tmp.

The options section is a bit longer:

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

This tells getmail to be very verbose and tell us the status of each message (whether it was backed-up successfully, total number of messages, etc). Also, everything will be logged to ~/.getmail/log.

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

    # do not alter messages
    delivered_to = false
    received = false

Setting delivered_to and received fields to false will prevent emails from being altered by getmail.

## Running getmail and adding a cron job

Now if we run getmail it will backup all Gmail emails to ~/bak/mail, outputting its status along the way.

We want to periodically run getmail to backup our Gmail account, so we will add a cron job:

    * * * * *   ID=getmail FREQ=1d getmail -q

The -q parameter will run getmail in quiet mode and only report errors.

## Migrating emails, importing old emails

This article or section is out of date.

Reason: The appendmail repo is gone. (Discuss in Talk:Backup Gmail with getmail)

Backing-up emails will suffice for most people, but if we want to migrate our emails to another account it gets a bit tricky. The appendmail script from http://bitbucket.org/wooptoo/appendmail[dead link 2020-12-20 ⓘ] can help us import emails to another account, be it Gmail or not. It can also be used to import old emails in our existing Gmail account.

Basically it reads every email file from its 'import' folder and puts it on Gmail or another account.

A direct link to download is http://bitbucket.org/wooptoo/appendmail/get/tip.zip[dead link 2020-12-20 ⓘ] It needs PHP to run, and you need to modify the username and password to yours.

## Troubleshooting

Depending on your Gmail security, you may be left with this error when running getmail:

    getmailrc: credential/login error ([ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/bin/answer.py?answer=78754 (Failure))
      0 messages (0 bytes) retrieved, 0 skipped

Since Gmail does not allow "lesssecureapps" anymore enable access for less secure apps you may want to turn on two-step-verification and then create a App password and put it as password into getmailrc (for POP3):

    ~/.getmail/getmailrc

    type = SimplePOP3SSLRetriever
    server = pop.gmail.com
    username = USER@gmail.com
    password = APPPASSWORD
    port = 995

Category:

- Mail retrieval agents

Hidden categories:

- Pages or sections flagged with Template:Merge
- Pages or sections flagged with Template:Out of date
- Pages with dead links