MySQL 备份:从mysqldump全备中 匹配出某几个表

作用:
通过 perl 脚本匹配出 mysqldump全备文件中的某个库的某几个表。

经测试perl是shell的N倍

请添加图片描述
208G的文件需要47分钟

vim test.pl
#!/usr/bin/perl

use strict;
use warnings;

# mysqldump 文件路径
my $dump_file = "/tmp/test.sql";
# 输出文件路径
my $output_file = "/tmp/filter.sql";
# 需要获取的库
my $database = 'database';
# 需要获取的表 多个表用逗号分隔
my $tables = "t1,t2,t3,t4,t5";


##############################################################################################################
my $database_switch = 0;
my $table_switch    = 0;

# 保存旧的IFS值
my $OLD_IFS = $,;

# 将IFS设置为逗号
$, = ',';

# 将逗号分隔的字符串拆分成数组
my @table_array = split(/,/, $tables);

# 恢复IFS为旧值
$, = $OLD_IFS;

# 对表格数组进行排序
@table_array = sort @table_array;

if (-e $output_file) {
    die "输出文件 $output_file 已存在,请确认文件内容后删除文件并重新执行脚本\n";
}

open my $out_fh, '>', $output_file or die "无法打开输出文件 $output_file: $!\n";

open my $dump_fh, '<', $dump_file or die "无法打开输入文件 $dump_file: $!\n";

# 读取文件的前18行
for (my $i = 0; $i < 18; $i++) {
    my $line = <$dump_fh>;
    last unless defined $line;
    print $out_fh $line;
}

# 使用while循环读取文件的每一行
while (my $line = <$dump_fh>) {
    chomp $line;

    if ($line =~ /^-- Current Database: \`$database\`$/) {
        $database_switch = 1;
    }
    elsif ($line =~ /^-- Current Database:/ && $line !~ /^-- Current Database: \`$database\`$/) {
        $database_switch = 0;
    }

    foreach my $table (@table_array) {
        if ($line =~ /^-- Table structure for table \`${table}\`$/) {
            $table_switch = 1;
            last;
        }
        elsif ($line =~ /^-- Table structure for table/ && $line !~ /^-- Table structure for table \`${table}\`$/) {
            $table_switch = 0;
        }
    }

    if ($database_switch == 1 && $table_switch == 1) {
        print $out_fh "$line\n";
    }
}

# 使用反引号获取tail命令的输出
my $result = `tail -n 11 $dump_file`;
print $out_fh $result;

close $dump_fh;
close $out_fh;

print "转储完成保存在 $output_file\n";

运行

chmod +x  test.pl
./test.pl

这个是shell 版本的

#!/bin/bash

# mysqldump 文件路径
dump_file="/tmp/test.sql"
# 输出文件路径
output_file="/tmp/filter.sql"
# 需要获取的库
database='database'
# 需要获取的表 多个表用逗号分隔
tables="t1,t2,t3,t4,t5"





database_switch=0
table_switch=0

# 保存旧的IFS值
OLD_IFS=$IFS

# 将IFS设置为逗号
IFS=','

# 将逗号分隔的字符串拆分成数组
table_array=($tables)

# 恢复IFS为旧值
IFS=$OLD_IFS

# 对表格数组进行排序
table_array=($(printf '%s\n' "${table_array[@]}" | sort))

if [[ -e "${output_file}" ]]; then
    echo "输出文件 ${output_file} 已存在,请确认文件内容后删除文件并重新执行脚本"
    exit 1
fi

head  -n 18 ${dump_file}  >${output_file}
# 使用while循环读取文件的每一行
while IFS= read -r line; do
    if [[ "${line}" == "-- Current Database: \`$database\`" ]]; then
        database_switch=1
    elif [[ "${line}" == "-- Current Database:"* && "${line}" != "-- Current Database: \`$database\`" ]]; then
        database_switch=0
    fi

    for table in "${table_array[@]}"; do
        if [[ "${line}" == "-- Table structure for table \`${table}\`" ]]; then
            table_switch=1
            break
        elif [[ "${line}" == "-- Table structure for table"* && "${line}" != "-- Table structure for table \`${table}\`" ]]; then
            table_switch=0
        fi
    done

    if [[ ${database_switch} -eq 1 && ${table_switch} -eq 1 ]]; then  
        #echo "${line} (Table: ${table})"
        echo "${line}" >> "${output_file}"
    fi
done < "$dump_file"

tail -n 11 ${dump_file}  >>${output_file}


echo "转储完成保存在 ${output_file}"
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值