Backup your Gmail account with getmail
For almost a year now I am using Gmail as a client to access all my mail accounts. In this article I am going to describe how to make backups of you Gmail account with getmail. Getmail is a simple, yet powerful console tool that downloads your mails and of course it is written in Python. Also because it makes a backup of the mails in an mbox-formatted file, you can access them with many mail clients (like Thunderbird) that support this format.
After installing it (for Debian: “sudo apt-get install getmail4”) you just have to create a file named “~/.getmail/getmail.gmail” that will contain the followings:
[retriever] type = SimplePOP3SSLRetriever server = pop.gmail.com username = email@example.com password = yourpassword [destination] type = Mboxrd path = ~/gmail-archive/gmail-backup.mbox [options] verbose = 2 message_log = ~/.getmail/gmail.log
Here you can find some other examples on how to write the above configuration file.
Also you have to create the empty mbox file, “~/gmail-archive/gmail-backup.mbox”, that will contain your backed-up mails.
Then, go to the Gmail’s settings and select from the “Forwarding and POP/IMAP” tab, “Enable POP for all mail (even mail that’s already been downloaded)”. Also make sure to select for the “When messages are accessed with POP”, “keep Gmail’s copy in the inbox”.
Now you are ready to backup your Gmail account.
In order to do that, run the following command in the terminal:
getmail -r ~/.getmail/getmail.gmail
You should see a long string of messages begin printing out as getmail starts grabbing the contents of your Gmail account.
Because Google has some limits on how many messages can be retrieved at one time you will have to run the command several times to retrieve all the mails.
Or just run something like the following:
#!/bin/bash # run the getmail command 1000 times for i in `seq 1 1000`; do getmail -r ~/.getmail/getmail.gmail done
It would be useful to auto-backup your mails periodically. Personally, I made a cronjob that checks for new mails hourly.
To do so, run:
And at the end of the document, add the following line:
@hourly /usr/bin/getmail -r path-to-your-home-folder/.getmail/getmail.gmail