Email backup

How to Back Up Several Email Accounts into One Archive

Updated 3 min read

On this page
  1. Connect each account with mailmd
  2. Choose what the archive includes
  3. Keep a separate backup of the archive

If you use several email accounts, one organized archive keeps their messages together and shows which account each message came from. An archive is a collection of messages kept for later access. A backup is an extra copy in case you lose the computer or its files.

mailmd is a free, open-source tool you run by typing commands in a terminal. It copies messages from multiple mailboxes to your computer as Markdown files, a plain-text format, with one file per message. It leaves the original mail in place. The archive has a separate folder for each account.

Connect each account with mailmd #

First install mailmd in a terminal. On macOS or Linux, or Windows using Git Bash, run:

curl -fsSL https://raw.githubusercontent.com/vincentsch/mailmd/main/install.sh | sh

Then connect each mailbox using the command that matches its provider:

mailmd connect imap --email you@example.com
mailmd connect gmail --client-secret client_secret.json
mailmd connect outlook --client-id your-client-id

Use the IMAP command for mailboxes that support IMAP, including Gmail with an app password. IMAP is a standard that lets email programs access messages in a mailbox. Some providers require an app password, and mailmd tells you when you need one.

Apps connect to Gmail through Google's Gmail API, and to Outlook.com and Microsoft 365 through Microsoft Graph. To use either service, create an app in Google Cloud or Microsoft Entra and connect with OAuth. OAuth lets you give an app specific access without sharing your regular account password. mailmd uses the Gmail API for Gmail and Microsoft Graph for Outlook.com and Microsoft 365 because many Outlook mailboxes no longer accept IMAP passwords. If sign-in is blocked, check the providers’ Gmail client guidance and Outlook authentication guidance.

After connecting each account, run:

mailmd sync

Messages are saved under ~/mail/<account>. Each account has its own folder, with subfolders for messages and years. Each Markdown file starts with the message headers, which contain message details. Messages written in HTML become readable Markdown, and attachments are saved beside them. To keep an original .eml copy of each message too, add --keep-raw to the sync command.

mailmd only reads mail folders and requests permission to read them. It does not mark messages as read, move or delete them, or send mail. Later syncs add new messages and update each message's read and starred status in the archive; if a sync stops, saved files remain and the next run continues. See the mailmd project documentation for setup details.

Choose what the archive includes #

By default, spam and trash are left out. Gmail over IMAP syncs All Mail, which includes archived messages. Use --folder to choose folders. Use --exclude to leave folders out, and --since to set a date cutoff.

Before the first full sync, decide whether to include drafts or junk. Keep account folders separate so you can tell which account a search result came from. The archive also includes an index.jsonl file that lists messages for scripts. To search, open the files in an editor or note app, use rg or grep to search them, or use jq to filter the index. mailmd does not have its own search engine.

Keep a separate backup of the archive #

The mailmd archive is a local copy, so back up its folder separately to another drive or backup location; a copy on the same computer will not protect it if that computer is lost or damaged. Keep a backup disconnected when practical, and test that you can open files from it. The Canadian Centre for Cyber Security’s backup guidance recommends keeping copies separate and testing recovery.

To keep the archive current, schedule mailmd sync --quiet to run regularly. You can use cron, systemd, launchd, or Task Scheduler. After the first sync, open messages and attachments from each account. Check expected folders and dates, and confirm that excluded folders match your intentions. mailmd never removes archive files; a message later deleted from the mailbox remains in the archive marked as deleted on the server.

Free and open source

Try mailmd

mailmd is a free command-line tool that copies your email into Markdown files on your computer, one file per message. It works with any IMAP mailbox, and with Gmail and Outlook through their own APIs.

Install

$ curl -fsSL https://raw.githubusercontent.com/vincentsch/mailmd/main/install.sh | sh

Set it up with your coding agent

Set up mailmd so you can read my email. Use https://github.com/vincentsch/mailmd: read its AGENTS.md and follow the steps. Connect my mailbox and run the first sync. Then show me where the files are.

More on email backup