mysql_truck 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/mysql_truck +9 -1
- data/lib/mysql_truck/version.rb +1 -1
- data/lib/mysql_truck.rb +36 -23
- metadata +4 -4
data/bin/mysql_truck
CHANGED
@@ -44,6 +44,14 @@ parser = OptionParser.new do |opts|
|
|
44
44
|
options[:s3_secret_access_key] = key
|
45
45
|
end
|
46
46
|
|
47
|
+
opts.on("-b", "--s3-bucket BUCKET", "S3 bucket") do |bucket|
|
48
|
+
options[:bucket] = bucket
|
49
|
+
end
|
50
|
+
|
51
|
+
opts.on("--bucket-dir BUCKET_DIR", "S3 bucket dir [defaults to database name]") do |dir|
|
52
|
+
options[:bucket_dir] = key
|
53
|
+
end
|
54
|
+
|
47
55
|
opts.on("-t", "--skip-tables TABLES",
|
48
56
|
"List of tables to skip separated by commas.") do |tables|
|
49
57
|
options[:skip_tables] = tables.split(",")
|
@@ -69,7 +77,7 @@ begin
|
|
69
77
|
|
70
78
|
missing_opts = []
|
71
79
|
[ :s3_access_key, :s3_secret_access_key,
|
72
|
-
:host, :username, :database
|
80
|
+
:host, :username, :database, :bucket
|
73
81
|
].each do |opt|
|
74
82
|
missing_opts << opt unless options.include?(opt)
|
75
83
|
end
|
data/lib/mysql_truck/version.rb
CHANGED
data/lib/mysql_truck.rb
CHANGED
@@ -33,7 +33,7 @@ module MysqlTruck
|
|
33
33
|
@s3 = RightAws::S3.new(
|
34
34
|
config[:s3_access_key],
|
35
35
|
config[:s3_secret_access_key])
|
36
|
-
@bucket = @s3.bucket(
|
36
|
+
@bucket = @s3.bucket(config[:bucket])
|
37
37
|
end
|
38
38
|
|
39
39
|
def db_connection_options
|
@@ -48,15 +48,32 @@ module MysqlTruck
|
|
48
48
|
" --fields-enclosed-by=\\\" --fields-terminated-by=, "
|
49
49
|
end
|
50
50
|
|
51
|
+
def initialize_directories
|
52
|
+
mkdir_p base_path
|
53
|
+
mkdir_p tmp_path
|
54
|
+
chmod 0777, tmp_path
|
55
|
+
end
|
56
|
+
|
57
|
+
def remove_directories
|
58
|
+
rm_r tmp_path, :force => true
|
59
|
+
end
|
60
|
+
|
51
61
|
def tmp_path
|
52
|
-
unless @
|
53
|
-
|
54
|
-
|
55
|
-
end
|
62
|
+
raise "@time not initialized yet" unless @time
|
63
|
+
base_path.join(@time.strftime("%Y-%m-%d-%H-%M"))
|
64
|
+
end
|
56
65
|
|
57
|
-
|
66
|
+
def base_path
|
67
|
+
if config[:tmp_dir]
|
68
|
+
config[:tmp_dir].is_a?(Pathname) ? config[:tmp_dir] : Pathname.new(config[:tmp_dir])
|
69
|
+
else
|
70
|
+
Pathname.new("/data/mysqltruck")
|
71
|
+
end
|
58
72
|
end
|
59
73
|
|
74
|
+
def bucket_dir
|
75
|
+
"mysql/#{config[:bucket_dir] || config[:database]}/"
|
76
|
+
end
|
60
77
|
end
|
61
78
|
|
62
79
|
class Dumper
|
@@ -65,7 +82,7 @@ module MysqlTruck
|
|
65
82
|
|
66
83
|
def initialize(config)
|
67
84
|
@config = config
|
68
|
-
@time = Time.now
|
85
|
+
@time = Time.now # Sets the directory for dump
|
69
86
|
|
70
87
|
initialize_s3
|
71
88
|
initialize_directories
|
@@ -76,7 +93,7 @@ module MysqlTruck
|
|
76
93
|
upload
|
77
94
|
|
78
95
|
ensure
|
79
|
-
|
96
|
+
remove_directories
|
80
97
|
end
|
81
98
|
|
82
99
|
def dump_data
|
@@ -92,6 +109,7 @@ module MysqlTruck
|
|
92
109
|
puts cmd
|
93
110
|
`#{cmd}`
|
94
111
|
|
112
|
+
# `mysqldump` creates files with .txt extensions, so we rename it.
|
95
113
|
path, file = csv_file.split
|
96
114
|
csv_file = path.join("#{file.basename(".txt")}.csv")
|
97
115
|
mv path.join(file), csv_file
|
@@ -115,9 +133,6 @@ module MysqlTruck
|
|
115
133
|
|
116
134
|
private
|
117
135
|
|
118
|
-
def initialize_directories
|
119
|
-
mkdir_p tmp_path
|
120
|
-
end
|
121
136
|
|
122
137
|
def upload_file(local_file)
|
123
138
|
path = Pathname.new(local_file)
|
@@ -137,7 +152,7 @@ module MysqlTruck
|
|
137
152
|
end
|
138
153
|
|
139
154
|
def bucket_path
|
140
|
-
@bucket_path ||= Pathname.new(
|
155
|
+
@bucket_path ||= Pathname.new(bucket_dir).join(@time.strftime("%Y-%m-%d-%H-%M"))
|
141
156
|
end
|
142
157
|
end # class Dumper
|
143
158
|
|
@@ -158,8 +173,9 @@ module MysqlTruck
|
|
158
173
|
def load_latest
|
159
174
|
prefix = backups.first
|
160
175
|
|
161
|
-
|
162
|
-
|
176
|
+
# Set directory where backup is downloaded to
|
177
|
+
@time = Time.new(*prefix.split("/").last.split("-"))
|
178
|
+
initialize_directories
|
163
179
|
|
164
180
|
puts "Downloading backups ..."
|
165
181
|
@bucket.keys(:prefix => prefix).each do |key|
|
@@ -168,20 +184,20 @@ module MysqlTruck
|
|
168
184
|
next if filename.match(/\.csv\.gz$/) && skip_tables.include?(File.basename(filename, ".csv.gz"))
|
169
185
|
|
170
186
|
print " - Downloading #{filename} ... "
|
171
|
-
File.open(
|
187
|
+
File.open(tmp_path.join(filename), "wb") do |f|
|
172
188
|
f.write key.get
|
173
189
|
end
|
174
190
|
print "complete.\n"
|
175
191
|
|
176
192
|
# gunzip file
|
177
193
|
print " -- Inflating #{filename} ... "
|
178
|
-
`gunzip #{
|
194
|
+
`gunzip #{tmp_path.join(filename)}`
|
179
195
|
print "complete.\n"
|
180
196
|
end
|
181
197
|
|
182
198
|
# Load schemas
|
183
199
|
puts "Loading schema."
|
184
|
-
cmd = "cat #{
|
200
|
+
cmd = "cat #{tmp_path}/*.sql | "
|
185
201
|
cmd += "mysql #{db_connection_options}"
|
186
202
|
puts cmd
|
187
203
|
`#{cmd}`
|
@@ -191,7 +207,7 @@ module MysqlTruck
|
|
191
207
|
puts "Loading data."
|
192
208
|
cmd = "mysqlimport #{db_connection_options}"
|
193
209
|
cmd += csv_options
|
194
|
-
Dir["#{
|
210
|
+
Dir["#{tmp_path}/*.csv"].each do |table|
|
195
211
|
print " - Importing #{File.basename(table, ".csv")} ... "
|
196
212
|
puts cmd + " " + table
|
197
213
|
`#{cmd} #{table}`
|
@@ -200,7 +216,7 @@ module MysqlTruck
|
|
200
216
|
|
201
217
|
puts "Backup loaded."
|
202
218
|
ensure
|
203
|
-
|
219
|
+
# remove_directories
|
204
220
|
end
|
205
221
|
|
206
222
|
# Get a list of backups stored on S3.
|
@@ -215,7 +231,7 @@ module MysqlTruck
|
|
215
231
|
@backups = []
|
216
232
|
# Backups are stored in the mysql/ directory
|
217
233
|
@bucket.s3.interface.incrementally_list_bucket(@bucket.name, {
|
218
|
-
:prefix => "
|
234
|
+
:prefix => "#{bucket_dir}", :delimiter => "/"
|
219
235
|
}) do |item|
|
220
236
|
@backups += item[:common_prefixes]
|
221
237
|
end
|
@@ -232,8 +248,5 @@ module MysqlTruck
|
|
232
248
|
@tables
|
233
249
|
end
|
234
250
|
|
235
|
-
def initialize_directories
|
236
|
-
mkdir_p tmp_dir
|
237
|
-
end
|
238
251
|
end # class Loader
|
239
252
|
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.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,12 +10,12 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-09-
|
13
|
+
date: 2011-09-16 00:00:00.000000000 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: right_aws
|
18
|
-
requirement: &
|
18
|
+
requirement: &2156700300 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,7 +23,7 @@ dependencies:
|
|
23
23
|
version: '0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *2156700300
|
27
27
|
description: Mysql database backup tool. Dumps/Loads to/from S3.
|
28
28
|
email:
|
29
29
|
- peter@paydrotalks.com
|