-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecover_innodb_tables
executable file
·86 lines (63 loc) · 1.99 KB
/
recover_innodb_tables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby
require 'ftools'
# You'll definitely need to change these
TOOLS_DIR = "/mnt/recover/src/innodb-tools"
TABLE_LIST = "./table_list"
USER = "root"
DB = "typo"
# You probably don't have to change these
DEFS_DIR = "./defs"
TSV_DIR = "./tsv" # That's tab-separated value, of course
def prep_environment
unless File.exist?("ibdata1")
abort "You must run recover_innodb_tables from the directory containing ibdata1."
# or you must fork this and put in some option parsing!
end
Dir.mkdir(DEFS_DIR) unless FileTest.directory?(DEFS_DIR)
Dir.mkdir(TSV_DIR) unless FileTest.directory?(TSV_DIR)
end
def defs_file(table)
"#{DEFS_DIR}/#{table}.defs"
end
def tsv_file(table)
"#{TSV_DIR}/#{table}.tsv"
end
def create_defs(table)
# We'll only create the def once, so you can tweak it by hand
return if File.exist?(defs_file(table))
puts "Creating #{table}.defs"
defs = %x[#{TOOLS_DIR}/create_defs.pl --user=#{USER} --db=#{DB} --table=#{table}]
File.open(defs_file(table), "w") {|f| f.write(defs) }
end
def needs_parsing(table)
return true unless File.exist?(tsv_file(table))
File.mtime(defs_file(table)) > File.mtime(tsv_file(table))
end
def build_parser(table)
puts "Building parser"
File.copy(defs_file(table), "#{TOOLS_DIR}/include/table_defs.h", "w") {|f| f.write(defs) }
out = %x[cd #{TOOLS_DIR};make]
if $?.exitstatus != 0
puts out
abort "Make failed: #{$?.exitstatus}"
end
end
def parse_data(table)
puts "Parsing ibdata"
# If your file is huge, this is a horrible memory-hogging way to do it.
# My file was small, and my first attempt at redirecting output with system()
# didn't work for some reason, so...
out = %x[#{TOOLS_DIR}/constraints_parser -f ibdata1 -5]
File.open(tsv_file(table), "w") {|f| f.write(out) }
end
prep_environment
tables = open(TABLE_LIST).readlines
tables.each do |t|
t = t.chomp.rstrip
puts "Recovering table '#{t}'"
create_defs(t)
if needs_parsing(t)
build_parser(t)
parse_data(t)
end
end