单位的email服务器老的不行了,头头拿了一台稍好一点的机器来代替。 都是sun sparc.
原来sendmail是系统用户,大概在2000左右,现在要转到mysql里面,有关qmail + mysql +vpopmail安装可以去参照本版 peijun.jiang 和 gadfly 的相关文章。
转换工作分成两步:用户迁移 和 mailbox转换
1.用户迁移
我没有在网上找到现成的脚本,就自己写了一个,用perl
#passwd2mysql
#begin
#!/usr/bin/perl
# author: xmubeta <www.chinaunix.net>
# This program convert passwd to mysql vpopmail table. Because i am updating
# mailserver from sendmail to qmail with mysql, i need to retain old user
# information include password and username.
#
#
# it requires DBI,DBD for mysql.
use DBI;
# setup for mysql username and password
my $MYSQL_VPOPMAIL_SERVER = "localhost";
my $MYSQL_VPOPMAIL_USER = "vpopmail";
my $MYSQL_VPOPMAIL_PASSWD = "passwd";
# end setup for mysql
# setup for vpopmail
my $PW_DOMAIN = "mail.test.com"; # default domain
my $PW_UID = 0;
my $PW_GID = 0;
my $PW_DIR = "/var/vpopmail/domains/${PW_DOMAIN}/"; # MailDir
my $PW_SHELL = "10485760s"; #mail quota
# end setup for vpopmail
#--connect to mysql server
$datasrc = 'DBI:mysql:database=vpopmail;host= ${MYSQL_VPOPMAIL_SERVER}';
$dbh = DBI->connect($datasrc,$MYSQL_VPOPMAIL_USER,$MYSQL_VPOPMAIL_PASSWD,{'Raise
Error' => 1});
$sth = $dbh->prepare("insert into vpopmail (pw_name,pw_domain,pw_passwd,pw_uid,p
w_gid,pw_gecos,pw_dir,pw_shell) values (?,'$PW_DOMAIN',?,$PW_UID,$PW_GID,?,?,'$P
W_SHELL')");
#query username,password from /etc/passwd and insert into mysql
while(($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getpwent()){
if ($uid < 100) {
print "${name} is system retain user, it will be not converted!\n";
next;
}
if (! $shell) {
print "${name} 's shell is not valid, it will be not converted!\n";
next;
}
if ($passwd eq "*LK*") {
print "${name} 's password has not been set,i will be not converted!\n";
next;
}
#--execute SQL
$sth->execute($name,$passwd,$comment,$PW_DIR.$name);
print "${name} : ${passwd} : ${comment}\n";
}
#--disconnect
$dbh->disconnect;
#end
说明: 一些定制的变量我都放在脚本前面, vpopmail目录我自己是在/var/vpopmail目录下,很多人可能是在/home/vpopmail下,按照需要改一下就好了。 有些系统用户不需要要转过去的,我设置了判断语句,如果你也需要转可以修改相应的语句。
2.转换mbox to maildir
这里我去下载了Russell Nelson 的http://www.qmail.org/convert-and-create 脚本,不过他的脚本不是转到vpopmail去的,而是用户的home目录,而且权限设置也按照用户来设置,所以我修改了一下。修改我就直接在下面代码注明了
#! /usr/bin/perl
# put into the public domain by Russell Nelson <nelson@qmail.org>
# NO GUARANTEE AT ALL; support is available for a fee from the author.
#
# Creates maildirs for everyone in /etc/passwd who receives mail.
# Copies all their mail in /var/spool/mail into their maildir.
# Assumes that nothing is trying to modify the mailboxes in /var/spool/mail
# This assumption could be removed by locking the mailboxes and deleting
# the mail after moving it.
# version 0.00 - first release to the public.
while(($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =
getpwent()) {
# if (!-e $dir) {
# print "warning: ${name}'s home dir, $dir, doesn't exist (passwd: $passwd
), skipping.\n";
# next;
# }
# $st_uid = (stat($dir))[4];;
# if ($uid != $st_uid) {
# print "warning: $name is $uid, but $dir is owned by $st_uid, skipping.\n
";
# next;
# }
#以上是判断用户目录权限的,此处不需要
$uid = 113; # 此处设置为 vpopmail的 uid
$gid = 100; # 此处设置为 vpopmail的 gid
print "$name\n";
$userhomedir = "/var/vpopmail/domains/mail.test.com/$name"; #存放虚拟域的路径, 分成两步创建目录
-d $userhomedir || mkdir $userhomedir,0700 || die "fatal: user home dir can'
t be created.\n";
chown ($uid,$gid,$userhomedir);
$spoolname = "/var/vpopmail/domains/mail.test.com/$name/Maildir";
-d $userhomedir || mkdir $userhomedir,0700 || die "fatal: user home dir can'
t be created.\n";
chown ($uid,$gid,$userhomedir);
-d $spoolname || mkdir $spoolname,0700 || die "fatal: mailbox doesn't exist
and can't be created.\n";
chown ($uid,$gid,$spoolname);
chdir($spoolname) || die("fatal: unable to chdir to $spoolname.\n");
-d "tmp" || mkdir("tmp",0700) || die("fatal: unable to make tmp/ subdir\n");
-d "new" || mkdir("new",0700) || die("fatal: u