I back up my remote IMAP mails in a local Maildir directory which is version-controlled by git. It's a bit geeky and hackish, but I think it's a good way if you don't want to lose any emails and want to be safe even if you accidentally delete a bunch of mails on your remote IMAP. The script which does the job works the following way:
- All remote mails on the IMAP server are synchronized with a local Maildir directory. I use
imapsync
for this. - The mail files in this local Maildir are then copied to a version controlled git-Maildir directory. The original directory structure is not retained, as this is not important to me. Instead, the git-Maildir contains a folder for every year and the mail files are copied to the year folder based on the Date-header of the mail file. I use
mu
(mailbox utils) to do the sorting work. - In the next step, the script automatically adds and commits the new files to the git repository.
With this, I have a local backup of all my emails. Even if I delete mails on remote side, I still have local copies. The advantage of the local git repository is data safety. Even I mess with my files in my local backup, I have a full history in the commit history and I can recover deleted mails.
The advantage of maildir over mbox is, that with maildir you have one file for each mail instead of one huge mbox file which changes every time you add new mails. One file per mail makes a nice git history.