How to Back Up Gmail to an External Hard Drive
On this page
To back up Gmail, copy your messages out of Google’s mailbox and keep that copy on a drive you control. mailmd is a free, open-source tool that you run by typing commands. It saves each message as a Markdown file on your computer. After mailmd copies your Gmail messages to your computer, copy the resulting folder to your external hard drive.
A Markdown file is a plain-text document with simple formatting. mailmd saves each message’s headers, such as its sender and subject, with the file. It saves attachments beside the messages. The steps below cover connecting Gmail, making the copy, and checking that the files reached the drive.
Connect Gmail to mailmd #
First, connect the external drive and confirm that your computer can save files to it. Install mailmd from its official project instructions:
curl -fsSL https://raw.githubusercontent.com/vincentsch/mailmd/main/install.sh | shFor a Gmail account, connect through IMAP, the standard method email programs use to access messages stored on a mail server. Gmail’s IMAP connection uses an app password, a password for an email program, when one is required:
mailmd connect imap --email you@example.comFollow mailmd’s prompts to finish connecting. You can also connect through the Gmail API, which requires creating your own OAuth app in Google Cloud. An OAuth app lets Google authorize the connection through its sign-in process instead of an IMAP password. Google documents current options for connecting Gmail to another email client.
mailmd can read messages in your mailbox but cannot change them. It asks for permission to read mail, does not mark messages as read, and does not move or delete anything in Gmail. Its login details are stored outside the message archive and are readable by your user, but they are not encrypted.
Sync messages and copy the archive #
Run the first sync:
mailmd syncmailmd saves the messages under ~/mail/<account>. Open that folder in your file manager and copy the account folder to a clearly named folder on the external drive, such as Gmail Backup. Keep the original on your computer until you have checked the drive copy.
Each email becomes a Markdown file with its headers. Messages written in HTML, a format for displaying formatted text, are converted to readable Markdown. Attachments are saved beside their messages, and mail folders and years become folders on your computer. For a copy of the original email files as well, use mailmd’s --keep-raw option.
Gmail messages in All Mail, including archived messages, are included when mailmd connects through IMAP. Spam and Trash are left out unless you request them. The --folder, --exclude, and --since options let you choose which mail to copy.
Check the backup and keep it current #
Open a few files from the external drive, including a recent message, an older message, and one with an attachment. Confirm that the attachment opens and that the message text is readable. Copying the files does not confirm that you can open and read them.
Run mailmd sync again whenever you want to add new mail to the local archive, then copy the updated folder to the external drive. Each sync adds new messages and updates which messages are marked as read or starred. It never deletes files from the archive; a message deleted later from Gmail remains in the local copy and is marked as deleted on the server.
For regular backups, schedule mailmd sync --quiet with your computer’s scheduling tools, then copy the updated archive to the external drive. Keep the drive disconnected when you are not backing up, and consider keeping another copy in a separate location. mailmd does not delete Gmail messages to free mailbox space, restore mail to Gmail, or act as a mail-reading application.
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.