加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl 脚本实现MySQL 异机导入导出

发布时间:2020-12-15 21:06:26 所属栏目:大数据 来源:网络整理
导读:单位的开发同事需要将一个mysql 中的库导入到本地mysql中 所以为了减少手工操作,就写了下面的一个用perl 实现的 mysql 异机导入导出脚本:如下 ?#!/usr/bin/perl ?-w ? #Author:andylhz #Date:20120/08/29 #Purpose:Import database from other mysql db ho

单位的开发同事需要将一个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_localn";
}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 filen";
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(0n";
print "$keys[1] -> $values[1] Branch MySQL(1n";
print "$keys[2] -> $values[2] Release ?MySQL(2n";
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_remoten";
}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 OKn";
?
?
#END

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读