how to fork mysqldump in perl cgi script

how to fork mysqldump in perl cgi script

am 03.08.2011 13:32:02 von Agnello George

Hi

I have the following cgi script that that takes input of the a
remote database to dumped to a local server . $db1 is the input
which is a database-name . The issue is that some of the data base is
taking to long to get dumped from the remote server on to my local
server and and i get a connection time out my script breaks , i was
told to use fork , but i cant seem to get a workable example of
forking process . Can some one help me with this . Thanks a million


( below is my code )
------------------------------------------------
#!/usr/bin/perl


use strict ;
use warnings ;
use Template;
use Data::Dumper;
use Net::SSH::Perl ;
use CGI;
use POSIX;
use DBI;
my $query = new CGI;

print "Content-type: text/html\n\n";

my $tt = Template->new( INCLUDE_PATH => "/var/www/cgi-bin/template" )
|| die "template process failed:$!";

my %tag ;

my %databases_dc = (

'social' => '172.16.1.1',
'mboarddb' => '172.16.1.1',
'auth' => '172.16.1.1' );

my %localsqlserver = ( user => 'user',
passwd => 'passwd' ) ;

my ( $db,%db,$datestr,$host,$status,$valu,@alldb);

@alldb = &get_all_mysql_with_datestamp();

if ($ENV{'REQUEST_METHOD'} eq "POST") {

my ($db1) = $query->Vars; $db = $db1;

$datestr = strftime("%Y_%m_%d_%H_%M", localtime) ;

$host = '192.168.1.57';

$valu = &check_if_local_remt_db_exist($db);

if ( $valu == 2 ) {
$status = " unable to connect to host 192.168.1.57";
} elsif ( $valu == 3 ) {
$status = " unable to connect $db --> $databases_dc{$db} ";
} elsif ( $valu == 1 ) {
$status = " the DB $db alread exist ";
} elsif ( $valu == 0) {
$status = " creating $db on 192.168.1.57 \n";
$status .= &create_db_local($db);
}


}

%tag = ( 'result' => $status,
'alldb' => @alldb );

sub create_db_local {
my $DB = $_[0] ;

my @all_strng = ("$DB","$datestr");
my $new_DB = join("_",@all_strng);

#this is the dump from a remote server to a local server -h is
172.16.1.1 ( the problem lies here )
`mysqldump --single-transaction $db -h $databases_dc{$db} -u
captain -pPASSwOrd > /temp/$new_DB.sql`;

`mysql -h 192.168.1.57 -u all_user -ppasswd -e "create database $new_DB"`;
`mysql $new_DB -h 192.168.1.57 -u all_user -ppasswd < /temp/$new_DB.sql`;
return $new_DB;
}


sub check_if_local_remt_db_exist {

my ( $DBH_local,$DBH_rmt,$use_db, $result);
my $DB = "$_[0]"."_"."$datestr" ;

$DBH_rmt = DBI->connect("DBI:mysql:$db:$databases_dc{$db}", "root",
"Passw0r", {PrintError => 1, RaiseError => 1 }) or return 3;
$DBH_local=DBI->connect("DBI:mysql::192.168.1.57", "user", "passwd")
or return 2 ;


if ( $DBH_local){
$use_db = $DBH_local->prepare("SELECT SCHEMA_NAME FROM
INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ? ") ;
$use_db->execute($DB);
$result = $use_db->fetchrow_arrayref;
}

if ( $DBH_local && $DBH_rmt ) {
if ( "$result->[0]" eq "$DB" ) {
print "$result->[0] == $DB";
return 1;
}else {
return 0;
}
}
$DBH_local->disconnect();
$DBH_rmt->disconnect();
return 2;

}


sub get_all_mysql_with_datestamp {

my ($DBH_local,$sth,$aref,@dbs) ;
$DBH_local = DBI->connect("DBI:mysql::192.168.1.57", "all_user",
"all_passwd") or return 2 ;
$sth = $DBH_local->prepare(q{SHOW DATABASES}) or die "Unable to
prepare show databases: ". $DBH_local->errstr."\n";
$sth->execute or die "Unable to exec show tables: ". $DBH_local->errstr."\n";
while ($aref = $sth->fetchrow_arrayref) {
next unless ($aref->[0] =~ /\w+_(\d{4})_(\d\d)_(\d\d)_(\d\d)_(\d\d)/);
push(@dbs,"$aref->[0]");
}
return \@dbs;
}

# push @{$tag{results}},


$tt->process("syncdata1.tmpl",\%tag) || die $tt->error();



--



Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/