Class: Dumpr::Driver::Base
- Inherits:
-
Object
- Object
- Dumpr::Driver::Base
- Defined in:
- lib/dumpr/driver.rb
Overview
abstract interface for all drivers
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#destination_dumpfile ⇒ Object
readonly
Returns the value of attribute destination_dumpfile.
-
#destination_host ⇒ Object
readonly
Returns the value of attribute destination_host.
-
#dump_options ⇒ Object
readonly
Returns the value of attribute dump_options.
-
#dumpdir ⇒ Object
readonly
Returns the value of attribute dumpdir.
-
#dumpfile ⇒ Object
readonly
Returns the value of attribute dumpfile.
-
#gzip ⇒ Object
readonly
Returns the value of attribute gzip.
-
#gzip_options ⇒ Object
readonly
Returns the value of attribute gzip_options.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#import_options ⇒ Object
readonly
Returns the value of attribute import_options.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #configure(opts) ⇒ Object
- #decompress ⇒ Object
-
#dump ⇒ Object
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.
- #dump_cmd ⇒ Object
- #dump_installed? ⇒ Boolean
- #import ⇒ Object
-
#import_cmd ⇒ Object
IMPORTING.
- #import_installed? ⇒ Boolean
-
#initialize(opts) ⇒ Base
constructor
A new instance of Base.
- #logger ⇒ Object
-
#remote_destination? ⇒ Boolean
DUMPING + EXPORTING.
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
#database ⇒ Object (readonly)
Returns the value of attribute database.
33 34 35 |
# File 'lib/dumpr/driver.rb', line 33 def database @database end |
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
35 36 37 |
# File 'lib/dumpr/driver.rb', line 35 def destination @destination end |
#destination_dumpfile ⇒ Object (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_host ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute dump_options.
34 35 36 |
# File 'lib/dumpr/driver.rb', line 34 def @dump_options end |
#dumpdir ⇒ Object (readonly)
Returns the value of attribute dumpdir.
34 35 36 |
# File 'lib/dumpr/driver.rb', line 34 def dumpdir @dumpdir end |
#dumpfile ⇒ Object (readonly)
Returns the value of attribute dumpfile.
34 35 36 |
# File 'lib/dumpr/driver.rb', line 34 def dumpfile @dumpfile end |
#gzip ⇒ Object (readonly)
Returns the value of attribute gzip.
34 35 36 |
# File 'lib/dumpr/driver.rb', line 34 def gzip @gzip end |
#gzip_options ⇒ Object (readonly)
Returns the value of attribute gzip_options.
34 35 36 |
# File 'lib/dumpr/driver.rb', line 34 def @gzip_options end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
33 34 35 |
# File 'lib/dumpr/driver.rb', line 33 def host @host end |
#import_options ⇒ Object (readonly)
Returns the value of attribute import_options.
34 35 36 |
# File 'lib/dumpr/driver.rb', line 34 def @import_options end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
32 33 34 |
# File 'lib/dumpr/driver.rb', line 32 def opts @opts end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
33 34 35 |
# File 'lib/dumpr/driver.rb', line 33 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
33 34 35 |
# File 'lib/dumpr/driver.rb', line 33 def port @port end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
33 34 35 |
# File 'lib/dumpr/driver.rb', line 33 def tables @tables end |
#user ⇒ Object (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 |
#decompress ⇒ Object
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 |
#dump ⇒ Object
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 #{} > #{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_cmd ⇒ Object
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
104 105 106 |
# File 'lib/dumpr/driver.rb', line 104 def dump_installed? raise BadConfig.new "#{self.class} has not defined dump_installed?" end |
#import ⇒ Object
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_cmd ⇒ Object
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
108 109 110 |
# File 'lib/dumpr/driver.rb', line 108 def import_installed? raise BadConfig.new "#{self.class} has not defined import_installed?" end |
#logger ⇒ Object
100 101 102 |
# File 'lib/dumpr/driver.rb', line 100 def logger @logger end |
#remote_destination? ⇒ Boolean
DUMPING + EXPORTING
118 119 120 |
# File 'lib/dumpr/driver.rb', line 118 def remote_destination? @destination_host && @destination_host != "localhost" end |