mysql_truck 0.6.2 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mysql_truck.rb CHANGED
@@ -3,6 +3,7 @@ require "mysql"
3
3
  require "csv"
4
4
  require "fileutils"
5
5
  require "pathname"
6
+ require "benchmark"
6
7
 
7
8
  require "mysql_truck/version"
8
9
  require "mysql_truck/helper"
@@ -20,6 +20,18 @@ module MysqlTruck
20
20
  end
21
21
 
22
22
  def dump_data
23
+ mysql = Mysql.init
24
+ mysql.options(Mysql::SET_CHARSET_NAME, "utf8")
25
+ mysql.connect(
26
+ config[:host],
27
+ config[:username],
28
+ config[:password],
29
+ config[:database],
30
+ config[:port]
31
+ )
32
+ # mysql.query_with_result = false # Do not store result in memory
33
+ mysql.query("SET NAMES utf8")
34
+
23
35
  tables.each do |table|
24
36
  puts "Dumping #{table}..."
25
37
  next if compressed_files_exist?(table) && smartly?
@@ -32,26 +44,14 @@ module MysqlTruck
32
44
  schema_cmd += "#{db_connection_options} #{table}"
33
45
  schema_cmd += " > #{filename(table)[:schema_file]}"
34
46
  puts schema_cmd
35
- `#{schema_cmd}`
47
+ benchmark { `#{schema_cmd}` }
36
48
 
37
49
  # Dump data
38
50
  puts "Writing data for #{table} ..."
39
- mysql = Mysql.init
40
- mysql.options(Mysql::SET_CHARSET_NAME, "utf8")
41
- mysql.connect(
42
- config[:host],
43
- config[:username],
44
- config[:password],
45
- config[:database],
46
- config[:port]
47
- )
48
- mysql.query_with_result = false # Do not store result in memory
49
- mysql.query("SET NAMES utf8")
50
- CSV.open(filename(table)[:data_file], 'wb', :col_sep => "\t") do |csv|
51
- mysql.query("SELECT * FROM #{table}")
52
- results = mysql.use_result # Stream the rows
53
- results.each do |row|
54
- csv << row.collect { |col| col && col.match(/\t/) ? " " : col }
51
+ benchmark do
52
+ puts `mysql #{db_connection_options} --skip-column-names --batch -e "select * from #{table}" | lzop -6 > #{filename(table)[:compressed_data_file]} 2>/var/log/mysql_truck_dump.log`
53
+ if $?.exitstatus != 0
54
+ puts "Command did not execute successfully"
55
55
  end
56
56
  end
57
57
  end
@@ -79,14 +79,16 @@ module MysqlTruck
79
79
  if compress_files?(table)
80
80
  puts "compressing #{filename(table)[:no_index_schema_file]}."
81
81
 
82
- compression_cmd = "lzop -8 -U"
83
- `#{compression_cmd} #{filename(table)[:no_index_schema_file]}`
82
+ benchmark do
83
+ compression_cmd = "lzop -8 -U"
84
+ `#{compression_cmd} #{filename(table)[:no_index_schema_file]}`
84
85
 
85
- puts "compressing #{filename(table)[:alter_table_file]}."
86
- `#{compression_cmd} #{filename(table)[:alter_table_file]}`
86
+ puts "compressing #{filename(table)[:alter_table_file]}."
87
+ `#{compression_cmd} #{filename(table)[:alter_table_file]}`
87
88
 
88
- puts "compressing #{filename(table)[:data_file]}."
89
- `#{compression_cmd} #{filename(table)[:data_file]}`
89
+ puts "compressing #{filename(table)[:data_file]}."
90
+ `#{compression_cmd} #{filename(table)[:data_file]}`
91
+ end
90
92
  end
91
93
 
92
94
  puts "#{table} dumped.\n\n"
@@ -64,13 +64,14 @@ module MysqlTruck
64
64
  end
65
65
 
66
66
  def benchmark
67
- Benchmark.realtime do
67
+ time = Benchmark.realtime do
68
68
  yield
69
69
  end
70
+ puts "Ran in #{formatted_time(time)}."
70
71
  end
71
72
 
72
73
  def formatted_time(time)
73
- "#{"%0.5f" % time}ms"
74
+ "#{"%0.5f" % time}s"
74
75
  end
75
76
 
76
77
  def csv_options
@@ -43,8 +43,7 @@ module MysqlTruck
43
43
  else
44
44
  decompress_cmd = "gunzip -f"
45
45
  end
46
- time = benchmark { `#{decompress_cmd} #{tmp_path.join(filename)}` }
47
- print "complete (#{formatted_time time}).\n"
46
+ benchmark { `#{decompress_cmd} #{tmp_path.join(filename)}` }
48
47
  end
49
48
  end
50
49
 
@@ -127,10 +126,9 @@ module MysqlTruck
127
126
  sed2_cmd = config[:date_suffix] ? "sed 's/TABLE #{table}/TABLE #{table}_#{backup_date_str.gsub(/-/, "")}/g'" : nil
128
127
  import_cmd = "mysql #{db_connection_options}"
129
128
 
130
- time = benchmark do
129
+ benchmark do
131
130
  `#{[ cat_cmd, sed_cmd, sed2_cmd, import_cmd].compact.join(' | ')}`
132
131
  end
133
- print "complete (#{formatted_time time}).\n"
134
132
  end
135
133
 
136
134
  def import_csv_file(table, backup_date_str, file_path)
@@ -141,12 +139,10 @@ module MysqlTruck
141
139
  `mv #{old_file_path} #{file_path}`
142
140
  end
143
141
 
144
- time = benchmark do
142
+ benchmark do
145
143
  `mysqlimport --local --compress #{csv_options} #{db_connection_options} #{file_path}`
146
144
  end
147
145
 
148
- print "complete (#{formatted_time time}).\n"
149
-
150
146
  file_path
151
147
  end
152
148
 
@@ -158,12 +154,10 @@ module MysqlTruck
158
154
  `mv #{old_file_path} #{file_path}`
159
155
  end
160
156
 
161
- time = benchmark do
157
+ benchmark do
162
158
  `mysqlimport --local --compress #{db_connection_options} #{file_path}`
163
159
  end
164
160
 
165
- print "complete (#{formatted_time time}).\n"
166
-
167
161
  file_path
168
162
  end
169
163
 
@@ -181,15 +175,13 @@ module MysqlTruck
181
175
  if !smartly? || (smartly? && !unzipped_file.exist?)
182
176
  print " - Downloading... "
183
177
 
184
- time = benchmark do
178
+ benchmark do
185
179
  file.open("wb") do |f|
186
180
  @bucket.s3.interface.get(@bucket.name, key.name) do |chunk|
187
181
  f.write chunk
188
182
  end
189
183
  end
190
184
  end
191
-
192
- puts "complete (#{formatted_time time})."
193
185
  else
194
186
  puts " already downloaded."
195
187
  end
@@ -1,3 +1,3 @@
1
1
  module MysqlTruck
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_truck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-01-28 00:00:00.000000000 Z
14
+ date: 2013-02-04 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: right_aws