How to Back Up Email on Linux
On this page
If your email lives in Gmail, Outlook, iCloud, or another mail service, a local copy protects you when you lose access to the account or delete messages you still need. IMAP, a protocol that lets mail programs access messages stored on a mail server, is one common way to read a mailbox and copy its contents.
For a Linux backup that stays as ordinary files on your computer, mailmd is the recommended option. It copies messages into a folder of Markdown files and leaves the mailbox unchanged. You will need to connect it to your mail account, run a first copy, and back up the resulting folder somewhere separate from your computer.
Why a mail sync is not always a backup #
A sync keeps two copies aligned, which means a deletion on one side can affect the other. A Unix Stack Exchange user described losing thousands of Gmail messages after a sync; a second computer that was offline still had a copy. A backup should preserve saved messages even when someone later deletes them from the mail server.
mailmd keeps archive files after server-side deletion and marks those messages as deleted on the server. It also opens mail folders read-only and requests read access. It does not mark messages as read or move or delete them. It also does not send mail. Those behaviors make it suitable for keeping a separate archive, though you still need to protect and back up the archive folder itself.
Back up mail with mailmd #
mailmd is a free, open-source command-line tool. It saves each message as a Markdown file with its headers, such as sender and subject, in frontmatter at the top. It turns HTML email into readable Markdown. It also saves attachments beside their messages and organizes folders and years into directories. The --keep-raw option also saves each original message as an .eml file.
Install mailmd on Linux:
curl -fsSL https://raw.githubusercontent.com/vincentsch/mailmd/main/install.sh | shThen connect the account using the option that matches its mail service:
- For an IMAP mailbox, including Gmail with an app password, run
mailmd connect imap --email you@example.com. Some providers require an app password; mailmd indicates when one is needed. - For Gmail through the Gmail API, run
mailmd connect gmail --client-secret client_secret.json. This requires your own OAuth app in Google Cloud. - For Outlook.com or a Microsoft-hosted mailbox, run
mailmd connect outlook --client-id YOUR_CLIENT_ID. This requires your own OAuth app in Microsoft Entra.
An OAuth app lets mailmd request permission to read your account through the provider’s sign-in system. For these API connections, you create the app with the relevant provider. mailmd connects to Gmail through the Gmail API and to Outlook.com and other Microsoft-hosted mailboxes through Microsoft Graph, which matters because many Outlook mailboxes no longer accept IMAP passwords.
After connecting, start the first copy:
mailmd syncmailmd stores the files under ~/mail/<account>. For Gmail over IMAP, it syncs All Mail, which includes archived messages. Spam and trash stay out unless you ask for them. Use --folder, --exclude, or --since to choose what to copy.
Each later mailmd sync adds new messages and updates read and starred marks. If you stop a run with Ctrl-C, mailmd keeps what it already saved and continues on the next run. To keep the archive current, schedule mailmd sync --quiet with cron or systemd. A schedule keeps the local copy updated; it does not create a second backup location.
Protect the archive and check it #
The mail files live on your computer, so a disk failure or theft can affect the archive along with the original machine. Back up the ~/mail folder to a separate drive or an off-site location. Encrypt off-site copies, and keep the encryption key somewhere separate from the backup; a general Linux backup guide recommends both precautions.
mailmd saves login details outside the archive, where they are readable only by your Linux user, but it does not encrypt them. Protect the account that owns those files. mailmd has no built-in search engine. Use an editor or note app to read messages. Search the text with rg or grep; use jq to filter index.jsonl, which lists the archived messages. You can also keep the archive folder in Git or include it in a normal backup.
Check that the backup contains the folders and messages you expect, then test that you can restore the backup copy of the archive. mailmd does not import messages back into a mailbox, convert them to PST or PDF, or act as a mail client. If you need mailbox restoration or local-only mail from a mail client, account for those separately.
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.