Commits

Zhang Huangbin committed f8921a4

Sync dump_openldap_to_xml.py.

Comments (0)

Files changed (3)

extra/migration/convert_winmail_mailbox.sh

-#!/usr/bin/env bash
-
-# Author:   Zhang Huangbin (michaelbibby <at> gmail.com )
-# Purpose:  Convert WinMail user mailboxs to standard IMAP directory
-#           structure (mailbox format).
-
-# Migration guide wrote in Chinese:
-#   http://code.google.com/p/iRedMail/wiki/iRedMail_tut_Migration
-
-# Description:
-#   WinMail stores all mails in user mailbox with email address as mailbox
-#   name, such as:
-#
-#       /path/to/mail/store/user@example.com/
-#                           |- xxxxxx.in    # Received (Inbound)
-#                           |- xxxxxx.out   # Sent (Outbound)
-#                           |- xxxxxx.del   # Deleted
-#
-#   What this script do is:
-#
-#       * Creates standard IMAP directory structure (mailbox format).
-#       * Copies mails to correct directories.
-#
-
-# Usage:
-#   1) Specify these variables below:
-#
-#       * source_dir    -> Where you store WinMail user mailboxes.
-#       * target_dir    -> Where you want to store converted mailboxes.
-#       * vmail_user    -> Owner (user) of your user mailboxes.
-#       * vmail_group   -> Owner (group) of your user mailboxes.
-#
-#       Default file localtion looks like below:
-#
-#           /path/to/mail/store/
-#                           |- user1@example.com/
-#                           |- user2@example.com/
-#                           |- user3@example.com/
-#                           |- convert_winmail_mailbox.sh   # <- we are here.
-#
-#   2) Execute command:
-#
-#       # sh convert_winmail_mailbox.sh
-#
-#      It will create new directory named as domain name, and user
-#      mailbox is named as username (without domain part), such as:
-#
-#           example.com/
-#               |- user1/
-#                   |- cur/     # Received, read.
-#                   |- new/     # Received, unread.
-#                   |- tmp/
-#                   |- .Drafts/ # Drafts.
-#                   |- .Sent/   # Sent.
-#                   |- .Trash/  # Trash, deleted.
-#               |- user2/
-#
-#   3) Copy the 'example.com/' to the location where you store user
-#      mailboxes.
-
-# Your original WinMail user mailboxes.
-source_dir='./'
-# Copy emails to another directory, it will be standard IMAP directory
-# structure (mailbox format).
-target_dir='./'
-
-# vmail user name/uid.
-vmail_user='vmail'
-# vmail group name/uid.
-vmail_group='vmail'
-
-for i in $(ls -d *@w-ibeda.com)
-do
-    username="$(echo $i | awk -F'@' '{print $1}')"
-    domain="$(echo $i | awk -F'@' '{print $2}')"
-    mailbox="${target_dir}/$domain/$username/"
-    
-    #mailbox="$(echo $i | awk -F'@' '{print $1"/"$2"/"}')"
-
-    # Create necessary directories as mailbox format.
-    # Inbox.
-    mkdir -p ${mailbox}/{cur,new,tmp}
-    # Sent,Junk,Drafts,Trash
-    mkdir -p ${mailbox}/.{Sent,Junk,Drafts,Trash}/{cur,new,tmp}
-
-    find_dir="${source_dir}/$i/"
-
-    # Copy inbox.
-    for email in $(find ${find_dir} -iname '*.in')
-    do
-        cp -f $email ${mailbox}/cur/
-    done
-
-    # Copy deleted mails.
-    for email in $(find ${find_dir} -iname '*.del')
-    do
-        cp -f $email ${mailbox}/.Trash/
-    done
-
-    # Copy sent mails.
-    for email in $(find ${find_dir} -iname '*.out')
-    do
-        cp -f $email ${mailbox}/.Sent/
-    done
-
-    chown -R ${vmail_user}:${vmail_group} ${target_dir}/${domain}
-done

extra/migration/dump_openldap_to_xml.py

 xml_file = 'accounts.xml'
 
 # ==== Do NOT modify below settings. ====
-filter = '(|(objectClass=mailDomain)(objectClass=mailAdmin)(objectClass=mailUser)(objectClass=mailAlias)(objectClass=mailList)(objectClass=mailExternalUser))'
-page_size = 100
+FILTER_IREDMAIL_ACCOUNTS = '(|(objectClass=mailDomain)(objectClass=mailAdmin)(objectClass=mailUser)(objectClass=mailAlias)(objectClass=mailList)(objectClass=mailExternalUser))'
+PAGE_SIZE = 100
+
+ATTRS_WITH_SINGLE_VALUE = ['accountStatus', 'cn', 'mtaTransport', \
+                           'street', 'expiredDate', 'disclaimer', 'description', \
+                           'domainName', \
+                           'domainBackupMX', \
+                           'domainMaxQuotaSize', 'domainMaxUserNumber', \
+                           'domainMaxAliasNumber', 'domainMaxListNumber', \
+                           'domainDefaultUserQuota',
+
+                           # User object.
+                           'mail', 'uid', \
+                           'storageBaseDirectory', 'mailMessageStore', 'homeDirectory', \
+                           'mailQuota', 'mailQuotaMessageLimit', 'userPassword', \
+                           'preferredLanguage', \
+
+                          ]
+
+ATTRS_WITH_MULTI_VALUES = ['enabledService', 'accountSetting', \
+                           'telephoneNumber', 'facsimileTelephoneNumber', \
+
+                           # Domain object.
+                           'domainAliasName',
+                           'domainSenderBccAddress', 'domainRecipientBccAddress', \
+                           'domainWhitelistIP', 'domainWhitelistSender', \
+                           'domainBlacklistIP', 'domainBlacklistSender', \
+
+                           # User object.
+                           'mailForwardingAddress', 'shadowAddress', 'memberOfGroup', \
+                           'title', \
+                           'userRecipientBccAddress', 'userSenderBccAddress', \
+                           'mailWhitelistRecipient', 'mailBlacklistRecipient', \
+                          ]
 
 # Dump object of mail domain in XML format.
 def dump_ldap_entry_in_xml(entry):
         f.write('\t\t<domain>\n')
 
         # Attributes with single value.
-        for attr in ['domainName', 'accountStatus', 'cn', 'mtaTransport', \
-                  'domainBackupMX', \
-                  'domainMaxQuotaSize', 'domainMaxUserNumber', \
-                  'domainMaxAliasNumber', 'domainMaxListNumber', \
-                  'domainDefaultUserQuota', 'disclaimer', 'description', \
-                  'street', 'expiredDate', \
-                 ]:
+        for attr in ATTRS_WITH_SINGLE_VALUE:
             if attr in entry:
                 f.write('\t\t\t<%s>%s</%s>\n' % (attr, entry.get(attr)[0], attr))
 
         # Attributes with multi value.
-        for attr in ['domainAliasName', 'enabledService', 'accountSetting', \
-                     'domainSenderBccAddress', 'domainRecipientBccAddress', \
-                     'telephoneNumber', 'facsimileTelephoneNumber', \
-                     'domainWhitelistIP', 'domainWhitelistSender', \
-                     'domainBlacklistIP', 'domainBlacklistSender', \
-                    ]:
+        for attr in ATTRS_WITH_MULTI_VALUES:
             if attr in entry:
                 f.write('\t\t\t<%s>\n' % attr)
                 for i in entry.get(attr):
         f.write('\t\t</domain>\n')
         f.close()
 
-        '''
-        f.write('\t<name>%s</name>\n' % entry.get('domainName')[0])
-
-        if 'accountStatus' in entry:
-            f.write('\t<status>%s</status>\n' % entry.get('accountStatus')[0])
-
-        if 'domainAliasName' in entry:
-            f.write('\t<aliases>\n')
-            for a in entry.get('domainAliasName'):
-                f.write('\t\t<name>%s</name>\n' % a)
-            f.write('\t</aliases>\n')
-
-        if 'domainAdmin' in entry:
-            f.write('\t<admins>\n')
-            for a in entry.get('domainAdmin'):
-                f.write('\t\t<mail>%s</mail>\n' % a)
-            f.write('\t</admins>\n')
-
-        if 'cn' in entry:
-            f.write('\t<company>%s</company>\n' % entry.get('cn')[0])
-
-        if 'mtaTransport' in entry:
-            f.write('\t<transport>%s</transport>\n' % entry.get('mtaTransport')[0])
-
-        '''
-
     elif 'mailAdmin' in objectClasses:
         pass
     elif 'mailUser' in objectClasses:
         f.write('\t\t<user>\n')
 
         # Attributes with single value.
-        for attr in ['mail', 'uid', 'accountStatus', 'cn', 'mtaTransport', \
-                     'storageBaseDirectory', 'mailMessageStore', 'homeDirectory', \
-                     'mailQuota', 'mailQuotaMessageLimit', 'userPassword', \
-                     'expiredDate', 'preferredLanguage', 'disclaimer', \
-                    ]:
+        for attr in ATTRS_WITH_SINGLE_VALUE:
             if attr in entry:
                 f.write('\t\t\t<%s>%s</%s>\n' % (attr, entry.get(attr)[0], attr))
 
         # Attributes with multi value.
-        for attr in ['enabledService', 'accountSetting', 'telephoneNumber', \
-                     'mailForwardingAddress', 'shadowAddress', 'memberOfGroup', \
-                     'telephoneNumber', 'title', \
-                     'userRecipientBccAddress', 'userSenderBccAddress', \
-                     'mailWhitelistRecipient', 'mailBlacklistRecipient', \
-                    ]:
+        for attr in ATTRS_WITH_MULTI_VALUES:
             if attr in entry:
                 f.write('\t\t\t<%s>\n' % attr)
                 for i in entry.get(attr):
 conn.bind_s(bind_dn, bind_pw)
 
 # Start paged control, 100 objects per page.
-paged_controller = SimplePagedResultsControl(ldap.LDAP_CONTROL_PAGE_OID, True, (page_size, ''))
+paged_controller = SimplePagedResultsControl(ldap.LDAP_CONTROL_PAGE_OID, True, (PAGE_SIZE, ''))
 
 # Send search request
-msgid = conn.search_ext(basedn, ldap.SCOPE_SUBTREE, filter, serverctrls=[paged_controller],)
+msgid = conn.search_ext(basedn, ldap.SCOPE_SUBTREE, FILTER_IREDMAIL_ACCOUNTS, serverctrls=[paged_controller],)
 
 pages = 0
 
     pages += 1
     print "Getting page %d" % (pages,)
     rtype, rdata, rmsgid, serverctrls = conn.result3(msgid)
-    print '%d results' % len(rdata)
+    print '%d accounts.' % len(rdata)
     for dn, entry in rdata:
         dump_ldap_entry_in_xml(entry)
 
     if pctrls:
         est, cookie = pctrls[0].controlValue
         if cookie:
-            paged_controller.controlValue = (page_size, cookie)
-            msgid = conn.search_ext(basedn, ldap.SCOPE_SUBTREE, filter, serverctrls=[paged_controller],)
+            paged_controller.controlValue = (PAGE_SIZE, cookie)
+            msgid = conn.search_ext(basedn, ldap.SCOPE_SUBTREE, FILTER_IREDMAIL_ACCOUNTS, serverctrls=[paged_controller],)
         else:
             break
     else:

extra/migration/migrate_extmail_mailbox.sh

-#!/usr/bin/env bash
-
-# Author:   Zhang Huangbin (michaelbibby <at> gmail.com )
-# Purpose:  Migrate ExtMail MySQL 'mailbox' table to iRedMail format.
-# Project:  iRedMail (http://www.iredmail.org/)
-#
-# Migration guide wrote in Chinese:
-#   http://www.iredmail.org/wiki/index.php?title=Migrate_from_ExtMail_Solution
-
-# Usage:
-#   English:
-#   * Run into MySQL command line with privilege user, e.g. root.
-#       # mysql -uroot -p extmail
-#   * Select some column from mailbox table:
-#       mysql> SELECT username,password,name,maildir,quota,netdiskquota,domain,createdate,active
-#            > INTO OUTFILE '/tmp/mailbox.sql'
-#            > FROM mailbox;
-#   * Run this script:
-#       # sh migrate_extmail_mailbox.sh /tmp/mailbox.sql
-#     It will create a new file: /tmp/mailbox.sql.new
-#   * Import this new file in MySQL command line:
-#       mysql> USE vmail;
-#       mysql> SOURCE /tmp/mailbox.sql.new;
-
-usage()
-{
-    echo -e "\nUsage: sh $0 script\n"
-}
-
-[ X"$#" != X"1" ] && usage && exit 255 
-
-OUTPUT_SQL="$1.iRedMail"
-echo ''> ${OUTPUT_SQL}
-
-while read line; do
-    username="$(echo $line | awk '{print $1}')"
-    password="$(echo $line | awk '{print $2}')"
-    maildir="$(echo $line | awk '{print $3}' | awk -F'Maildir/' '{print $1}' | tr [A-Z] [a-z] )"
-    quota="$(echo $line | awk '{print $4}')"
-    netdiskquota="$(echo $line | awk '{print $5}')"
-    domain="$(echo $line | awk '{print $6}')"
-    createdate="$(echo $line | awk '{print $7, $8}')"
-    active="$(echo $line | awk '{print $9}')"
-
-    echo $quota | grep -i 'S$' >/dev/null 2>&1
-    if [ X"$?" == X"0" ]; then
-        quota="$(echo $quota | sed 's/S$//')"
-        quota="$(expr \( $quota / 1024 \) )"
-    else
-        :
-    fi
-
-    cat >> ${OUTPUT_SQL} <<EOF
-INSERT INTO mailbox (username, password, maildir, quota, netdiskquota, domain, created, active) values ("$username", "$password", "$maildir", $quota, $quota, "$domain", "$createdate", "$active");
-EOF
-done < $1
-
-cat <<EOF
-Warning: you may want to change 'mail_location' setting in /etc/dovecot.conf like below:
-
-    mail_location = maildir:/%Lh/Maildir/:INDEX=/%Lh/Maildir/
-
-EOF