Posted in Howto Linux Misc

Backup your Gmail account in maildir format using fetchmail

March 19, 2008 - 6 comments

First of all, enable POP on your gmail account :

- settings
- Forwarding and POP/IMAP
- Enable POP for all mail (even mail that’s already been downloaded)

Then, the configuration for fetchmail and procmail :

You need to use procmail in order to be able to store the mails in maildir format (1 file per mail, mbox format is 1 big file for all emails)

/home/USER/.fetchmailrc :

poll pop.gmail.com
protocol pop3
username "ACCOUNT@gmail.com" password "PASSWORD"
keep ssl
mda "/usr/bin/procmail -m /home/USER/.procmailrc"

We are using the pop3 protocol, we keep messages on the server and use the SSL server to retrieve our emails from.

/home/USER/.procmailrc :

MAILDIR=/home/USER/
VERBOSE=on

:0
Mail/

Initial retrieval :

The first time, you’ll need to run fetchmail in USER’s session manually to retrieve your mailbox. Indeed, after a while fetchmail will stop retrieving emails, so you need to run it again a couple of times before it’s done. I guess gmail limits the number of emails you can retrieve in a session. You should set up a regular cronjob to retrieve emails automatically, you shouldn’t wait too long between two retrievals to avoid missing emails due to the restriction.

Setting up a cron job in the USER’s session to run the backup every night :

crontab -e

0 1 * * * /usr/bin/fetchmail > /dev/null 2> /home/USER/Mail/fetchmail.log

I set up mutt to read my backup emails from the CLI :

/home/USER/.muttrc :

set mbox_type=Maildir
set folder="~/Mail"
set mask="!^\\.[^.]"
set mbox="~/Mail"
set record="+.Sent"
set postponed="+.Drafts"
set spoolfile="~/Mail"

Comments

sr

June 11, 2008 - 1:55

thanks a lot for the tip! / merci beaucoup

jay

March 9, 2009 - 5:47

works along with this link http://devnulll.blogspot.com/2007/09/howto-mutt-and-gmail-on-debian_17.html I follow both but used your fetchmail method instead of getmail. thanks

Cristiano - Brazil

March 27, 2009 - 17:04

Hi Sébastian , very nice your documentation, but I want know more about , if fetchmail support md5 or crypt in password, reggards.

Cristiano.

skype: cristianodebian
msn: cristianolinux at gmail.com
gtalk: cristianolinux at gmail.com

Sebastien Wains

March 27, 2009 - 21:06

Hi Cristiano,

No, apparently the developer “refused” to develop such a feature..

http://catb.org/~esr/writings/cathedral-bazaar/cathedral-bazaar/ar01s09.html

“Another lesson is about security by obscurity. Some fetchmail users asked me to change the software to store passwords encrypted in the rc file, so snoopers wouldn’t be able to casually see them.

I didn’t do it, because this doesn’t actually add protection. Anyone who’s acquired permissions to read your rc file will be able to run fetchmail as you anyway—and if it’s your password they’re after, they’d be able to rip the necessary decoder out of the fetchmail code itself to get it.”

Cristiano - Brazil

March 28, 2009 - 14:55

Ok Sébastien , I’ll see about this.

At case :
.fetchmailrc

pass “test”

Can I , change this password for md5 ou crypt?
I not want password, in mode plaintext.
reggards,
Cristiano

Sebastien Wains

March 28, 2009 - 18:06

Err, no, that’s what I said in the previous message.

If someone can read your fetchmailrc, he could run it and access your emails, password encrypted or not. So just set proper permissions on the file and you’re safe.

Leave Comment

Please consider visiting the partners below if you enjoyed this article :

If this post saved you time and money, please consider checking my Amazon wishlist.

Before submitting, some rules :
- Is your comment related to the article ?
- You're having a problem ? Have you checked Google, other howtos, docs, manpages ?
- You're still having the problem ? Have you raised log verbosity, checked traces, ran tcpdump ?
- Have you checked your configuratoin for typo ?
Unless your comment is providing additional info or respect the rules above, DON'T comment.
If you don't understand what you are doing, I urge you to read the documentation, I'm not your free Level 1 helpdesk guy.