nopriv.py is a Python script that allows you to back up your emails, which supports any IMAP compatible email accounts, such as Gmail for instance.
The tool backs up the emails to Maildir and to a browsable HTML5 archive, so you can easily browse all your emails offline with just a web browser. It supports multiple IMAP folders, attachments, incremental backups, HTML and text emails as well as multipart emails.
For now the script can't restore your email backups, but on its homepage there's a link to another script which can do this. Also, since this is just a script, the password is stored in plain text, but you can remove the password after the backup is complete.
Here's how an email backed up using nopriv.py looks like when opening it in a web browser:
In my test, nopriv.py took about half an hour to backup one of my Gmail accounts which has about 4400 emails. But obviously, this depends on your Internet connection speed, email attachements, IMAP server and so on.
For more info and how to use nopriv.py, head over to its homepage (scroll to "Usage" for instructions on getting the script and setting everything up).
thanks to Remy van Elst for the tip!