Class: MysqlTruck::Dumper
- Inherits:
-
Object
- Object
- MysqlTruck::Dumper
show all
- Includes:
- FileUtils, Helper
- Defined in:
- lib/mysql_truck/dumper.rb
Constant Summary
collapse
- REGEX =
/,?\s*(UNIQUE)?\s*KEY\s`[A-Za-z\d_]+`\s*\([A-Za-z\d_,`]+\),?\s*/m
Instance Method Summary
collapse
Methods included from Helper
#base_path, #benchmark, #bucket_dir, #config, #csv_options, #db_connection_options, #formatted_time, #initialize_directories, #initialize_s3, #local_host?, #remote_host?, #remove_directories, #s3_path, #tmp_path
Constructor Details
#initialize(config) ⇒ Dumper
Returns a new instance of Dumper.
8
9
10
11
12
13
14
|
# File 'lib/mysql_truck/dumper.rb', line 8
def initialize(config)
@config = config
@time = Time.now
initialize_s3
initialize_directories
end
|
Instance Method Details
#dump ⇒ Object
16
17
18
19
20
|
# File 'lib/mysql_truck/dumper.rb', line 16
def dump
dump_data
upload
remove_directories
end
|
#dump_data ⇒ Object
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
|
# File 'lib/mysql_truck/dumper.rb', line 22
def dump_data
mysql = Mysql.init
mysql.options(Mysql::SET_CHARSET_NAME, "utf8")
mysql.connect(
config[:host],
config[:username],
config[:password],
config[:database],
config[:port]
)
mysql.query("SET NAMES utf8")
tables.each do |table|
puts "Dumping #{table}..."
next if compressed_files_exist?(table) && smartly?
if dump_table?(table)
base_cmd = "mysqldump --quick"
schema_cmd = "#{base_cmd} --no-data "
schema_cmd += "#{db_connection_options} #{table}"
schema_cmd += " > #{filename(table)[:schema_file]}"
puts schema_cmd
benchmark { `#{schema_cmd}` }
puts "Writing data for #{table} ..."
benchmark do
puts `mysql #{db_connection_options} --skip-column-names --batch -e "select * from #{table}" | sed -e 's/\tNULL/\t\\\\N/g'| lzop -6 > #{filename(table)[:compressed_data_file]} 2>/var/log/mysql_truck_dump.log`
if $?.exitstatus != 0
puts "Command did not execute successfully"
end
end
end
if split_schema_file?(table)
schema_contents = filename(table)[:schema_file].read
File.open(filename(table)[:no_index_schema_file], 'w') do |f|
f.write(schema_contents.gsub(REGEX, ''))
end
indices = []
File.open(filename(table)[:alter_table_file], 'w') do |f|
f.write("ALTER TABLE #{table}\n")
schema_contents.gsub(/^,?\s*((UNIQUE)?\s*KEY\s`[A-Za-z\d_]+`\s*\([A-Za-z\d_,`]+\)),?\s*$/) do |part|
indices << $1
end
f.write(indices.collect {|i| "ADD #{i}"}.join(",\n"))
end
end
if compress_files?(table)
puts "compressing #{filename(table)[:no_index_schema_file]}."
benchmark do
compression_cmd = "lzop -8 -U"
`#{compression_cmd} #{filename(table)[:no_index_schema_file]}`
puts "compressing #{filename(table)[:alter_table_file]}."
`#{compression_cmd} #{filename(table)[:alter_table_file]}`
puts "compressing #{filename(table)[:data_file]}."
`#{compression_cmd} #{filename(table)[:data_file]}`
end
end
puts "#{table} dumped.\n\n"
end
end
|
#upload ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/mysql_truck/dumper.rb', line 98
def upload
Dir["#{tmp_path}/*"].each do |file|
next if File.extname(file) != ".lzo"
puts "Uploading #{file} ..."
upload_file file
end
puts "Finished uploading backups."
end
|