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
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
This is the main site
Download
Restore
small mistake
IMAP Grab GUI