Class: Dumpr::Driver::Base

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

Overview

abstract interface for all drivers

Direct Known Subclasses

Mysql, Postgres

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Base

Returns a new instance of Base.



37
38
39
# File 'lib/dumpr/driver.rb', line 37

def initialize(opts)
  self.configure(opts)
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



33
34
35
# File 'lib/dumpr/driver.rb', line 33

def database
  @database
end

#destinationObject (readonly)

Returns the value of attribute destination.



35
36
37
# File 'lib/dumpr/driver.rb', line 35

def destination
  @destination
end

#destination_dumpfileObject (readonly)

Returns the value of attribute destination_dumpfile.



35
36
37
# File 'lib/dumpr/driver.rb', line 35

def destination_dumpfile
  @destination_dumpfile
end

#destination_hostObject (readonly)

Returns the value of attribute destination_host.



35
36
37
# File 'lib/dumpr/driver.rb', line 35

def destination_host
  @destination_host
end

#dump_optionsObject (readonly)

Returns the value of attribute dump_options.



34
35
36
# File 'lib/dumpr/driver.rb', line 34

def dump_options
  @dump_options
end

#dumpdirObject (readonly)

Returns the value of attribute dumpdir.



34
35
36
# File 'lib/dumpr/driver.rb', line 34

def dumpdir
  @dumpdir
end

#dumpfileObject (readonly)

Returns the value of attribute dumpfile.



34
35
36
# File 'lib/dumpr/driver.rb', line 34

def dumpfile
  @dumpfile
end

#gzipObject (readonly)

Returns the value of attribute gzip.



34
35
36
# File 'lib/dumpr/driver.rb', line 34

def gzip
  @gzip
end

#gzip_optionsObject (readonly)

Returns the value of attribute gzip_options.



34
35
36
# File 'lib/dumpr/driver.rb', line 34

def gzip_options
  @gzip_options
end

#hostObject (readonly)

Returns the value of attribute host.



33
34
35
# File 'lib/dumpr/driver.rb', line 33

def host
  @host
end

#import_optionsObject (readonly)

Returns the value of attribute import_options.



34
35
36
# File 'lib/dumpr/driver.rb', line 34

def import_options
  @import_options
end

#optsObject (readonly)

Returns the value of attribute opts.



32
33
34
# File 'lib/dumpr/driver.rb', line 32

def opts
  @opts
end

#passwordObject (readonly)

Returns the value of attribute password.



33
34
35
# File 'lib/dumpr/driver.rb', line 33

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



33
34
35
# File 'lib/dumpr/driver.rb', line 33

def port
  @port
end

#tablesObject (readonly)

Returns the value of attribute tables.



33
34
35
# File 'lib/dumpr/driver.rb', line 33

def tables
  @tables
end

#userObject (readonly)

Returns the value of attribute user.



33
34
35
# File 'lib/dumpr/driver.rb', line 33

def user
  @user
end

Instance Method Details

#configure(opts) ⇒ Object



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
# File 'lib/dumpr/driver.rb', line 41

def configure(opts)
  opts = (@opts||{}).merge(opts)
  # db connection settings
  @host = opts[:host] || "localhost"
  @port = opts[:port]
  @user = opts[:user] or raise BadConfig.new "user is required"
  @password = (opts[:password]  || opts[:pass]) # or raise BadConfig.new "password is required"

  # dump all_databases or specific database(s)
  @all_databases = nil
  @database = nil
  @databases = nil
  @tables = nil
  if (opts[:database] || opts[:db])
    @database = (opts[:database] || opts[:db])
    @tables = [opts[:table], opts[:tables]].flatten.uniq.compact
  elsif opts[:databases]
    @databases = [opts[:databases]].flatten.uniq.compact # not used/supported yet
  elsif opts[:all_databases]
    @all_databases = true
  else
    #raise BadConfig.new "database is required"
  end

  # dump settings
  @gzip = opts[:gzip].nil? ? true : opts[:gzip]
  @gzip_options = opts[:gzip_options] || "-9"
  @dumpdir = opts[:dumpdir] || Dir.pwd #"./"
  @dumpfile = (opts[:file] || opts[:dumpfile] || opts[:filename]) or raise BadConfig.new "[file] is required"
  @dumpfile = @dumpfile.to_s.dup # this is frozen?
  @dumpfile = @dumpfile[0].chr == "/" ? @dumpfile : File.join(@dumpdir, @dumpfile)
  @dumpfile.chomp!(".gz")
  # (optional) :destination is where dumps are exported to, and can be a remote host:path
  @destination = opts[:destination] || @dumpfile
  if @destination.include?(":")
    @destination_host, @destination_dumpfile = @destination.split(":")[0], @destination.split(":")[1]
  else
    @destination_host, @destination_dumpfile = "localhost", @destination
  end
  # destination might be a path only, so build the entire filepath
  if File.extname(@destination_dumpfile) == ""
    @destination_dumpfile = File.join(@destination_dumpfile, File.basename(@dumpfile))
  end
  @destination_dumpfile.chomp!(".gz")
  @dump_options = opts[:dump_options]
  @import_options = opts[:import_options]

  # set / update logger
  if opts[:logger]
    @logger = opts[:logger]
  elsif opts[:log_file]
    @logger = Logger.new(opts[:log_file])
  end
  @logger = Logger.new(STDOUT) if !@logger
  @logger.level = opts[:log_level] if opts[:log_level] # expects integer

  @opts = opts
end

#decompressObject



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dumpr/driver.rb', line 185

def decompress
  if File.exists?(@dumpfile + ".gz")
    if File.exists?(@dumpfile) && !@opts[:force]
      logger.warn "skipping decompress because #{@dumpfile} already exists."
    else
      logger.debug "decompressing..."
      run "gzip -d -f #{@dumpfile}.gz"
    end
  else
    logger.warn "decompress failed. #{@dumpfile}.gz does not exist!"
  end
end

#dumpObject

creates @dumpfile pipes :dump_cmd to gzip, rather than write the file to disk twice if @destination is defined, it then moves the dump to the @destination, which can be a remote host:path



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
177
# File 'lib/dumpr/driver.rb', line 125

def dump
  logger.debug("begin dump")
  if dump_installed? != true
    raise MissingDriver.new "#{self.class} does not appear to be installed.\nCould not find command `#{dump_cmd.to_s.split.first}`"
  end
  dumpfn = @dumpfile + (@gzip ? ".gz" : "")
  Util.with_lockfile("localhost", dumpfn, @opts[:force]) do

    logger.debug "preparing dump..."
    if !File.exists?(File.dirname(dumpfn))
      run "mkdir -p #{File.dirname(dumpfn)}"
    end

    # avoid overwriting dump files..
    if File.exists?(dumpfn)
      if @opts[:force]
        logger.warn "#{dumpfn} exists, moving it to #{dumpfn}.1"
        #run "rm -f #{dumpfn}.1;"
        run "mv #{dumpfn} #{dumpfn}.1"
      else
        logger.warn "#{dumpfn} already exists!"
        raise DumpFileExists.new "#{dumpfn} already exists!"
      end
    end

    logger.debug "dumping..."
    if @gzip
      run "#{dump_cmd} | gzip #{gzip_options} > #{dumpfn}"
    else
      run "#{dump_cmd} > #{dumpfn}"
    end
    dumpsize = Util.human_file_size("localhost", dumpfn)
    logger.info("generated #{dumpfn} (#{dumpsize})")

    if @destination
      if remote_destination?
        logger.debug "exporting to #{@destination_host}..."
        Util.with_lockfile(@destination_host, @destination_dumpfile, @opts[:force]) do
          run "scp #{dumpfn} #{@destination_host}:#{@destination_dumpfile}#{@gzip ? '.gz' : ''}"
        end
      elsif @destination_dumpfile && @destination_dumpfile+(@gzip ? '.gz' : '') != dumpfn
        logger.debug "exporting..."
        destdir = File.dirname(@destination_dumpfile)
        run "mkdir -p #{destdir}" if !Util.dir_exists?("localhost", destdir)
        Util.with_lockfile("localhost", @destination_dumpfile, @opts[:force]) do
          run "mv #{dumpfn} #{@destination_dumpfile}#{@gzip ? '.gz' : ''}"
        end
      end
    end

  end # with_lockfile
  logger.debug("end dump")
end

#dump_cmdObject



112
113
114
# File 'lib/dumpr/driver.rb', line 112

def dump_cmd
  raise BadConfig.new "#{self.class} has not defined dump_cmd"
end

#dump_installed?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/dumpr/driver.rb', line 104

def dump_installed?
  raise BadConfig.new "#{self.class} has not defined dump_installed?"
end

#importObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/dumpr/driver.rb', line 198

def import
  if import_installed? != true
    raise MissingDriver.new "#{self.class} does not appear to be installed.\nCould not find command `#{import_cmd.to_s.split.first}`"
  end
  Util.with_lockfile("localhost", @dumpfile, @opts[:force]) do
    decompress if @gzip

    if !File.exists?(@dumpfile)
      raise "Cannot import #{@dumpfile} because it does not exist!"
    else
      dumpsize = Util.human_file_size("localhost", @dumpfile)
      logger.info("importing #{@dumpfile} (#{dumpsize})")
      run import_cmd
    end

  end # with_lockfile
end

#import_cmdObject

IMPORTING



181
182
183
# File 'lib/dumpr/driver.rb', line 181

def import_cmd
  raise BadConfig.new "#{self.class} has not defined import_cmd!"
end

#import_installed?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/dumpr/driver.rb', line 108

def import_installed?
  raise BadConfig.new "#{self.class} has not defined import_installed?"
end

#loggerObject



100
101
102
# File 'lib/dumpr/driver.rb', line 100

def logger
  @logger
end

#remote_destination?Boolean

DUMPING + EXPORTING

Returns:

  • (Boolean)


118
119
120
# File 'lib/dumpr/driver.rb', line 118

def remote_destination?
  @destination_host && @destination_host != "localhost"
end