Posted on Thu 14 January 2016 in linux
When you are using external email providers instead of hosting your own email server, you need to cross your fingers that everything goes well with the provider's server. For safety-conscious people it may be worthwhile to think about some mechanism to download your emails on some external server to have a backup. In case of mailboxes providing IMAP, a simple backup system can be easily setup using OfflineIMAP.
Since OfflineIMAP is part of the regular debian respositories it can be easily installed:
apt-get install offlineimap
In the next step, a corresponding configuration file has to be created. For a quickstart, copy the minimal config from the documentation to the home folder:
cp /usr/share/doc/offlineimap/examples/offlineimap.conf.minimal ~/.offlineimaprc
Edit the configuration and adapt it to your need, e.g.:
[general] accounts = Gmx [Account Gmx] localrepository = Local remoterepository = GmxRemote [Repository Local] type = Maildir localfolders = ~/Mail [Repository GmxRemote] type = IMAP ssl = yes remotehost = imap.gmx.net remoteuser = myuser remotepass = mypassword maxconnections = 4
In the general section one or multiple
accounts can be set. Each account requires a separate
Account section to link a local repository to a remote repository. For the local as well as the remote repository, a corresponding
Repository section must be created that specifies further details: the local repository uses the common
Maildir type and stores its files in the
~/Maildir folder, while for the remote repository
IMAP is used and the corresponding credentials for authentication are provided to access the data. For the remote repository additional tweaks are used for using ssl and to use up to 4 connections simultaneuously.
Since sensitive user/password credentials are stored in the configuration, make sure to set the corresponding permissions:
chmod 0600 ~/.offlineimaprc
A first test of the configuration
For a first test, run:
offlineimap -c ~/.offlineimaprc
All emails from the remote server will be copied to the local repository folder. Depending on the number of emails, this first synchronization of mails can take some time.
To view the downloaded mails
mutt can be used, e.g.:
mutt -f ~/Mail/INBOX
note: if mutt complaints about the missing the user's mailbox, you can create it by sending a test mail to the user:
echo "this is a test mail" | mail $USER
If you require a more sophisticated access to your emails think about installing a real IMAP server such as dovecot.
If everything is working fine, an automatic email backup can be configured by using a cron job. For example, to backup the emails once an hour, create the script
/etc/cron.hourly/offlineimap with the following content:
#!/bin/sh USER=user /bin/su - $USER -c "/usr/bin/offlineimap -c /home/$USER/.offlineimaprc \ -u Quiet -l /var/log/offlineimap/offlineimap.log"
Create the corresponding log directory and set access rights for the user:
mkdir /var/log/offlineimap chown user.user /var/log/offlineimap chmod 0700 /var/log/offlineimap
Make the file executable:
chmod +x /etc/cron.hourly/offlineimap
Run the script, if everything is working fine. If yes, check after an hour if the backup is working correctly.