Skip to content

Instantly share code, notes, and snippets.

@saetia
Last active June 26, 2017 16:59
Show Gist options
  • Save saetia/2369908 to your computer and use it in GitHub Desktop.
Save saetia/2369908 to your computer and use it in GitHub Desktop.
Parse MySQL dumps
#!/usr/bin/perl -w
use strict;
my $dump_file = shift @ARGV;
my $out_dump_file = 'stripped_dump.sql';
open DUMP, "<$dump_file" or die $!;
open OUT, ">$out_dump_file" or die $!;
my @required_tables = @ARGV;
my $table_dump_started = 0;
while (<DUMP>){
if (index($_, 'CREATE TABLE') >= 0){
$table_dump_started = 0;
foreach my $table (@required_tables){
if (index($_, "`$table`") >= 0){
$table_dump_started = 1;
last;
}
}
}
print OUT
if $table_dump_started;
}
close OUT;
close DUMP;
ruby parse.rb metropoliscoffee.db.120411.dump
#!/usr/bin/ruby
if ARGV.length == 1
dumpfile = ARGV.shift
else
puts("\033[31mhow to:\033[0m ruby parse.rb mysql.dump\n")
exit 1
end
STDOUT.sync = true
if File.exist?(dumpfile)
d = File.new(dumpfile, "r")
outfile = false
table = ""
directory = File.basename(dumpfile)+'.tables'
Dir.mkdir(directory) unless File.directory?(directory)
while (line = d.gets)
if line =~ /^-- Table structure for table .(.+)./
table = $1
puts("\033[32mfound\033[0m #{table}\n")
outfile = File.new("#{directory}/#{table}.sql", "w")
end
if table != "" && outfile
outfile.syswrite line
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment