Perl 腳本實現MySQL 異機導入導出

單位的開發同事需要將一個mysql 中的庫導入到本地mysql中 所以爲了減少手工操作,就寫了下面的一個用perl 實現的 mysql 異機導入導出腳本:如下

 #!/usr/bin/perl  -w

 
#Author:andylhz
#Date:20120/08/29
#Purpose:Import database from other mysql db host to local.
 
 
my $db_host_local="localhost";
my $db_username_local="root";
my $db_password_local="password";
my $db_dbname_local="baby";
my $db_backupdir_local="/root/dbbackup/local";
if (-e $db_backupdir_local){
    print "The database backup dir is $db_backupdir_local\n";
}else{
    mkdir $db_backupdir_local;
}
my $db_time_local=`date +%y%m%d%H%M%S`;
#backup localhost databases
print "$db_dbname_local is backuping.....\n";
system "/usr/local/mysql/bin/mysqldump -h $db_host_local -u$db_username_local -p$db_password_local --databases $db_dbname_local >$db_backupdir_local/$db_dbname_local$db_time_local";
if ( $?==0 ){
       print "Database $db_dbname_local is backup OK!\n";
}else{
       print "Database $db_dbname_local is backup FALSE!\n";
       exit ;
}
 
print "Compress the dump file\n";
system  "gzip  $db_backupdir_local/$db_dbname_local$db_time_local";
 
if ( $?==0 ){
       print "Dump file is compress OK!\n";
}else{
       print "Dump file is compress FALSE!\n";
      exit;
}
 
####
 
#!/usr/bin/perl
 
 
%iplist=('144trunk'=>'192.168.100.144','114branch'=>'192.168.100.114','164release'=>'192.168.100.164');
@keys= keys %iplist;
@values= values %iplist;
 
print "$keys[0] -> $values[0] Trunk   MySQL(0\n";
print "$keys[1] -> $values[1] Branch MySQL(1\n";
print "$keys[2] -> $values[2] Release  MySQL(2\n";
print "Please choose the MySQL host IP which you want to import:\n";
chomp($_=<STDIN>);
if( m/0|1|2/ ){
    chomp( $dbhost_remote="$values[$_]");
}else{
    print "The mysql database is not found choose one from above list!\n";
    exit;
}
 
####
 
#sub improt_remote_db {
# print "Please tell me which mysql db do you want to import to local:\n";
my $db_host_remote="$dbhost_remote";
my $db_username_remote="root";
my $db_password_remote="password";
my $db_dbname_remote="baby";
my $db_backupdir_remote="/root/dbbackup/remote";
if (-e $db_backupdir_remote){
    print "The database export dir is $db_backupdir_remote\n";
}else{
    mkdir $db_backupdir_remote;
}
 
my $db_time_remote=`date +%y%m%d%H%M%S`;
#backup localhost databases
print  "The database $db_dbname_remote is dumping.....\n";
`/usr/local/mysql/bin/mysqldump -h $db_host_remote -u$db_username_remote -p$db_password_remote --databases $db_dbname_remote >$db_backupdir_remote/$db_dbname_remote$db_time_remote`;
 
if ( $?==0 ){
       print "Database $db_dbname_remote is export OK!\n";
}else{
       print "Database $db_dbname_remote is export FALSE!\n";
       exit;
}
 
print "Import $db_host_remote  mysql $db_dbname_remote begin.....\n";
 
system "/usr/local/mysql/bin/mysql -u$db_username_local -p$db_password_local $db_dbname_local < $db_backupdir_remote/$db_dbname_remote$db_time_remote";
 
if ( $?==0 ){
       print "Database $db_dbname_remote is import OK!\n";
}else{
       print "Database $db_dbname_remote is import FALSE!\n";
       exit;
}
 
#}
####
unlink "$db_backupdir_remote/*";
print "Import $db_host_remote  mysql $db_dbname_remote is OK\n";
 
 
#END
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章