利用Perl 脚本实现 MySQL 异机导入导出2013-10-30单位的开发同事需要将一个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
"; }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.....
"; 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!
"; }else{ print "Database $db_dbname_local is backup FALSE!
"; exit ; } print "Compress the dump file
"; system "gzip $db_backupdir_local/$db_dbname_local$db_time_local"; if ( $?==0 ){ print "Dump file is compress OK!
"; }else{ print "Dump file is compress FALSE!
"; 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
"; print "$keys[1] -> $values[1] Branch MySQL(1
"; print "$keys[2] -> $values[2] Release MySQL(2
"; print "Please choose the MySQL host IP which you want to import:
"; chomp($_=<STDIN>); if( m/0|1|2/ ){ chomp( $dbhost_remote="$values[$_]"); }else{ print "The mysql database is not found choose one from above list!
"; exit; } #### #sub improt_remote_db { # print "Please tell me which mysql db do you want to import to local:
"; 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
"; }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.....
"; `/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!
"; }else{ print "Database $db_dbname_remote is export FALSE!
"; exit; } print "Import $db_host_remote mysql $db_dbname_remote begin.....
"; 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!
"; }else{ print "Database $db_dbname_remote is import FALSE!
"; exit; } #} #### unlink "$db_backupdir_remote/*"; print "Import $db_host_remote mysql $db_dbname_remote is OK
"; #END本文出自 “影子骑士” 博客,请务必保留此出处http://andylhz2009.blog.51cto.com/728703/980818