IMAP Backup Tool

This program incrementally backs up IMAP folders to local mbox files. New messages are appended to the folder's mbox file. It is a Python program, originally by Rui Carmo. I have updated it extensively.

Features

  • Downloads all IMAP folders
  • Stores messages in mbox, mbox.gz, or mbox.bz2
  • Each folder downloads to its own mbox file, eg. Inbox.Drafts.mbox
  • Downloads only new messages, appends them to the mbox file
  • IMAP4 SSL, supporting client and server certificates
  • Accesses IMAP account in read-only mode. Does not affect message 'seen' status.

Bugs

Imapbackup-1.4b and previous versions use the Python imap module. This module triggers a bug in socket.read() and sslobj.read(). Imapbackup.py works around the bug in socket.read() by replacing the function with a patched version. The patch is very simple. Unfortunately, the Python ssl module is implemented in C and is therefore more difficult to patch at run-time. Here is the error that sometimes occurs:

File INBOX.mbox: 682 messages
Downloading 85 new messages to INBOX.mbox /Traceback (most recent call last):
  File "imapbackup.py", line 650, in <module>
    main()
  File "imapbackup.py", line 566, in main
    download_messages(server, filename, new_messages, config)
  File "imapbackup.py", line 143, in download_messages
    typ, data = server.fetch(messages[msg_id], "RFC822")
  File "c:\Python25\lib\imaplib.py", line 437, in fetch
    typ, dat = self._simple_command(name, message_set, message_parts)
  File "c:\Python25\lib\imaplib.py", line 1055, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "c:\Python25\lib\imaplib.py", line 885, in _command_complete
    typ, data = self._get_tagged_response(tag)
  File "c:\Python25\lib\imaplib.py", line 986, in _get_tagged_response
    self._get_response()
  File "c:\Python25\lib\imaplib.py", line 948, in _get_response
    data = self.read(size)
  File "c:\Python25\lib\imaplib.py", line 1150, in read
    data = self.sslobj.read(size-read)
MemoryError

I will make a patch to the ssl C code and submit it to the Python developers. I will also submit a patch to socket.py. I'll also look into using a different library for SSL. Please email me if you find any other bugs: About Michael.

Download

  • imapbackup.py v1.4a
  • MD5: fea335bb595f0dc9282656c7be415e27
  • SHA-1: c1a00acd04b7e7a118fb33d357b36708ab821848

Change Log

2007-05-28 Version 1.4a:

  • SSL support! Can use a private key file and server certificate chain file. Unfortunately, Python's ssl module doesn't check the certificate chain. This needs to be fixed.
  • You can now specify the port number as part of the server name. Example:
    imapbackup.py -u user -s mail.com:1234
  • Cleaned up code. Used pylint to find code that didn't comply with best practices.
  • Download imapbackup-1.4a.py

2007-05-27 Version 1.3b:

2007-05-26 Version 1.3a:

  • Better support for result of LIST command. Fixes the problem of some folders not getting backed up from Courier IMAPd
  • Improved usage printout, made parameters more consistent.
  • Added support for socket._fileobject.recv bugfix on Windows
  • Download imapbackup-1.3a.py

2007-03-27 Version 1.2e by Rui Carmo

Compatibility

I've tested it on Python 2.4.4 and Python 2.5.1 on Windows, downloading from a Courier IMAP server 4.1.1 on Linux.

Usage

Usage: imapbackup [OPTIONS] -s HOST -u USERNAME [-p PASSWORD]
 -a --append-to-mboxes     Append new messages to mbox files. (default)
 -y --yes-overwrite-mboxes Overwite existing mbox files instead of appending.
 -n --compress=none        Use one plain mbox file for each folder. (default)
 -z --compress=gzip        Use mbox.gz files.  Appending may be very slow.
 -b --compress=bzip2       Use mbox.bz2 files. Appending not supported: use -y.
 -e --ssl                  Use SSL.  Port defaults to 993.
 -k KEY --key=KEY          PEM private key file for SSL.  Specify cert, too.
 -c CERT --cert=CERT       PEM certificate chain for SSL.  Specify key, too.
                           Python's SSL module doesn't check the cert chain.
 -s HOST --server=HOST     Address of server, port optional, eg. mail.com:143
 -u USER --user=USER       Username to log into server
 -p PASS --pass=PASS       Prompts for password if not specified.

NOTE: mbox files are created in the current working directory.

Example

C:\imapbackup>imapbackup.py -u mleonhard -s fyodor.hcoop.net -e
Password:
Connecting to 'fyodor.hcoop.net' TCP port 993, SSL
Logging in as 'mleonhard'
Finding Folders: 60 folders
Folder shared: SELECT failed: ['Mailbox does not exist, or must be subscribed to
.']
Folder shared.SpamAssassin: SELECT failed: ['Mailbox does not exist, or must be
subscribed to.']
Folder INBOX: 449 messages
File INBOX.mbox -
WARNING: Message #138 in INBOX.mbox has a malformed Message-Id header.
File INBOX.mbox |
WARNING: Message #172 in INBOX.mbox has a malformed Message-Id header.
File INBOX.mbox -
WARNING: Message #310 in INBOX.mbox has a malformed Message-Id header.
File INBOX.mbox |
WARNING: Message #456 in INBOX.mbox has a malformed Message-Id header.
File INBOX.mbox: 448 messages
Downloading 9 new messages to INBOX.mbox: 50.38 KB total, 9.77 KB for largest me
ssage
Folder INBOX.Sent: 356 messages
File INBOX.Sent.mbox: 356 messages
New messages: 0
Folder INBOX.Trash: 7 messages
File INBOX.Trash.mbox: 16 messages
New messages: 0
Folder INBOX.Drafts: 11 messages
File INBOX.Drafts.mbox: 12 messages
New messages: 0
Disconnecting
C:\imapbackup>_

License

Rui Carmo released v1.2e under the BSD license. I release this udpated version under the same license.

Copyright © 2007, Rui Carmo, Michael Leonhard

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Copyright © 1999-2007 Michael Leonhard Hosted by HCOOP Valid HTML 4.01!