Chinaunix首页 | 论坛 | 博客
  • 博客访问: 125179
  • 博文数量: 28
  • 博客积分: 2431
  • 博客等级: 大尉
  • 技术积分: 321
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 18:40
文章分类

全部博文(28)

文章存档

2011年(3)

2010年(6)

2009年(7)

2008年(12)

分类:

2009-09-23 21:24:47

因为要用到,所以写了一个··但是写得很烂·····编码很烂····估计很多人看不懂··不是因为深奥,而是编码的问题····风格啊···就是懒(烂)
 

###############################################################################################

#本脚本是对服务器的数据库进行快速备份的工具。使用办法就是把这个脚本放进计划任务里边定时执行。    #

#备份方法有二:                                                                                                                                                                #

#1.使用mysqldump进行热备份。该方法速度较慢,但安全,移植性好。                                     #

#2.就是快速备份。直接copy数据库文件,但是要关闭mysql服务才可以,否则数据库很容易损坏。 #

#该脚本保留最新的5个完全备份文件,支持完全备份和增量备份(暂不支持),备份成功与否都发邮件通知 #

#管理员。                                                                                                                                                                            #

#有需要会加上ftp上传功能等。                                                                                                                 #

###############################################################################################

#usage:mysqlbackup.pl -u 用户名 -p 密码 -d 要备份的数据库名 -s 备份方式(a为完全,i为增量,c为直接复制文件)-f 备份的文件名 -h 显示用法

use strict;
use Getopt::Std;
use Mail::Sender;
#ftp服务器的配置

my $ftp_server='';
my $user='';
my $passwd='';
my $cwd='';
#-------------------------------------------------------------

#获取命令行参数

my $opt_string='u:p:d:s:f:h';
my %opts;
getopts($opt_string,\%opts)or &usage and exit;
#-------------------------------------------------------------

#参数初始化

my $backup_style=$opts{s};            #a为完全备份(all),i为增量备份(increment),c直接复制数据库文件

my($dbhost,$dbuser,$dbpasswd,$dbname,$opt)=('',$opts{u},$opts{p},$opts{d},'');
my $backup_dir='';
my $date;
my $bak_file;
#if(!$opts{f})

#{

    #$backup_dir='d:\\';

    #$date=&getdate;

    #$bak_file=$backup_dir.$date.'.sql';

#}

my $tool_dir='D:\Program Files\MySQL\MySQL Server 5.1\bin';
my $cmd='mysqldump.exe';
my $log='d:\\bak-log\\log.log';
#-------------------------------------------------------------

#执行备份

#执行完全备份

#导出整个数据库

#mysqldump -u用户名 -p密码 数据库名 > 导出的文件名

chdir $tool_dir ||die"$!\n";
($dbuser and $dbpasswd and $dbname) or die "错误:需要数据库的连接信息\n";
print "start backup.......\n";
if($opts{f})
{
    $opts{f}=~/(.*)\\.*/gi;
    die "系统找不到指定的路径\n" if !-e $1;
    $bak_file=$opts{f};
}
else
{
    if($opts{s} eq 'a')
    {
        print "检查旧的备份文件......\n";
        &check_point();
    }
}
if($backup_style eq 'a')            #a为完全备份,i增量,c快速

{
    my $start_time=localtime;
    my $flag;
    open FH,">>",$log||die "$!\n";
    open (STDERR, ">&FH") || die ("open STDERR failed");
    if(system("$cmd -u$dbuser -p$dbpasswd $dbname > $bak_file"))
    {
        print FH "backup faild at $start_time\n";
        print "backup faild.please check the log for detail.\n";
        $flag=0;
        &send_mail($flag);
        exit;
    }
    #`$cmd -u$dbuser -p$dbpasswd $dbname > $bak_file`; # >和$bak_file 之间要有空格

    my $end_time=localtime;
    print FH "$start_time:start backup database $dbname\n$end_time:backup database $dbname success\n";
    close FH||die "close handle error:$!\n";
    print "backup success\n";
    $flag=1;
    &send_mail($flag);
}
elsif($backup_style eq 'i')
{
    print "脚本暂不支持增量备份\n";
    exit;
}
elsif($backup_style eq 'c')
{
    #!system("net stop mysql")||die "$!\n";

    print "脚本暂不支持热备份\n"and exit;
}
else
{
    &usage;    
}
sub getdate
{
        my ( $y, $m, $d ) = (localtime)[ 5, 4, 3 ];
    $y += 1900;
    $m += 1;
    return $y.$m.$d ;
}
sub usage
{
    #my $opt_string='u:p:d:s:f:h';

    print <<EOF;
  用法:
       -u :数据库用户名
       -p :数据库密码
       -d dbname:要备份的数据库名
       -f f ile :文件名
       -s style :备份方式,a为完全,i为增量 ,c直接复制数据库文件,该参数要求关闭mysql服务
       -h help : 显示帮助

例子: $0 -u root -p root -d wei -s a -f filename
EOF
}
sub check_point
{
    #备份文件格式如:01.sql

    $backup_dir='d:\\bak\\';
    my $count=0;
    opendir DH, $backup_dir or die "Cannot open $backup_dir: $!";
  foreach(readdir DH)
  {
      next if $_!~/.*\.sql$/;
        if(/(0[1-5])\.sql$/gi)
        {
            $count++;
        }
  }
  closedir DH or die "close dir_hadle error:$!\n";;
        if($count>0 and $count <5)
        {
            $bak_file=$backup_dir.'0'.++$count.'.sql';
        }
        elsif($count==0)
        {
            $bak_file=$backup_dir.'01.sql';
        }
        else
        {
            for(qw/4 3 2 1/)
            {
                my $new_name=$_+1;
                rename ($backup_dir."0$_.sql",$backup_dir.'0'.$new_name.'.sql') or die "can't rename :$!\n";
            }
            $bak_file=$backup_dir.'01.sql';
        }
}
sub send_mail{
my $success=shift @_;
my $subject;
my $msg;
if($success)
{
    $msg='数据库备份成功';
    $subject='数据库备份成功';
}
else
{
    $msg='数据库备份失败';
    $subject='数据库备份失败';
}
my $sender;
$sender=new Mail::Sender();

    #my @protocols = $sender->QueryAuthProtocols(); 查询服务器支持的认证方式


if ($sender->MailMsg({
               #smtp => 'smtp.163.com',by Default,test@163.com


               #from => 'test@163.com', by Default


               to =>'fhfg4156456@139.com',
               subject => $subject, #主题


               msg => $msg, #内容


               auth => 'LOGIN', #smtp的验证方式


               authid => 'admin', #user


               authpwd => '123456', #pwd


     }) < 0) {
              warn "$Mail::Sender::Error\n";
         }
    print "邮件发送成功.\n";
}

    


阅读(1236) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~