Artemis | Blog | Portfolio | About & Contact

2020 / 10 / 31 - Software discoveries (raw)

Backing up e-mails from an IMAP server

E-mail is what connects most people.

It holds most of our accounts, helps us communicate with one another, and can be used for a variety of tasks, including shopping lists, personal notes, and much more. With that many responsibilities in place, we can surely attest that our mailboxes are both pretty important and sensitive, we should not lose access to them, and we should avoid sharing their contents as much as we can.

Still, running your own e-mail server infrastructure is an absolute pain, for a large variety of reasons, including anti-spam flagging (DKIM, SPF, and other stuff to maintain), blacklists avoidance (because Microsoft and Google are always frisky with their blacklists), and some other problems.

Some choose to handle this cost, resulting in an infrastructure they have absolute control over, and for which they can tailor their backup solutions. Some others aren't so inclined to partake in such tedious maintenance, and choose instead to put their trust in some companies1.

Both choices are fine per-se, but they both fall under the same issue: "what happens if my host dies?". This question could be talking about a temporary network outage, a database corruption issue (data loss), or the service outright dying or blocking you without rescourse.

The common result in all those issues (and many more I haven't mentioned) is that you lose access. A good idea is to keep a replica of them somewhere, ideally not on the same server used to host them. Some e-mail services offer automated backups, but since I don't know their backup policy (how the backups are handled, how reliable is the system, and other considerations), I prefer to have my own, which stores files the way I want, in a storage of my liking.

For that, you have multiple solutions and tools you can set up.

The nifty tool getmail is the one I chose to go with. With it, you could for example choose to move your e-mails from your "main" IMAP server to another IMAP server (e.g. the one provided and automatically configured by Synology on their Synology DSM NAS OS shipped with all their NAS), but I chose another solution: to move the e-mails from the main IMAP server to a local storage, i.e. as a list of maildir-formatted files.

This allows me to get all my e-mails as text documents on my local computer, so I can access, search, and index them as I wish at any given moment.

Setting up getmail for local download of e-mails

The tool getmail is configued by a configuration file stored by default at ~/.getmail/getmailrc. For this example, we'll want to backup our e-mails at ~/Documents/my-emails.

The configuration format, as described in their documentation, is pretty straightforward in our case.

We want to get our e-mails from the IMAP server our mail provider gives us and save them all in our folder (given above).

We start by configuring our IMAP source.

[retriever]
type = SimpleIMAPSSLRetriever
server = mail.my.domain
username = user@mail.my.domain
password = this-is-a-test-password
mailboxes = ALL

We then have to point how and where we want to send them.

[destination]
type = MailDir
path = ~/Documents/my-emails

If we run the command getmail right away, we'll notice that it indeed works! Well, kinda. The issue here is that every unread e-mail will be flagged as read once the tool handled them, which isn't really what we want...

For this reason, we must add another configuration entry to disable that.

[options]
read_all = False

And that's it!

You can just throw it in a systemd timer or a crontab, and be done with it, it will silently run and copy all your e-mails the way you want!

*/15 * * * * getmail

For reference, this configuration is almost mine, and I chose to put the nifty tool restic behind getmail to automatically encrypt and backup my e-mails in a snapshot-based backup setup on my NAS in addition to the local clone.


  1. As a personal note, I used to self-host my e-mails, but ultimately moved to FastMail due to the maintenance cost which was burdening me.