Backing up Gmail on an Ubuntu mailserver

Backstory

I used to run my own Courier/Postfix mailserver to manage my family’s email (technically, I still do, but more later). Of late, I’ve been finding this to be a little bit of a hassle. Mostly because my laptop battery occasionally dies and I want to check my email from a public computer. This is a less than optimal situation. I have to run a webmail program on the server (RoundCube is nice though) and I have to type my server login into a strange computer. So I decided to switch to Gmail and it is wonderful. I like the interface, I can use a different password for it than for my home network, and somebody else does the work of keeping it running.

But I’m not too thrilled about all my email “living in the cloud.” I trust Google. I have no choice. If I’m going to give personal information to anyone, it would probably be to Google (since they probably already know more about me than I do). But there is always the chance that they will turn off POP/IMAP access to my email and it will be trapped there. Multiple simultaneous meteor strikes could destroy the data centres where my email is housed. If that happens, I’ll be unable to find all my LOLcat emails. That just wouldn’t do.

Solution

Enable POP on Gmail, download the email with Fetchmail, archive it to Amazon S3 once a month.

POP on Gmail

Gmail POP

In Gmail: open Settings; choose the “Forwarding and POP/IMAP” tab; choose one of the “Enable POP” options.

Download with Fetchmail

You’ll need to have a mailserver running on your computer. I suggest Postfix. You’ll probably also want to get access to your mail. If you’re installing this on your desktop, then most clients will be able to access your Maildir directly. If you’re installing this on a server, I suggest Courier. Both of these are available through apt. I would suggest reading a more detailed tutorial (see esp. pg. 5-6) if you have never done this before.

You will of course need fetchmail (sudo apt-get install fetchmail). You need a config file (sudo touch /etc/fetchmailrc & chmod 600 /etc/fetchmailrc) with your email addresses and passwords like so:

set daemon 3600

poll pop.gmail.com port 995 with protocol pop3
        user {gmailuser}@gmail.com with password {gmailpassword} is {localuser} here options ssl

Replace {gmailuser} with your Gmail username, {gmailpassword} with your Gmail password, and {localuser} with your username on your Linux box. Run sudo /etc/fetchmail restart and your email should be getting downloaded to your computer.

Archiving

You might be fine stopping there. You now have a local copy of all your mail. On the other hand, maybe you want more. It would be nice to have the mail sorted into folders automatically and it would be nice to have a second copy. I have a cron.monthly script that automatically sorts my Inbox into folders by month and year and copies the archived mail to Amazon S3.

The cleanup-maildir python script can be found here (check the comments for a bug fix). Copy that to somewhere in your $PATH (try /usr/local/bin). My archive script can be found here (or Bzip2 compressed). Copy it to someplace like /usr/local/bin and create an executable file in /etc/cron.monthly which contains a line like: maildir-backup /mnt/backup/Mail/username username. The first parameter is the backup destination and the second one is the local user to back up.

You can use the command-line version of JungleDisk to mount your Amazon S3 space and set that as your backup destination.

Hopefully this helps somebody someday. If nothing else, now I’ll remember what I did for when I upgrade my server next.

Related Post

Read More
Read More
Read More
Read More