Back up Email with a Single Command
Productivity Sauce
The Web is awash with tutorials on how to back up your email. Strangely, none of them covers probably the easiest and most efficient email backup system based on the excellent IMAP Grab utility. This tiny Python script acts as a user-friendly wrapper for the getmail tool. Using the script, you can set up a fool-proof email backup solution using just a single command.
Deploying the IMAP Grab backup solution is a cinch. First off, install the getmail package using your distro's package manager. Download then the latest IMAP Grab package, unzip it, and move the imapgrab.py file to the directory of your choosing. Make the script executable using the chmod +x imapgrab.py command.
To make sure that the script works properly and to obtain a list of the available mailboxes, run the following command in the terminal:
./imapgrab.py -l -s imap.mail.server -u username -p password
Replace imap.mail.server, username, and password with the actual mail server address and your login credentials. The -l (or --list) argument instructs the script to fetch a list of IMAP folders on the specified email account. If everything works as it should, the command should return a list of the available mailboxes.
Now you can construct a command to back up emails. It may look something like tihs:
./imapgrab.py -d -v -M -f /home/user/emailbackup -s imap.mail.server -u username -p password -m "_ALL_,-INBOX.Trash,-INBOX.Junk Mail"
The -d (--download) argument instructs the script to download messages, while the -f (--folder) argument specifies the destination directory. By default, the script downloads emails in the Mbox format which stores all messages in a single file. However, IMAP Grab supports the Maildir format, too, and you can force the script to use this format by adding the -M (--maildir) argument.
IMAP can also handle SSL connections, and you can use the -S (--ssl) and -p (--port) arguments to enable this feature:
./imapgrab.py -l -s imap.mail.server -S -p 993 -u username -p password
The -m (--mailboxes) argument lets you specify which IMAP folders you want the script to back up. The _ALL_ switch instructs the script to download all the available folders. To exclude certain folders from the backup, use the minus (-) character followed by the name of the mailbox you want the script to omit. So the -m "_ALL_,-INBOX.Trash,-INBOX.Junk Mail" argument instructs the script to download all folders except INBOX.Trash and INBOX.Junk Mail.
Finally, when you run the script for the first time, you might want to add the -a (-all) argument which forces IMAP Grab to download all messages.
That's all there is to it. To perform backup on a regular basis, set up a cron job, and you are done.
Comments
comments powered by DisqusSubscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
This is the main site
Download
Restore
small mistake
IMAP Grab GUI