從mysqldump全備獲取指定庫的sql
使用說明:
mysqldump備份檔案為/data/bak_dump.sql
1> 提取資料庫db1的sql並儲存在當前目錄下
perl fetch_db_dumpsql.pl -s=bak_dump.sql -B=db1
2> 提取資料庫db1和db2的sql並儲存在當前目錄下
perl fetch_db_dumpsql.pl -s=xxx.sql -B=db1,db2
3> 提取資料庫db1、db2和db3的sql並儲存在/tmp/目錄下
perl fetch_db_dumpsql.pl -s=xxx.sql -B=db1,db2,db3 -O=/tmp/
點選(此處)摺疊或開啟
-
#!/usr/bin/perl -w
use strict;
use Getopt::Long qw(:config no_ignore_case); # use GetOption function
use Term::ANSIColor;
# mysql config
my $output_path = `pwd`;
chomp($output_path);
my $dbstr;
my $srcfile='';
# log file
my $logfile;
# ----------------------------------------------------
# handle Ctrl+C
sub catch_zap {
my $signame = shift;
print "\nExit Now...\n\n";
exit;
}
$SIG{INT} = \&catch_zap;
# ----------------------------------------------------------------------------------------
# Main()
# ----------------------------------------------------------------------------------------
sub main{
# get input params
&get_option();
&do_fetch();
}
&main();
# ----------------------------------------------------------------------------------------
# Func : get input params
# %2c-an-Introduction
# ----------------------------------------------------------------------------------------
sub get_option{
GetOptions('h|help'=> \&help_print,
's|srcfile=s' => \$srcfile,
'B|databases=s' => \$dbstr,
'O|outputdir=s' => \$output_path
) or help_print();
# must set databases
if (($srcfile eq '') or ($dbstr eq '')){
&help_print();
}
$logfile = qq{$output_path/fetch.log};
if (!(-e $logfile)){
`touch $logfile`;
}
print "srcfile=$srcfile, databases=$dbstr, output=$output_path \n";
}
# ----------------------------------------------------------------------------------------
# Func : fetch dbs's sql
# ----------------------------------------------------------------------------------------
sub do_fetch{
my @dbs = split(',',$dbstr);
my %dbhash;
my $spfir = ',';
my $spsec = ';';
my $m_srcfile = $srcfile;
my $sh;
# get head,tail info;
my $headendpos=1;
$sh = qq{grep -n '/\\\*!40101 SET SQL_MODE=\@OLD_SQL_MODE \\\*/;' $m_srcfile};
my $res = `$sh`; chomp($res);
my $tailbgpos = `echo '$res' | cut -d : -f 1`; chomp($tailbgpos);
# get all db pos
$sh = qq{grep -n -i \'^-- Current Database: `\' $m_srcfile };
open FILE_ALLDB, " $sh | " or die "can't do shell:$sh";
my ($bgpos,$endpos,$curpos,$curdb,$lastdb,$lastpos);
my $i=1;
while (my $line =){
# line format "no:USE `dbname`"
chomp($line);
my @arline = split(':',$line);
$curpos = `echo '$line' | awk -F: \'{print \$1}\'`; chomp($curpos);
$curdb = `echo '$line' | awk -F\\\` \'{print \$2}\'`; chomp($curdb);
# first db
if ($i > 1){
$endpos = $curpos - 1;
$dbhash{$lastdb} .= "$lastpos$spfir$endpos$spsec";
}elsif ($i == 1){
$headendpos = $curpos - 1;
}
$lastdb = $curdb;
$lastpos = $curpos;
$i++;
}
close FILE_ALLDB;
# LAST DB. endpos = tailbgpos-1;
$endpos = $tailbgpos - 1;
$dbhash{$lastdb} .= "$lastpos$spfir$endpos$spsec";
# do fetch
my ($posstrdb, $posstr, @posardb, @posar);
my $outfile;
#foreach my $db (keys %dbhash){
foreach my $db (@dbs){
# fetch one db sql
print "============= start fetch $db sqls\n";
my $posstrdb = $dbhash{$db};
if (defined($posstrdb)){
#print "==== $db:$posstrdb\n";
$outfile = qq{$output_path$db.sql};
# head sql
$sh = qq{sed -n '1,$headendpos p' $m_srcfile > $outfile};
`$sh`;
# db sql
@posardb = split($spsec,$posstrdb);
foreach $posstr (@posardb){
@posar = split($spfir,$posstr);
$bgpos = $posar[0];
$endpos = $posar[1];
$endpos = '$' unless defined($endpos);
# fetch sql
$sh = qq{sed -n '$bgpos,$endpos p' $m_srcfile >> $outfile};
print colored ["Green "],"$db pos: $bgpos--$endpos";
print colored ["reset"],"\n";
`$sh`;
#`$sh` or die "Can't fetch $db sqls";
}
# tail sql
$sh = qq{sed -n '$tailbgpos,\$ p' $m_srcfile >> $outfile};
`$sh`;
}else{
print colored ["red on_yellow"],"database:$db not exist!";
print colored ["reset"],"\n";
}
}
}
# ----------------------------------------------------------------------------------------
# Func : print help information
# ----------------------------------------------------------------------------------------
sub help_print{
print <=========================================================================================
Info :
Created By babaoqi
Usage :
Command line options :
-h, --help Print Help Info.
-s, --srcfile src dumpsql file
-B, --databases fetch some databases.
-O, --outfile output sql file. default:cur path
Sample :
shell> perl fetch_db_dumpsql.pl -s=xxx.sql -B=db1
shell> perl fetch_db_dumpsql.pl -s=xxx.sql -B=db1,db2
shell> perl fetch_db_dumpsql.pl -s=xxx.sql -B=db1,db2,db3 -O=/tmp/
=========================================================================================
EOF
exit ;
}
-
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-1065939/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Mysql】mysqldump全備中還原指定的庫MySql
- mysqldump備份單庫、部分庫、全庫、及排除部分庫MySql
- Mysqldump 在備庫進行備份時會阻塞備庫的sql_threadMySqlthread
- Sql Server 獲取指定表、檢視結構SQLServer
- mssql sqlserver 從指定字串中獲取數字的方法SQLServer字串
- 用sed從mysqldump全備檔案中取出某張表的表結構MySql
- mysqldump 全量和增量備份指令碼MySql指令碼
- 將MYSQLDUMP全庫備份分為單庫檔案的方法(自己寫的小工具)MySql
- mysqldump 資料庫備份程式MySql資料庫
- mysql 備份資料庫 mysqldumpMySql資料庫
- 【SQL】獲取指定範圍內結果集的實現方法SQL
- MySQL主從配置及mysqldump備份MySql
- 【SQL】SQL解惑-如何從字串中獲取IP地址SQL字串
- oracle資料庫獲取指定表的列的相關資訊Oracle資料庫
- js如何獲取指定元素的尺寸JS
- js獲取指定月份的天數JS
- JavaScript 獲取指定月份的天數JavaScript
- 從遠端把mysql透過mysqldump備份資料庫到本地MySql資料庫
- 利用MySQL全備份(mysqldump),如何只恢復一個庫或者一個表?MySql
- 獲取資料庫空閒空間的SQL資料庫SQL
- SQL解惑-如何從字串中獲取IP地址SQL字串
- MySQLDump的備份方法MySql
- Mysqldump的備份流程MySql
- JavaScript 獲取指定區間的數字JavaScript
- JavaScript獲取table表格指定列的值JavaScript
- jQuery如何獲取指定元素的索引值jQuery索引
- JavaScript獲取指定元素的同輩元素JavaScript
- jquery獲取具有指定內容的元素jQuery
- jQuery獲取表格的指定行和列jQuery
- Mysql備份系列(2)--mysqldump備份(全量+增量)方案操作記錄MySql
- MySQL修改字符集(mysqldump轉換全庫)MySql
- JavaScript使用id獲取指定元素JavaScript
- mysqldump全量備份+mysqlbinlog二進位制日誌增量備份MySql
- jquery實現的獲取指定元素指定型別元素數目jQuery型別
- 從全備份的SQL語句中恢復某張表 [原創]SQL
- js實現從陣列中獲取相加和為指定數字的元素JS陣列
- 獲取資料庫中到指定經緯度距離的座標資料庫
- git 從遠端倉庫獲取所有分支Git