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

.execute(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
# File 'lib/dumpr/cli.rb', line 10

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


  op = OptionParser.new do |opts|

    opts.banner = <<-ENDSTR
Usage: #{PROG_NAME} [options]

Exporting:

  #{PROG_NAME} --user youruser --password yourpass --db yourdb --dumpfile yourdb.sql --destination server2:/data/backups

Importing:

  #{PROG_NAME} -i --user youruser --password yourpass --db yourdb --dumpfile /data/backups/yourdb.sql

Don't forget to set up your .ssh/config so you won't be prompted for ssh passwords for file transfers

Options:

ENDSTR

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

    opts.on("-i", "--import", "Import dump file.  Default behavior is to dump and export to --destination") do |val|
      options[:import] = val
    end

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

    opts.on("--db DATABASE", "--database DATABASE", "Database to dump/import") do |val|
      options[:database] = val
    end

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

    opts.on("--tables [t1,t2,t3]", 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("--dumpfile [DUMPFILE]", "Filename of dump to create/import") 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", "Default directory for dumpfiles. Default is working directory") do |val|
      options[:dumpdir] = val
    end

    opts.on("--dump-options=[DUMPOPTIONS]", "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
     exit
    end

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

  end

  begin
    op.parse!(args)
  rescue OptionParser::MissingArgument => e
    puts "invalid arguments.  try #{PROG_NAME} --help"
    exit 1
  end


  # do it
  begin
    if options[:import]
      Dumpr.import(options[:driver], options)
    else
      Dumpr.export(options[:driver], options)
    end
  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 trash 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