Email backup

How to Back Up Your Email Automatically on a Schedule

Updated 4 min read

On this page
  1. Connect your mailbox with mailmd
  2. Schedule mailmd to run
  3. Keep the archive recoverable

If your email lives in Gmail, Outlook, or another mail service, a scheduled backup keeps a separate copy on your computer in case messages disappear from the mailbox. A backup is a copy you can keep independently; it does not change what remains in your online account.

For a local, readable copy that updates on a schedule, mailmd is the recommended fit. It saves each message as a Markdown file, a text file with simple formatting, and can run through your computer’s scheduler. You’ll connect your mailbox, run an initial copy, then schedule the same command to run regularly.

Connect your mailbox with mailmd #

mailmd is a free, open-source command-line tool. It reads email and saves files on your computer; it does not move, delete, or send messages. The mailmd website and project repository provide the tool and setup details.

On Linux or macOS, or Windows using Git Bash, install mailmd and connect the account you want to back up:

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

Choose the connection command that matches your mailbox:

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

Use the IMAP command for IMAP mailboxes, including Gmail with an app password. Gmail’s API connection needs your own OAuth app in Google Cloud. Outlook.com and Microsoft 365 use Microsoft Graph and need your own app in Microsoft Entra. Many IMAP providers require an app password; mailmd indicates when one is needed.

Then copy the mailbox:

mailmd sync

mailmd saves the files under ~/mail/<account>. Each message gets its own Markdown file with headers at the top, and attachments sit next to it; folders and years become directories. The tool leaves spam and trash out unless you request them. For Gmail over IMAP, it syncs All Mail, which includes archived messages.

The first run creates the local copy. Later runs add new messages and update read and starred marks. If you stop a run with Ctrl-C, mailmd keeps the files already saved; the next run continues. It never removes archive files, so a message later deleted from the server remains in the local folder marked as deleted on the server.

Schedule mailmd to run #

Schedule mailmd sync --quiet with your computer’s built-in scheduler. Start with a daily run if you want a copy that stays reasonably current, then choose a different interval if your needs call for it. The scheduler runs the command; mailmd does not manage its own schedule.

On Linux or macOS, you can use cron, one option for running commands at specified times. First find mailmd’s executable path:

command -v mailmd

Use that full path in your cron entry. For example, this entry runs the sync daily at 2 a.m.:

0 2 * * * /absolute/path/to/mailmd sync --quiet

Replace /absolute/path/to/mailmd with the path returned by command -v. Cron may start with a smaller set of environment settings than your interactive shell, so using the full path avoids relying on your shell’s PATH. Test the command yourself before scheduling it; the cron backup example also advises testing a backup command before automating it. You can use systemd or launchd instead of cron if those fit your computer’s setup.

On Windows, use Task Scheduler to create a task with a daily trigger and an action that starts the mailmd executable with sync --quiet as its arguments. Task Scheduler runs programs at set times or when selected events occur. Run the task under the same Windows user account that connected the mailbox. The Task Scheduler overview describes setting a trigger and an action for a scheduled task.

After creating the schedule, run the task once and confirm that the archive contains the expected messages and attachments. Then check it again after a new message arrives. A schedule that never runs, or runs under a different user account, will not keep the intended archive up to date.

Keep the archive recoverable #

mailmd keeps a local copy, but a copy on one computer is still vulnerable to disk failure, loss, or damage. Back up the mail folder to a separate drive or another storage location. The 3-2-1 approach recommends keeping three copies on two types of storage, with one copy offsite; backup strategy guidance explains that separate copies reduce the chance that one incident affects them all.

mailmd never deletes files from its archive, but a separate backup method could mirror unwanted changes or share the same account access. Choose a destination that gives you an independent copy, and periodically check that you can open messages and attachments from it.

The mailmd login details are stored outside the archive, readable only by your user, and are not encrypted. Keep that in mind when other people can access the computer. The Markdown files also contain email content, so protect the archive and any extra copy accordingly.

The archive lets you read and search messages on your computer, but mailmd is not a mail client and does not import messages back into a mailbox. It also does not convert email to PST or PDF. Treat it as a readable local record, and keep your mailbox or another recovery method for restoring messages to an email service.

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