Backing Up Email With Fetchmail And Procmail

Dec 6, 2013Ryan Burnette

I recently needed to back up an email account before deleting it. I tried several methods for doing this and found that using one of my Ubuntu-based VPS servers I could easily set up a background task that backed everything up.

I referenced several other articles to gather the information I needed. I tried many configurations before finding one that worked for me on Ubuntu 12.10 x64.

Here are the steps:

Install fetchmail and procmail

On Ubuntu this is quick and painless with the help of apt.

apt-get install fetchmail procmail -y

Create configuration files for fetchmail and procmail

Create a .fetchmailrc file.

touch ~/.fetchmailrc

Edit the configuration using your favorite editor and include the following. Note that I'm connecting to GMail in this example, but the settings will vary depending upon the requirements of your email server.

poll service 995 protocol pop3 username password "my_password" options ssl mda 'usr/bin/procmail -d %T'

Note the critical mda directive which lets fetchmail know to talk to procmail to deal with email that it fetches.

Let's create a folder in the home directory to hold downloaded email. And a folder within it for this account. We'll touch a log file for logging. Also, we'll create a .procmailrc file.

mkdir -p ~/email/my_username touch ~/email.log touch ~/.procmailrc

Edit the .procmailrc configuration using your favorite editor and include the following.

MAILDIR=$HOME/email LOGFILE=$HOME/email.log VERBOSE=on :0 my_username/

Replace my_username with the name of the folder you created to hold your downloaded email.

If everything is in place, we can fire it off and download all the email. I suggest running inside a screen so you can disconnect and walk away. In my case I was downloading several gigabytes of email. It can take a while, but it gets the job done.

fetchmail -nk -a -d 200 -N &


Blog Index