MySQL本地全量备份脚本

      通过学习田逸老师的《互联网运营智慧》,将田逸老师的Mysql全量备份的shell脚本翻译成了PERL。因为有些操作系统的默认的SHELL各不一样。所以换成PERL兼容性更强!

#!/usr/bin/perl

#Usage:mysql_bak.pl

 

use strict;

use warnings;

use DBI;

my $back_path="/tmp";

my $log_path="/tmp/mysql.log";

my $usr='zabbix';

my $password='KINGCARL';

 

open(FH,'>>',$log_path);

my $dbh=DBI->connect("DBI:mysql:zabbix",$usr,$password);

die "Connect failed:".DBI->errstr() unless $dbh;

my $sth=$dbh->prepare("show databases");

$sth->execute() or die "execute failed:".$sth.errstr();

while(my $name=$sth->fetchrow()){

my $time=localtime();

$time=~s/ /-/g;

#my $newfile="$back_path/$name-$time.tgz";

my $dumpfile="$back_path/$name-$time.sql";

print FH <<EOF;

------------------------------------------------------

--$time--

------------------------------------------------------

EOF

  if (-f $dumpfile) {

print FH <<EOF;

[$dumpfile]The Backup File is exists,Can't backup!

EOF

}else{

system("mysqldump --databases $name -p$password > $dumpfile");

system("bzip2 -z $dumpfile");

print FH <<EOF;

[$dumpfile]Backup Success!

EOF

system("rm -f $dumpfile");

}

}

你可能感兴趣的:(mysql,数据库,休闲,全量备份脚本,MSQL备份)