NOTE: This project was moved to https://github.com/mleonhard/imapbackup.
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.
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.
imapbackup.py -u user -s mail.com:1234
socket._fileobject.recv
bugfix on
WindowsI'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: 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.
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>_
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:
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-2012 Michael Leonhard