21

IMAP Email Backup

 4 years ago
source link: https://smalldata.tech/blog/2020/04/07/imap-email-backup
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Recently, I had some issues with my email provider and it gave me a bit of a scare as I realized that I had taken email for granted and did not have a backup in case things went south. Since my email address is run off of a domain that I own, I can easily switch email providers. However, I use mutt and it does not download all the email bodies in all the folders so I did not necessarily have a local copy of all my emails.

yMnQzyb.png!web

Take my advice - I don't use it :).

Thus began the hunt for a simple email backup solution and I found it in the form of a really nice python script: imapgrab.py . Note that this script has the following requirements:

sudo apt install getmail

Once the requiremements are met, simply copy the script over to any folder and execute it as follows: ./imapgrab.py -l -s imap.mail.server -u username -p password . This will list all available IMAP folders on the provided server. To download a copy of all mails run the following:

./imapgrab.py -d -v -f ~/[email protected] -s imap.mail.server -S -p 993 -u [email protected] -p password -m "_ALL_,-INBOX.Trash,-INBOX.Spam,-INBOX.Junk,-Trash,-Junk,-Spam"

-d instructs the script to download the IMAP folders. -f provides the location to store the downloaded email. -m provides instructions as to what to download (here we download everything except the trash and spam folders). Check out the script itself for more usage parameters.

With this script running, I was able to regularly backup my email and as a bonus, here are the commands to encrypt and decrypt the backed up emails (linux specific):

# create archive
tar -jcvf ~/[email protected] ~/[email protected] 

# encrypt (could take a while)
gpg -c ~/[email protected]

# delete original
rm ~/[email protected]
rm -rf ~/[email protected]

# decrypt
gpg ~/[email protected]

# extract
tar -xvf ~/[email protected]

Once encrypted you can safely store your email archive anywhere on the cloud.

Finally, in order to actually read the emails, you can import the mailboxes in thunderbird or use mutt as I like to do: mutt -f ~/[email protected]/INBOX.mbox :).

Note that at the time of writing the script does not work with Python 3 and if you do manage to convert it, please do throw me a shout!

HackerNews submission / discussion


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK