:::: MENU ::::

Backup multiple email accounts automatically on Linux

ball of emails

I launched a new personal website last week, and changed my domain registrar and hosting company in the process. Being concerned how this would affect my primary email account (mail at samtuke dot com), and keen not to lose any of the 10.000+ messages that are stored there, I set about backing them up to a local folder. I also have an ancient Hotmail account, dating from 1997, containing all sorts of childhood contacts and life affecting discussions, and since Microsoft finally decided to allow users gratis use of the IMAP protocl, I decided to back up both email accounts to save time.

Back in the day I used my freedombox to download, sort, and upload to folders all my work emails, in order to avoid races between different mail clients on different platforms in their application of competing filtering rules. Offlineimap, a command line IMAP download utility, served me very well back then, and I’d also used it successfully with Mutt. So again I turned to this tool, this time for the mail backup-backend.

In short, offlineimap downloads messages over the IMAP protocol, and lets you permanently configure a smorgsboard of options such that emails can be syncronised repeatedly, automatically, and without human intervention. It’s ideal for use as a cron job, for example. Installation on Linux distributions is easy:

sudo yum install offlineimap

or

sudo apt-get install offlineimap

After installation, a one-time configuration is necessary. Several criteria must be defined, including:

  • Type of syncronisation to use (one way or symmetrical)
  • Number of concurrent connections to use
  • Location and format for messages saved locally
  • Connection security
  • Mail server username and password (obviously)

These options and other magic are stored in the offlineimap configuration file. Create it:

nano ~/.offlineimaprc

Then copy paste the example below. Settings are arranged in distinct, reusable “local” and “remote” accounts. There’s some duplication here, but the labels are mercifully intuitive:

[general]
accounts = hotmail, mailatsamtukedotcom

[Account hotmail]
localrepository = local-hotmail
remoterepository = remote-hotmail

[Account mailatsamtukedotcom]
localrepository = local-mailatsamtukedotcom
remoterepository = remote-mailatsamtukedotcom

[Repository local-hotmail]
type = Maildir
localfolders = ~/Mail/hotmail

[Repository local-mailatsamtukedotcom]
type = Maildir
localfolders = ~/Mail/mailatsamtukedotcom

[Repository remote-hotmail]
type = IMAP
remotehost = imap.hotmail.com
remoteuser = samtuke
remotepass = dontyoulook
ssl = yes
cert_fingerprint = 12312d3sad231dsadadsa87dwqdsgvfhhrg1w23w
maxconnections = 4

[Repository remote-mailatsamtukedotcom]
type = IMAP
remotehost = mail.samtuke.com
remoteuser = mail
remotepass = secretsauce
ssl = yes
cert_fingerprint = tw456sd4674uy56pu48vc1qw7c3123aqw8e45f6d
maxconnections = 4

You can add as many accounts as you like, just separate their names by comma on the “accounts” line, and make sure that correct local and remote sections are defined for each.

Once you’re happy with your configuration, create any mail local mail directories that you’ve set for storing the backup emails.

Finally, simply run the program by name on the commandline and you should see your mails flying in:

offlineimap

The command can be run as part of a larger backup script (to compress and encrypt messages once received, for example), or by cron for routine backups. Good luck!


7 Comments

So, what do you think ?