Difference between revisions of "Migrating from Postfix and MySQL with bash"

Line 40: Line 40:
 
echo "Writing to $file ...\n";
 
echo "Writing to $file ...\n";
 
$fh = fopen($file, "w");
 
$fh = fopen($file, "w");
 +
 +
fwrite($fh, "#!/bin/sh -x\n\n");
  
 
while ($row = mysql_fetch_array($dane, MYSQL_NUM))
 
while ($row = mysql_fetch_array($dane, MYSQL_NUM))
 
{
 
{
     $StringData = "zmprov ca ".$row[0]." dsfs123hsdyfgbsdgfbsd displayName '".$row[2]."'\n";
+
     $data_mbox = "zmprov ca ".$row[0]." dsfs123hsdyfgbsdgfbsd displayName '".$row[2]."'\n";
     fwrite($fh, $StringData);
+
     $data_mbox .= "zmprov ma ".$row[0]." userPassword '{crypt}".$row[1]."'"."\n";
    $StringData = "zmprov ma ".$row[0]." userPassword '{crypt}".$row[1]."'"."\n";
+
     fwrite($fh, $data_mbox);
     fwrite($fh, $StringData);
 
 
}
 
}
  
Line 68: Line 69:
 
         if (count($dest_r) > 1) {
 
         if (count($dest_r) > 1) {
 
                 // distribution list
 
                 // distribution list
                 fwrite($fh, "zmprov cdl $row[0]\n");
+
                 $data_list .= "zmprov cdl $row[0]\n";
 
                 foreach ($dest_r as $dest) {
 
                 foreach ($dest_r as $dest) {
                         fwrite($fh, "zmprov adlm $row[0] $dest\n");
+
                         $data_list .= "zmprov adlm $row[0] $dest\n";
 
                 }
 
                 }
 
         }
 
         }
 
         if (count($dest_r) == 1) {
 
         if (count($dest_r) == 1) {
 
                 // simple alias
 
                 // simple alias
                 $StringData = "zmprov aaa $row[0] $dest_r[0]\n";
+
                 $data_alias .= "zmprov aaa $row[0] $dest_r[0]\n";
                fwrite($fh, $StringData);
 
 
         }
 
         }
 
}
 
}
  
 +
fwrite($fh, $data_list . $data_alias);
 
fclose($fh);
 
fclose($fh);
  

Revision as of 18:47, 1 December 2009

This script will help you to migrate your Postfix+MySQL (and optionally Postfixadmin) installation to Zimbra.

It reads the "mailbox" and "aliases" table used by Postfix with MySQL backend and generates a bash script that will re-create the same email accounts and aliases/forwards on your zimbra server.

#!/usr/bin/php

// Postfixadmin (http://postfixadmin.sourceforge.net/) to Zimbra
// (www.zimbra.com) migration script
//
// History:
// Based on work from: Jaros�[34m~Baw Czarniak
// Enhanced by NERvOus (www.nervous.it) on 1-12-2009

<?php
/////////////////////////////////////////////////////////

$user="changeme";
$pass="changeme";
$db="postfix";
$table_mbox="mailbox";
$table_alias="alias";
$file="exported.sh";

/////////////////////////////////////////////////////////
echo "This script generates a bash script called: $file
The script contains the commands to re-create the mboxes and
aliases on zimbra server.\n\n
";

$mydb = mysql_connect('localhost',$user, $pass) or die ('Error of connection with server');
mysql_select_db($db);
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");

$query = "SELECT username,password,name,maildir,quota,domain 
                FROM $table_mbox";
$dane = mysql_query($query) or die ('Error during query for '.mysql_error());

echo "Writing to $file ...\n";
$fh = fopen($file, "w");

fwrite($fh, "#!/bin/sh -x\n\n");

while ($row = mysql_fetch_array($dane, MYSQL_NUM))
{
    $data_mbox = "zmprov ca ".$row[0]." dsfs123hsdyfgbsdgfbsd displayName '".$row[2]."'\n";
    $data_mbox .= "zmprov ma ".$row[0]." userPassword '{crypt}".$row[1]."'"."\n";
    fwrite($fh, $data_mbox);
}

$query = "SELECT address, trim(trailing ',' from goto) AS dest 
                FROM ".$table_alias."
                WHERE address NOT LIKE '@%'  
                        AND address NOT IN (SELECT username FROM $table_mbox) 
                HAVING address != dest";
$dane = mysql_query($query) or die ('Error during query for '.mysql_error());
while ($row = mysql_fetch_array($dane, MYSQL_NUM)) {
        // multiple dests
        unset($rawdest_r);
        unset($dest_r);
        $rawdest_r = preg_split('/,/', $row[1]);
        foreach ($rawdest_r as $dest) {
                if ($dest != $row[0]) {
                // don't forward to itself
                        $dest_r[] = $dest;
                }
        }
        if (count($dest_r) > 1) {
                // distribution list
                $data_list .= "zmprov cdl $row[0]\n";
                foreach ($dest_r as $dest) {
                        $data_list .= "zmprov adlm $row[0] $dest\n";
                }
        }
        if (count($dest_r) == 1) {
                // simple alias
                $data_alias .= "zmprov aaa $row[0] $dest_r[0]\n";
        }
}

fwrite($fh, $data_list . $data_alias);
fclose($fh);


echo "Done.

Now copy exported.sh to zimbra server and run:
# su - zimbra
$ sh ./$file
";

?>

Jump to: navigation, search