forked from ripienaar/mysql-dump-split
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split-mysql-dump.rb
145 lines (123 loc) · 4.09 KB
/
split-mysql-dump.rb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env ruby
require 'optparse'
require 'zlib'
tables = []
ignore = []
dumpfile = ""
@compress = false
def new_outfile(filename)
if @compress
file = Zlib::GzipWriter.open("#{filename}.gz")
else
file = File.new(filename, "w")
end
# It is always a good idea to update these variables before we start
file.puts("/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;")
file.puts("/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;")
file.puts("/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;")
file.puts("/*!40101 SET NAMES utf8 */;")
file.puts("/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;")
file.puts("/*!40103 SET TIME_ZONE='+00:00' */;")
file.puts("/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;")
file.puts("/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;")
file.puts("/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;")
file.puts("/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;")
file
end
def close_outfile(file)
if file and !file.closed?
# It is always a good idea to restore these variables once we're done
file.puts("/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;")
file.puts("/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;")
file.puts("/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;")
file.puts("/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;")
file.puts("/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;")
file.puts("/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;")
file.puts("/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;")
file.close
end
end
cmds = OptionParser.new do |opts|
opts.banner = "Usage: split-mysql-dump.rb [options] [FILE]"
opts.on("-s", "Read from stdin") do
dumpfile = $stdin
end
opts.on("-t", '--tables TABLES', Array, "Extract only these tables") do |t|
tables = t
end
opts.on("-i", '--ignore-tables TABLES', Array, "Ignore these tables") do |i|
ignore = i
end
opts.on("-c", '--compress', Array, "Compress split files with Gzip") do
@compress = true
end
opts.on_tail("-h", "--help") do
puts opts
end
end.parse!
if dumpfile == ""
dumpfile = ARGV.shift
if not dumpfile
puts "Nothing to do"
exit
end
end
STDOUT.sync = true
if File.exist?(dumpfile)
if dumpfile == $stdin
d = $stdin
else
d = File.new(dumpfile, "r")
end
outfile = nil
table = nil
db = nil
linecount = tablecount = starttime = 0
while (line = d.gets)
# Detect table changes
if line =~ /^-- Table structure for table .(.+)./ or line =~ /^-- Dumping data for table .(.+)./
is_new_table = table != $1
table = $1
# previous file should be closed
if is_new_table
close_outfile(outfile)
puts("\n\nFound a new table: #{table}")
if (tables != [] and not tables.include?(table))
puts"`#{table}` not in list, ignoring"
table = nil
elsif (ignore != [] and ignore.include?(table))
puts"`#{table}` will be ignored"
table = nil
else
starttime = Time.now
linecount = 0
tablecount += 1
outfile = new_outfile("#{db}_#{table}.sql")
outfile.write("USE `#{db}`;\n\n")
end
end
elsif line =~ /^-- Current Database: .(.+)./
db = $1
table = nil
close_outfile(outfile)
outfile = new_outfile("#{db}_1create.sql")
puts("\n\nFound a new db: #{db}")
elsif line =~ /^-- Position to start replication or point-in-time recovery from/
db = nil
table = nil
close_outfile(outfile)
outfile = new_outfile("1replication.sql")
puts("\n\nFound replication data")
end
# Write line to outfile
if outfile and !outfile.closed?
outfile.write(line)
linecount += 1
elapsed = Time.now.to_i - starttime.to_i + 1
print(" writing line: #{linecount} in #{elapsed} seconds \r")
end
end
end
# Let's not forget to close the file
close_outfile(outfile)
puts