Created
January 10, 2010 09:29
-
-
Save pklaus/273410 to your computer and use it in GitHub Desktop.
MboxDump – http://wiki.e-shell.org/IMAPFolderBackupTool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MboxDump v0.1 | |
# | |
# made by Francisco de Borja Lopez Rio (Wu) - <[email protected]> | |
# http://www.e-shell.org | |
# | |
# Wed Nov 22 16:03:06 CET 2006 | |
1.- DESCRIPTION | |
----------------- | |
2.- HOW TO USE | |
---------------- | |
3.- DEPENDENCIES | |
------------------ | |
4.- NOTES / BUGS | |
------------------ | |
1.- DESCRIPTION | |
----------------- | |
This is a little script to create a backup of an IMAP folder into a mbox file, | |
you will be able to restore mails inside such backup using almost every mail | |
user agen (MUA for short) avaliable today (sylpheed, evolution, eudora, | |
thunderbird, etc). | |
2.- HOW TO USE | |
---------------- | |
Simple, you can call it directly from this directory, without any parameters | |
to see the help message: | |
$ ./MboxDump.py | |
MboxDump: Simple Python script to create dumps from cyrus imapd mailboxes | |
Use: | |
$ ./MboxDump.py [-h host] [-P port] ][-u username] [-p passwd] [-m mbox] [-o output file] [-t dump type] | |
-h host | |
Set the name or IP address of the cyrus imapd server we will connect to. | |
If no host is set, localhost is assumed. | |
-P port | |
Set the port to connect, by default 143. | |
-u username | |
Set the username we will use to connect to the cyrus imapd server. | |
If no username is provided, the current username is used (the one of the user | |
that runs ./MboxDump.py). | |
-p passwd | |
This sets the password we will use to connect to the cyrus imapd server. | |
If no password is provided, the script will ask for one. | |
-m mbox | |
With this parameter, we set which mailbox we want to dump. Default is INBOX. | |
-o outputfile | |
Sets the name of the file where the mbox dump will be saved. If no output filename | |
is set, it will defaults to username.mbox (for example frank.INBOX) | |
-t dump type | |
Sets the format in which the dump will be made, by default unix mailbox format. | |
The posibilities are: | |
- mbox: Unix clasic mailbox format | |
- maildir: maildir format. | |
$ | |
The -t switch doesn't work at all right now, see 4.- NOTES / BUGS below. | |
You do not need to have root access in order to use the script, but it could be | |
nice to put it somewhere in your path, so you can call it everywhere and not only | |
from (for example) your home directory. The most common ways to do that are: | |
A) Install it on your personal ~/bin : | |
$ mv MboxDump.py ~/bin/MboxDump | |
$ echo $PATH | |
/home/wu/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/games:. | |
$ pwd | |
/home/wu | |
$ ls | |
bin demo.log rc.pgsql | |
$ MboxDump | |
MboxDump: Simple Python script to create dumps from cyrus imapd mailboxes | |
Use: | |
[ strip ] | |
$ | |
This is an usual manner to install scripts in unix systems. Probably you will found that | |
the bin directory already exists inside your home directory and that it is already | |
in your PATH environment variable. If not, just create it and add it just like this way: | |
$ mkdir ~/bin | |
$ vi ~/.profile | |
(here add $HOME/bin to the PATH= line, something like that:) | |
PATH=$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/games:. | |
B) Install it system-wide. This will let all users on the system uses the script, so it could | |
be a good idea on a multiuser (multiadmin) system. You do require root privileges for that one: | |
# mv MboxDump.py /usr/local/bin/MboxDump | |
# pwd | |
/root | |
# echo $PATH | |
/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin | |
# MboxDump | |
MboxDump: Simple Python script to create dumps from cyrus imapd mailboxes | |
Use: | |
[ strip ] | |
# | |
(You only have to copy MboxDump.py to wherever you want it to be, and check that | |
such directory is in the PATH of every user of the system.) | |
3.- DEPENDENCIES | |
------------------ | |
The only depency of this script is the python language (version >= 2.3), which | |
you can install (if you do not have it installed yet) from the ports tree | |
(in BSD operating systems like FreeBSD or OpenBSD): | |
# cd /usr/ports/lang/python && make install && make clean | |
Using pre-compiled packages (installpkg, apt-get install, yum, or whatever, will | |
be your friend depending on the Linux version). | |
Or installing it from sources, just go to http:://www.python.org and download the | |
latest version.. | |
4.- NOTES / BUGS | |
------------------ | |
You will see some references to the cyrus imapd server, that's because I created the | |
script to work with some cyrus imapd servers I use to manage, but the script is almost | |
generic, and you can use it with almost every imapd server out there. | |
It is the first version, so it lacks of some things, like a the posibility to create | |
a maildir-format backup or the hability to create sub-backups if the IMAP folder has | |
subfolders (currently all emails are dumped into one, big file. I'll try to add those | |
as soon as I can, meanwhile, if you find any bug about this script, please email me | |
at [email protected] and tell me about it. Thnx. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: iso-8859-15 | |
# -*- Python -*- | |
""" | |
Script to create dumps from mailboxes in a given imap server | |
Some parameters must be provided as arguments. | |
$ MboxDump [-h host] [-P port] ][-u username] [-p passwd] [-m mbox] [-o output file] [-t dump type] | |
-h host | |
Set the name or IP address of the imap server we will connect to. | |
If no host is set, localhost is assumed. | |
-P port | |
Set the port to connect, by default 143. | |
-u username | |
Set the username we will use to connect to the imap server. | |
If no username is provided, the current username is used | |
(the one of the user that runs MboxDump). | |
-p passwd | |
This sets the password we will use to connect to the imap server. | |
If no password is provided, the script will ask for one. | |
-m mbox | |
With this parameter, we set which mailbox we want to dump. Default is INBOX. | |
-o outputfile | |
Sets the name of the file where the mbox dump will be saved. If no output filename | |
is set, it will defaults to username.mbox (for example frank.INBOX) | |
-t dump type | |
Sets the format in which the dump will be made, by default unix mailbox format. | |
The posibilities are: | |
- mbox: Unix clasic mailbox format | |
- maildir: maildir format. | |
by Francisco de Borja López Río - [email protected] | |
Soluciones Informáticas Código23 S.L. - http://www.codigo23.net | |
""" | |
import getpass, imaplib, os, sys | |
def ShowHelp(args): | |
""" Just show how to use the script and exit. """ | |
HelpString = """ | |
MboxDump: Simple Python script to create dumps from cyrus imapd mailboxes | |
Use: | |
$ %s [-h host] [-P port] ][-u username] [-p passwd] [-m mbox] [-o output file] [-t dump type] | |
-h host | |
Set the name or IP address of the cyrus imapd server we will connect to. | |
If no host is set, localhost is assumed. | |
-P port | |
Set the port to connect, by default 143. | |
-u username | |
Set the username we will use to connect to the cyrus imapd server. | |
If no username is provided, the current username is used (the one of the user | |
that runs %s). | |
-p passwd | |
This sets the password we will use to connect to the cyrus imapd server. | |
If no password is provided, the script will ask for one. | |
-m mbox | |
With this parameter, we set which mailbox we want to dump. Default is INBOX. | |
-o outputfile | |
Sets the name of the file where the mbox dump will be saved. If no output filename | |
is set, it will defaults to username.mbox (for example frank.INBOX) | |
-t dump type | |
Sets the format in which the dump will be made, by default unix mailbox format. | |
The posibilities are: | |
- mbox: Unix clasic mailbox format | |
- maildir: maildir format. | |
""" % (args[0], args[0]) | |
print HelpString | |
def MboxDump(hostname='localhost', hostport=143, username=getpass.getuser(), passwd='', mbox='INBOX', output_file='', dump_type='mbox'): | |
""" Method to dump a mailbox """ | |
# If no output file is provided, we set the default one: | |
if output_file == '': | |
output_file = username + '.' + mbox | |
try: | |
# connect to the imap server | |
imap_conn = imaplib.IMAP4(hostname, hostport) | |
except: | |
print 'Error: Could not connect to host %s on port %s ' %(hostname, hostport) | |
else: | |
try: | |
# authenticate as username, passwd | |
imap_conn.login(username, passwd) | |
except: | |
print 'Error: Could not login as user %s with provided password' % username | |
else: | |
try: | |
# select the imap folder we will dump | |
imap_conn.select(mbox) | |
except: | |
print 'Error: Could not open mailbox %s ' % mbox | |
else: | |
# get the number of e-mails inside the folder | |
typ, data = imap_conn.search(None, 'ALL') | |
if len(data) <= 0: | |
print 'Warning: mailbox %s is empty, no messages to dump' % mbox | |
else: | |
# go through the list of mails and create the dump in mailbox format. | |
mboxdump = '' | |
for num in data[0].split(): | |
# To build each entry from the imap folder, we need to get | |
# not only the RFC822 message, but some other information before: | |
typ, data = imap_conn.fetch(num, '(BODY[HEADER.FIELDS (FROM)])') | |
# Comment the following line if you are using python < 2.3.5 | |
message = 'From ' + data[0][1].lstrip('From: ').strip() + ' ' | |
# Uncomment the following if you are using python < 2.3.5 | |
#message = 'From ' + data[0][1].split('From: ')[1].strip() + ' ' | |
typ, data = imap_conn.fetch(num, '(INTERNALDATE)') | |
# we have to perform some pre-processing of the date | |
# first check if there is an internal date, if not, use a dummy one | |
if len(data) > 0: | |
procdatetime = data[0].split('"')[1].lstrip().split(' ') | |
procdate = procdatetime[0].split('-') | |
message = message + 'Mon ' + procdate[1] + ' ' + procdate[0] + ' ' + procdatetime[1] + ' ' + procdate[2] + '\n' | |
else: | |
message = message + 'Fri Sep 1 08:23:28 2006 \n' | |
#message = message + data[0].split('"')[1] + '\n' | |
typ, data = imap_conn.fetch(num, '(RFC822)') | |
message = message + data[0][1] | |
# once we get the mail information, we add it to the dump | |
mboxdump = mboxdump + message | |
# Once the dump is complete, we check if the information has to be displayed | |
# on-screen or saved into a file | |
openfile = open(output_file, 'w') | |
openfile.write(mboxdump) | |
openfile.close() | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
# No args, we have to show the use help | |
ShowHelp(sys.argv) | |
else: | |
# Ok, let's dump some mailboxes. | |
# | |
# First we check which parameters the user is providing us, setting | |
# the proper options. | |
# Check the host | |
if '-h' in sys.argv: | |
# host provided by the user | |
hostname = sys.argv[sys.argv.index('-h')+1] | |
else: | |
# no host, default is localhost | |
hostname = 'localhost' | |
# Check the host port | |
if '-P' in sys.argv: | |
# port provided by the user | |
hostport = int(sys.argv[sys.argv.index('-P')+1]) | |
else: | |
# no port, default is 143 | |
hostport = 143 | |
# Check the username | |
if '-u' in sys.argv: | |
# username provided by the user | |
username = sys.argv[sys.argv.index('-u')+1] | |
else: | |
# no username, by default we will use the same username as the user | |
# running the script. | |
username = getpass.getuser() | |
# Check the password | |
if '-p' in sys.argv: | |
# password provided by the user | |
passwd = sys.argv[sys.argv.index('-p')+1] | |
else: | |
# no password, by default we ask for a password | |
passwd = getpass.getpass() | |
# Check the mailbox to dump | |
if '-m' in sys.argv: | |
# mailbox name provided by the user | |
mbox = sys.argv[sys.argv.index('-m')+1] | |
else: | |
# no mailbox specified, default is INBOX | |
mbox = 'INBOX' | |
# Check the output filename | |
if '-o' in sys.argv: | |
# output filename | |
output_file = sys.argv[sys.argv.index('-o')+1] | |
else: | |
# no output filename, default | |
output_file = username + '.' + mbox | |
# Check the type of the dump we will provide | |
if '-t' in sys.argv: | |
# dump type provided by the user | |
dump_type = sys.argv[sys.argv.index('-t')+1] | |
#dump_type must be one of the following: | |
if dump_type not in ['mbox', 'maildir']: | |
#dump type not valid, getting it back to unix standar mailbox | |
#(anyway we tell the user there is a problem) | |
print 'Error: %s is not a valid type for the dump, using default type (mbox) ' %(dump_type) | |
dump_type = 'mbox' | |
else: | |
# no mailbox specified, default is mbox | |
dump_type = 'mbox' | |
# Call MboxDump | |
MboxDump(hostname, hostport, username, passwd, mbox, output_file, dump_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment