[代码]一个多数据库备份程序

[代码]一个多数据库备份程序

[代码]一个多数据库备份程序
你可以运行在linux的crond里,这样系统会自动备份多个数据库形成文件。

[quote]#!/usr/bin/perl
use FindBin qw($Bin);
use strict;
use DBI;

$|=1;
################################################################################################
# this is tool for database backup,this script run under crond system in linux, you also can
# add info to @datainfo with your new backup rules.
#-----------------------------------------------------------------------------------------------
# design by hoowa
# last modify 2005-5-8
################################################################################################
#database rules
my @datainfo=(
{host=>'localhost',user=>'abc',pass=>'password',dbname=>'daaaa',tables=>['index','chinese','english']},
{host=>'192.168.1.142',user=>'backer',pass=>'8786554',dbname=>'en',tables=>[]},
);
#path settings
my $dump = '/usr/local/mysql/bin/mysqldump'; #dumper command
my $savedir='/mnt/dbbackup'; #backup save to where
my $tempdir = '/tmp'; #temporary file dir
my $logfile = 'dbbacker.log.txt'; #access logs
my $debug = 0; #any number unless 0 view debug info
#checking paths setting
unless (-e"$dump") { &info("can't find $dump\n"); exit; }
unless (-d"$savedir") { &info("can't find $savedir\n"); exit; }
unless (-d"$tempdir") { &info("can't find $tempdir\n"); exit; }

&info("DataBase BACKER by hoowa");
foreach my $onedb (@datainfo) {

# Anyway to delete all dbbacker files before
&info("===========================================================================");
&info("backing $onedb->{'dbname'}.* by $onedb->{'user'} @ $onedb->{'host'}......");
system("rm -f $tempdir/*.dbbacker");#delete all temporary database file
sleep(1);

###################################################################
#Getting table name from parameters or remote database
my (@tables);
if (ref($onedb->{'tables'}) eq "ARRAY" && $#{$onedb->{'tables'}} >= 0) {
&info("param tables is [$#{$onedb->{'tables'}}]");
@tables = @{$onedb->{'tables'}};
} else {
&info("Connecting database......");

#goto next loop if database was error
my $dbh = DBI->connect("DBI:mysql:database=$onedb->{'dbname'};host=$onedb->{'host'}",$onedb->{'user'},$onedb->{'pass'}) or die next;
my $sth = $dbh->prepare("show tables");
$sth->execute or die $dbh->errstr;
while (my $ref = $sth->fetchrow_arrayref()) {
push(@tables,$ref->[0]);
}
$sth->finish();
$dbh->disconnect();
&info("db-fetch tables is [$#tables]");
}

###################################################################
# Dumping database
foreach $_ (@tables) {
&info("dumping $_.dbbacker......");

unlink("$tempdir/$_.dbbacker") if (-e"$tempdir/$_.dbbacker");
system("$dump -C -h $onedb->{'host'} -u $onedb->{'user'} -p$onedb->{'pass'} $onedb->{'dbname'} $_ > $tempdir/$_.dbbacker");
}

###################################################################
# Compress all files
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900; $mon++;
&info("compressing......");
sleep(1);
system("tar zcf $savedir/$onedb->{'host'}.$onedb->{'dbname'}.$year-$mon-$mday-$hour$min.tar.gz $tempdir/*.dbbacker");
sleep(1);

###################################################################
# Anyway delete all dbbacker files after next loop
system("rm -f $tempdir/*.dbbacker");
}
exit;

END {
&info("deleting all temporary files......");
sleep(1);
system("rm -f $tempdir/*.dbbacker");
&info("bye!bye!");
}

sub info
{
my ($msg)=@_;

my $time = localtime;
warn "[$time] $msg\n" if ($debug ne '0');
open(LOG,">>$Bin/$logfile");
print LOG "[$time] ".$msg."\n";
close(LOG);
return;
}
[/quote]

修改几个配置文件就可以在你机器上运行了。
我靠,一个顶的...
我靠,一个顶的都没有。是不是都不管理数据库啊
[CCB]2[/CCB] [CCB]2[/CCB]
收下了,顶一个先,应该不错,学到不少东西
这是备份 mysql...
这是备份 mysql 数据库。

程序在数据库 dump 之前,怎么没有看到 lock tables 呢? 步骤一般该是

lock tables
mysqldump
unlock tables
mysql不熟啊,哈哈。[fly]顶一下[/fly]
收了收了,...