Skip to content

Instantly share code, notes, and snippets.

@saetia
Last active June 26, 2017 16:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
ruby -e "$(curl -fsSkL https://gist.githubusercontent.com/saetia/2369908/raw/4bf9a6d36060582e69f02c314f66449971a7bb11/parse.rb)" /Users/Joel/site.db.121010.dump
ruby parse.rb site.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).gsub(/[^0-9a-z\.\_]/i, '')+'.tables'
Dir.mkdir(directory) unless File.directory?(directory)
while (line = d.gets)
if line =~ /^-- Table structure for table .(.+)./
table = $1.gsub(/[^0-9a-z\.\_]/i, '')
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