Maintaining a local Dovecot mirror

Mail-in-a-Box already has backup functionality, but I’ve been playing around with an alternative, complementary approach. Essentially backups solve two separate problems: 1) creating reliability through redundancy, and 2) keeping a history of changes. I’m tackling the first problem — creating redundancy.

What I want is an easy-to-set-up Dovecot IMAP instance that can host a local copy of all the mail that exists on my account on Mail-in-a-Box. If my server dies (or I lose internet), I can still access all my mail through the local IMAP instance. Thunderbird etc. can provide this with the right caching settings, but I don’t want a cache — I want a full copy, so that if I need to completely re-build my Mail-in-a-Box server, I can painlessly restore all my local mail to the freshly-built server and have everything back in minutes. And I can always be confident that the mirrored backup is working (important!) because I’m regularly accessing my mail via the local instance.

It’s just a proof of concept, but I’ve got some scripts to accomplish this in my standalone-dovecot-imap repo. It works pretty simply: sdimap init creates a new IMAP instance in the current directory. Run sdsync mirror youruser@yourdomain.com yourserver.yourdomain.com to copy all the mail from the server down to the local instance.

Ultimately what I really want is to keep only the last N days (90, 180, whatever) of email on my Mail-in-a-Box server. All the rest — the years of archived email — would be safely stored inside an encrypted volume on my home PC, The sdsync script would automatically pull down the latest mail (moving mail around locally if I happened to re-file it on the server) and automatically delete old mail from the server (after it reached a certain age). That way I get the benefits of having my email in the cloud, but without the risk of someone breaking into the server and having access to years of email archives.

Apologies in advance for the meandering post. I thought maybe there’d be a few others who are also interested in a set up like this.

1 Like

That’s a really nice idea! Consider writing a blog post, and/or a little flow diagram, to help make your system clear to more people. It took me a few re-reads to get it, but yeah, this sounds like a nice structure.

How do you manage automating the local sync? You have cron on a desktop or RPi or something?

Thanks for the encouragement. This was my first attempt at explaining the concept. As I refine it further, I’ll definitely have to put together an easier-to-digest explanation.

As for automating the sync, the primary use case I have in mind is mail stored in an encrypted volume on a home desktop. So the user mounts the encrypted volume, and then they can run sdsync -w … (-w for watch — already implemented) to constantly poll the server for changes. A cron job would be just as easy.

I hadn’t considered low-power, embedded environments, which would be cool. Right now the scripts are nothing more than a wrapper around Dovecot… so if it can run Dovecot it will run standalone-dovecot-imap.

A second use I’ve discovered for this script would be to update to a fresh install of Mail-in-a-Box. I’ve run into problems similar to issue #109 (https://github.com/JoshData/mailinabox/issues/109). As one approach to solving my problems, I want to attempt to copy all my mail off the server, re-install Mail-in-a-Box and copy the mail back.

In theory, the only steps necessary with standalone-dovecot-imap would be to:

  1. Local: Run sdimap init
  2. Local: Run sdsync mirror myuser@example.com box.example.com
  3. Server: Wipe the system clean
  4. Server: install Mail-in-a-Box, creating the myuser@example.com user
  5. Local: Run sdsync mirror myuser@example.com box.example.com

I haven’t tested the above steps yet, but maybe I’ll try it this week and report back.

Normally I’d say the user should just copy /home/user-data from one machine to the other. That should work to re-create the box.

1 Like