Skip to Content

Backup Gmail using Getmail

Posted on

Perish the thought … But if you lost access to the contents of your Gmail account, how bad would it be? Not a good day, but not so bad if you have a backup! I use getmail to backup all my Gmail messages to my home server.

Let’s go!

0. Enable IMAP access to Gmail in Settings->Forwarding and POP/IMAP.

1. Install getmail and create mail directories …

$ sudo apt install getmail4
$ mkdir -m 0700 ~/.getmail
$ mkdir -p ~/mail/{cur,new,tmp}

2. Create the configuration file ~/.getmail/getmailrc

type = SimpleIMAPSSLRetriever
server =
mailboxes = ("[Gmail]/All Mail",)
username =
password = SUPerS3CR3TPASSWORD

type = Maildir
path = ~/mail/

# only download *new* emails
read_all = false
# do not delete emails on server
delete = false
# do not alter emails
delivered_to = false
received = false
# maximum downloads each run; 0 for no limit
max_messages_per_session = 0
# verbosity ... (2) print all actions; (1) print messages about retrieving and
# deleting messages; (0) only print warnings and errors.
verbose = 1
message_log = ~/.getmail/log

3. Run command …

$ getmail -v

Getmail connects to the server and downloads any new messages into ~/mail/new/. When it runs the first time it may take a few minutes before it starts retrieving messages, and downloading the contents of All Mail may take several hours.

4. Setup a cronjob using the command crontab -e to run (for example) a daily backup at 05:45 …

# m h  dom mon dow   command
45 5 * * * /usr/bin/getmail

5. Helpful links:

Happy hacking!