Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5281766
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: LINUX

2009-08-09 23:05:51

分享小弟針對emos 1.3備份mail perl 程式!

分享小弟針對 emos1.3 系統備份mail perl 程式!
小弟接觸perl 才二星期,寫得很粗糙,可能不適合各位大大,但因為小弟受到各位大大照顧,才能建置emos mail server ,所以想為論壇儘點棉薄之力,謝謝各位大大!!!

#!/usr/bin/perl
use File::Copy;
use File::stat;
use Date::Format;
use Mail::Internet;
use Encode qw/encode decode/;
use Encode;
use Encode::Guess qw/big5-eten utf8/;

#使用 postfix rcpt_bcc_list、send_bcc_list設定,將要備份mail 轉寄至 lin 目錄
sys_copy('/home/domains/mail.xxxx.com.tw/lin/Maildir/cur/','/backup/mail/');
sys_copy('/home/domains/mail.xxxx.com.tw/lin/Maildir/new/','/backup/mail/');
#要篩選使用者
@user=("jliu","young7","elaine","pacila","woodyg","doreen","gisele","jessice","liky","chris","enerce","sunny","jenny");

if (! -e "/mailbackup/anyone/") { mkdir("/mailbackup/anyone/", 0755);}
my $mail_size = sprintf("%.2f",dir_size('/mailbackup/')/1024);

#燒錄容量大小(MB)
my $burn_szie = 4200;
#檢測要燒錄檔案是否超過容量
if ($mail_size >= $burn_szie) {
    $iname = makeISO();
    burniso($iname);
    print "燒錄mail成功!!\n";
    #正式刪除mail檔案,只保留ISO檔
    !system "rm -rf /mailbackup/*" or die "can't delete:$!--/mailbackup/*\n";
    die mail("jacky\@mail.xxxx.com.tw","已燒錄mail備份成功 !!","" );
}

print "mail目錄現有容量共有:$mail_size MB\n";

my $total_size;

#針對 目錄 /backup/mail/ 下所備份mail 作處理
for  $file () {
    $i = 'Y';
    $x = 'N';   # =Y時,此信件有要備份使用者
    $y =  0 ;   
    #獲得檔案建立日期
    my $date_string = time2str("%Y-%m-%d %H-%M",stat($file)->mtime);
    #獲得檔案容量大小,sprintf 四拾五入
    my $filesize = sprintf("%.2f", (-s $file)/(1024*1024));
    print "filesize=$filesize MB\n";
    my $subj;
    open FH, $file;
    @all=;
    close(FH);
    while ($y <= $#user){  #  $#user 帶出 @userb陣列總共有幾個元素
        for $num(@all){
            #針對 @user 使用者作篩選
           if (($num =~ /^To: .*$user[$y]\@mail.xxxx.com.tw/)||
                ($num =~ /^Return-Path: <$user[$y]\@mail.xxxx.com.tw>/)||
                ($num =~ /^Cc:.*$user[$y]\@mail.xxxx.com.tw/)||
                ($num =~ /for.*$user[$y]\@mail.xxxx.com.tw/)){
                print "name=$user[$y]\n";
                #print $_ while ();
                #print "$file\n";
                $x = 'Y';
           } elsif (($num =~ /^Subject:/i) && ($i eq 'Y')){
                chomp($num); #去除變數值的最後換行符號(\n)
            
                #將主旨(subject)含有中文字轉換為可視中文
                if (($num =~ /\?utf-8\?/i)||($num =~ /\?big5\?/i)){
                    if ($num=~/\?B\?/){
                        use MIME::Base64;
                        $num =~/\?B\?.*\?/;
                        $num1 = substr($&,3,length($&)-4);
                        #print "num=$num\n";
                        $subj = decode_base64($num1);
                    } elsif ($num=~/\?Q\?/) {
                        use MIME::QuotedPrint;
                        $num =~/\?Q\?.*\?/;
                        $num1 = substr($&,3,length($&)-4);
                        $subj = decode_qp($num1);
                   }
                   #針對utf8 -> big5
                   if ($num =~ /\?utf-8\?/i){
                        $subj= encode("big5",decode("utf8",$subj));
                        #print "num=$num\n";
                        #print "長度=length($num)\n";
                   }
                } else {
                   #print "原文英文信:$num\n";
                   $num1 = substr($num,9);
                   my $enc = guess_encoding($num1) ;
                   if ((length($num1) > 0) && (($enc -> name) eq "big5-eten")) {
                        #print "中文信:$num1\t",$enc->name,"\n";
                        $subj=$num1;
                   } else {
                        $subj = substr($num1,0,42);#針對超過64字元長字串進行裁減
                        #printf "裁減英文信:$subj ,長度:%s\n",length($num1);
                   }
                }
                #print"----$file---\n";
                $i = 'N';
           } else { next;}
        }
        if ($x eq 'Y'){
           $dir = "/mailbackup/$user[$y]/";
           #print "dir=$dir\n";
           if (! -e $dir) { mkdir($dir, 0755);} #辨別是否有此使用者目錄,如沒有此使用者就新增此目錄
           print "$date_string $subj.eml\n";
           copy("$file", "$dir$date_string $subj.eml");
           unlink $file if -e $file;
           if (! -e $file) { print "$file 已備份匯出!\n"; }
           last;  # 此信件已尋找到符合帳號,所以跳出 while迴圈:
        }
        $y++;
    }
    #針對無法辨識信件歸類至 anyone目錄
    if (-e $file) {
        $dir = "/mailbackup/anyone/";
        copy("$file", "$dir$date_string $subj.eml");
        print "name=anyone\n";
        print "$date_string $subj.eml\n";
        unlink $file if -e $file;
        print "$file 已備份匯出!\n";
    }
    $total_size = $total_size + $filesize;
    print "匯出mail容量累計:$total_size MB\n";

    $mail_tol = $mail_size+$total_size;
    print "總容量:$mail_tol MB\n";

    if ($mail_tol >= $burn_szie){
        print "mail匯出已超過 $burn_szie MB容量,請放入DVD 片!!\n";
        $iname = makeISO();
        burniso($iname);
        #正式刪除mail檔案,只保留ISO檔
        !system "rm -rf /mailbackup/*" or die "can't delete:$!--/mailbackup/*\n";
        mail("jacky\@mail.xxxx.com.tw","已燒錄mail備份成功 !!","" );
    }

    last if ($mail_tol >= $burn_szie);
    print "換檔案\n";
}


# 複製完刪除
sub copy_dir{
  my $source = $_[0];
  my $target = $_[1];
  use File::Copy;
  opendir(DIR,$source);
  while(my $filename = readdir(DIR)){
    if(-f "$source/$filename"){
      copy("$source/$filename","$target/$filename") or die "can't copy: $!--$source/$filename\n";
      unlink "$source/$filename" if -e "$source/$filename";
    }
  }
  close(DIR);
}

#呼叫系統 shell cp
sub sys_copy{
  my $source = $_[0];
  my $target = $_[1];
  #use File::Copy;
  opendir(DIR,$source);
  while(my $filename = readdir(DIR)){
    if(-f "$source/$filename"){
      !system "cp -rfp $source/$filename $target/$filename" or die "can't copy: $!--$source/$filename\n";
      unlink "$source/$filename" if -e "$source/$filename";
    }
  }
  close(DIR);
}

#回傳指定目錄大小(KB)
sub dir_size{
  $dir = `du -sk $_[0]` ;
  $dir =~ s/$_[0]//;    #去除多餘文字
  $dir =~ s/\s+//;      #去除空白地方
  return $dir;
}


sub round {
    my $val = shift;
    my $col = shift;
    my $r = 10 ** $col;
    my $a = ($val > 0) ? 0.5 : -0.5;
    return int($val * $r + $a) / $r;
}

#製作影像檔
sub makeISO {
    ($sec,$min,$hour,$day,$mon,$year)=localtime(time);
    $year+=1900 ;
    $mon++ ;
    $isoname = "mailbackup$year-$mon-$day.iso";
    #$iso = Filesys::MakeISO->new;
    #$iso->image('mailbackup.iso');
    #$iso->dir('/mailbackup');
    #$iso->make_iso;
    #if(!$iso) {mail("jacky\@mail.xxxx.com.tw","無法制作ISO檔!!","$!" );}
    !system "mkisofs -o /backup/mail-iso/$isoname -r -J -graft-point /mailbackup/=/mailbackup/"
      or  die mail("jacky\@mail.xxxx.com.tw",'無法製作ISO檔!!',"$!" );
    return $isoname;
}

sub burniso {
    $isoname = $_[0];
    !system "cdrecord -v dev=2,0,0  fs=8m speed=4 -data -sao driveropts=burnfree /backup/mail-iso/$isoname" or die unlink "/backup/mail-iso/$isoname" if -e "/backup/mail-iso/$isoname" and mail("jacky\@mail.xxxx.com.tw","無法燒錄ISO檔:$isoname!!","$!" );
}


#sub del_file {
#   ;
#}

sub mail {
    #收件者
    $to = $_[0];
    # sendmail 程式路徑位址
    my $mailprg = "/usr/sbin/sendmail -t";
    # 寄件者 l
    my $from = "root\@mail.xxxx.com.tw";
    #主旨
    my $subj = $_[1];
    #內容
    my $messg = $_[2];

    open (MAIL,"|$mailprg") || die "$!\n";
    print MAIL "Return-Path: $from\n";
    print MAIL "From: $from\n";
    print MAIL "To: $to\n";
    print MAIL "Subject:$subj\n";
    print MAIL "\n";
    print MAIL "$messg\n";
    print MAIL "--- mail message for xxxx (c) 2009 written by mailbackup\n";
    close(MAIL);
    print "OK!\n\n";
}
阅读(2396) | 评论(1) | 转发(0) |
0

上一篇:IO::Compress::Gzip

下一篇:牛人写的 samba

给主人留下些什么吧!~~

chinaunix网友2009-08-10 10:45:29

好文