munchymail a mail archive that keeps every copy of every message

Live syncing

The service (mmadm serve) keeps live origins current itself: Gmail and IMAP every 3 minutes, an IMAP INBOX at once through IDLE, munchymail sync on request, and failure handling. A live mbox tree is imported only by sync or mmadm import; serve its mail over IMAP instead and keep the tree as a snapshot.

Which origins are synced

Every origin has a role, stored in the database:

Role Meaning Synced
live a mailbox still in use (mailhost-dovecot, gmail) yes
snapshot a copy of a mailbox as it was (a backup, a found file) never
export a service’s export (a Takeout) never

An origin with no role is never synced, and neither is a retired one. mmadm origin role LABEL ROLE sets it; the upgrade makes the Gmail origin live and leaves the rest unset.

Gmail

IMAP

An IMAP origin’s root is the server’s URL; how to log in is in config.toml:

mmadm origin add mailhost-imap imaps://imap.example.org --role live
[accounts."me@example.org".origins."mailhost-imap"]
imap = "imaps://imap.example.org"
user = "me"
# Run at each login; the password stays in memory. A failure (e.g. a keyring
# still locked after a reboot) is retried, not a reason to stop the origin.
password_command = ["secret-tool", "lookup", "service", "imap", "user", "me"]
# For mutual TLS, and a server certificate from a private CA:
client_cert = "~/.config/munchymail/creds/imap.cert.pem"
client_key = "~/.config/munchymail/creds/imap.key.pem"
server_cert = "/etc/pki/my-ca.pem"
# Optional: only these folders; a trailing * matches the rest of a name.
# folders = ["INBOX", "lists/*"]
# Optional: the service keeps its sync connection between checks, replaced
# once idle this long (default 25; Dovecot logs out at 30); 0 logs in each time.
# keep_minutes = 25

Why not watch mbox trees directly

A rescan re-reads every file under Dovecot’s shared lock. Measured on a 5 GB tree: 13 s with nothing new, and the lock on a multi-gigabyte INBOX held for 8–9 s, during which Dovecot can’t deliver to it. Reading only what was appended would shorten the lock but miss Dovecot’s in-place header rewrites. IMAP gives the same changes exactly, through Dovecot’s own locking.

Failures

Kind Example What the service does
transient network down, Google 5xx, quota retries with backoff, up to an hour apart
one origin token revoked, mailbox path gone stops syncing that origin and says so in overview; mmadm origin resume LABEL restarts it
systemic database corrupt or unwritable, disk full exits with status 78 and stays down

The unit must then not restart it forever:

[Unit]
StartLimitIntervalSec=10min
StartLimitBurst=5

[Service]
RestartPreventExitStatus=78

Unit changes