Class: Dumpr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/dumpr/cli.rb

Constant Summary collapse

PROG_NAME =
File.basename($0)

Class Method Summary collapse

Class Method Details

.dump(args) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dumpr/cli.rb', line 10

def self.dump(args)
  # default options
  options = {}
  options[:dumpdir] = Dir.pwd
  options[:driver] = :mysql
  options[:gzip] = true


  op = OptionParser.new do |opts|

    opts.banner = <<-ENDSTR
#{PROG_NAME} #{Dumpr::Version}

usage: #{PROG_NAME} [options] [file]

   #{PROG_NAME} --user test --db test_example test_example_dump.sql

Create a database dump file, exporting database(s) to a file.
The default database type is MySQL. Supports MySQL and Postgres.

arguments:

[file] File is the filename of the database dump being created.
       It may be relative, 'mydumpfile.sql' to write to your working directory.
       It may be absolute, 'server:/path/to/dumpfile.sql'.
       If server: is specified, ssh/scp is used to transfer data to the remote server.
       You will want to setup .ssh/config to avoid being prompted for passwords.
       The .gz file extension is assumed.
       By default it will first look for a compressed version at [file].gz.
       The --no-gzip option can be used to skip compression.

options:

ENDSTR

    opts.on("-t", "--type TYPE", "Database type: mysql or postgres. Default is mysql.") do |val|
      options[:driver] = val
    end

    opts.on("--db DATABASE", "--database DATABASE", "Dump a single database") do |val|
      options[:database] = val
    end

    # TODO: Add support to Driver for this
    opts.on("--databases x,y,z", Array, "Dump multiple databases") do |val|
      options[:databases] = val
    end

    opts.on("--all-databases", "Dump ALL databases") do |val|
      options[:all_databases] = val
    end

    opts.on("--tables z,y,z", Array, "Dump certain tables, to be used on conjuction with a single --database") do |val|
      options[:tables] = val
    end

    opts.on("-u USER", "--user USER", "Database user") do |val|
      options[:user] = val
    end

    opts.on("-p PASS", "--password PASS", "--password=pass", "Database password") do |val|
      options[:password] = val
    end

    opts.on("-h HOST", "--host HOST", "Database host") do |val|
      options[:host] = val
    end

    opts.on("-P PORT", "--port PORT", "Database port") do |val|
      options[:port] = val
    end

    opts.on("-f FILENAME", "--file FILENAME", "Filename of dump to create, may passed in place of the [file] argument.") do |val|
      options[:dumpfile] = val
    end

    # could get rid of all these and just rely on full filepath being passed.

    # opts.on("--dumpfile FILENAME", "Alias for --file") do |val|
    #   options[:dumpfile] = val
    # end

    # opts.on("--destination DESTINATION", "Destination for dumpfile. This can be a remote host:path.") do |val|
    #   options[:destination] = val
    # end

    # opts.on("--dumpdir DIRECTORY", "Default directory for dumpfiles. Default is working directory") do |val|
    #   options[:dumpdir] = val
    # end

    opts.on("--dump-options=[OPTIONS]", "Extra options to be included in dump command") do |val|
      options[:dump_options] = val
    end

    opts.on("--no-gzip", "Don't use gzip") do |val|
      options[:gzip] = false
    end

    opts.on("--gzip-options=[GZIPOPTIONS]", "gzip compression options.  Default is -9 (slowest /max compression)") do |val|
      options[:gzip_options] = val
    end

    opts.on("--log-file LOGFILE", "Log file.  Default is stdout.") do |val|
      options[:log_file] = val
    end

    opts.on("--force", "Overwrite dumpfile if it exists already.") do |val|
      options[:force] = val
    end

    opts.on("-h", "--help", "Show this message") do
     puts opts
     print "\n"
     exit
    end

    opts.on("-v", "--version", "Show version") do
      puts Dumpr::Version
      exit
    end

  end

  begin
    op.parse!(args)
    if args.count == 0 && options[:dumpfile].nil?
      raise OptionParser::InvalidOption.new("[file] or --file is required.")
    elsif args.count == 1
      options[:dumpfile] = args[0]
    else
      raise OptionParser::NeedlessArgument.new("wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.join(', ')}")
    end
  rescue => e
    case (e)
    when OptionParser::InvalidOption, OptionParser::AmbiguousOption, OptionParser::MissingArgument, OptionParser::InvalidArgument, OptionParser::NeedlessArgument
      STDERR.puts "#{e.message}"
      STDERR.puts  "Try -h for help with this command."
      exit 1
    else
      raise e
    end
  end


  # do it
  begin
    Dumpr.export(options[:driver], options)
  rescue Dumpr::MissingDriver => e
    puts "#{e.message}."
    exit 1
  rescue Dumpr::BadConfig => e
    puts "bad arguments: #{e.message}.\n See --help"
    exit 1
  rescue Dumpr::DumpFileExists => e
    puts "#{e.message}\nIt looks like this dump exists already. You should move it, or use --force to overwrite it"
    exit 1
  rescue Dumpr::BusyDumping => e
    puts "#{e.message}\n See --help"
    exit 1
  rescue Dumpr::CommandFailure => e
    puts e.message
    exit 1
  end

  exit 0

end

.import(args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/dumpr/cli.rb', line 179

def self.import(args)
  # default options
  options = {}
  options[:dumpdir] = Dir.pwd
  options[:driver] = :mysql
  options[:gzip] = true


  op = OptionParser.new do |opts|

    opts.banner = <<-ENDSTR
#{PROG_NAME} #{Dumpr::Version}

usage: 
#{PROG_NAME} [options] [file]

#{PROG_NAME} --user test --db test_example ./test_example_dump.sql

Import a database dump file, restoring data to the specified hosts and database(s).
The default database type is MySQL. Supports MySQL and Postgres.
WARNING: This command will overwrite your database information.
     Be sure you specify the correct host and database name(s)
     and the [file] that contains the data you want in it.

arguments:

[file] File is the path of the database dump file being imported.
       File may be relative, 'mydumpfile.sql.gz' to read from your working directory.
       File may be absolute, 'server:/path/to/dumpfile.sql'.
       If server: is specified, ssh/scp is used to transfer data from the remote server.
       You will want to setup ssh configuration to avoid password prompts.
       The .gz file extension is assumed.
       By default it will first look for a compressed version at [file].gz.
       The --no-gzip option can be used to skip compression.

options:

ENDSTR

    opts.on("-t", "--type TYPE", "Database type: mysql or postgres. Default is mysql.") do |val|
      options[:driver] = val
    end

    opts.on("--db DATABASE", "--database DATABASE", "Import to a single database") do |val|
      options[:database] = val
    end

    # TODO: add support to Driver for --databases and --tables
    #      import probably does not need this right now
    opts.on("--databases x,y,z", Array, "Import multiple databases") do |val|
      options[:databases] = val
    end

    opts.on("--all-databases", "Import ALL databases") do |val|
      options[:all_databases] = val
    end

    # opts.on("--tables x,y,z", Array, "Import only certain tables, to be used on conjuction with a single --database") do |val|
    #   options[:tables] = val
    # end

    opts.on("-u USER", "--user USER", "Database user") do |val|
      options[:user] = val
    end

    opts.on("-p PASS", "--password PASS", "--password=pass", "Database password") do |val|
      options[:password] = val
    end

    opts.on("-h HOST", "--host HOST", "Database host") do |val|
      options[:host] = val
    end

    opts.on("-P PORT", "--port PORT", "Database port") do |val|
      options[:port] = val
    end

    opts.on("-f FILENAME", "--file FILENAME", "Filename of dump to create, may passed in place of the [file] argument.") do |val|
      options[:dumpfile] = val
    end

    # could get rid of all these and just rely on full filepath being passed.

    # opts.on("--dumpfile FILENAME", "Alias for --file") do |val|
    #   options[:dumpfile] = val
    # end

    # opts.on("--destination DESTINATION", "Destination for dump files. This can be a remote host:path.") do |val|
    #   options[:destination] = val
    # end

    # opts.on("--dumpdir DIRECTORY", "Default directory for dump files. Default is working directory") do |val|
    #   options[:dumpdir] = val
    # end

    opts.on("--import-options=[OPTIONS]", "Extra options to be included in import command") do |val|
      options[:import_options] = val.to_s
    end

    opts.on("--no-gzip", "Don't use gzip") do |val|
      options[:gzip] = false
    end

    # opts.on("--gzip-options=[GZIPOPTIONS]", "gzip compression options.  Default is -9 (slowest /max compression)") do |val|
    #   options[:gzip_options] = val
    # end

    opts.on("--log-file LOGFILE", "Log file.  Default is stdout.") do |val|
      options[:log_file] = val
    end

    opts.on("--force", "Overwrite dump file if it exists already.") do |val|
      options[:force] = val
    end

    opts.on("-h", "--help", "Show this message") do
     puts opts
     print "\n"
     exit
    end

    opts.on("-v", "--version", "Show version") do
      puts Dumpr::Version
      exit
    end

  end

  begin
    op.parse!(args)
    if args.count == 0 && options[:dumpfile].nil?
      raise OptionParser::InvalidOption.new("[file] or --file is required.")
    elsif args.count == 1
      options[:dumpfile] = args[0]
    else
      raise OptionParser::NeedlessArgument.new("wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.join(', ')}")
    end
  rescue => e
    case (e)
    when OptionParser::InvalidOption, OptionParser::AmbiguousOption, OptionParser::MissingArgument, OptionParser::InvalidArgument, OptionParser::NeedlessArgument
      STDERR.puts "#{e.message}"
      STDERR.puts  "Try -h for help with this command."
      exit 1
    else
      raise e
    end
  end


  # do it
  begin
    Dumpr.import(options[:driver], options)
  rescue Dumpr::MissingDriver => e
    puts "#{e.message}."
    exit 1
  rescue Dumpr::BadConfig => e
    puts "bad arguments: #{e.message}.\n See --help"
    exit 1
  rescue Dumpr::DumpFileExists => e
    puts "#{e.message}\nIt looks like this dump exists already. You should move it, or use --force to overwrite it"
    exit 1
  rescue Dumpr::BusyDumping => e
    puts "#{e.message}\n See --help"
    exit 1
  rescue Dumpr::CommandFailure => e
    puts e.message
    exit 1
  end

  exit 0

end