Class: Rubber::Commands::RestoreDb

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/rubber/commands/util.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.subcommand_descriptionObject



246
247
248
# File 'lib/rubber/commands/util.rb', line 246

def self.subcommand_description
  "Performs a restore of the database"
end

.subcommand_nameObject



242
243
244
# File 'lib/rubber/commands/util.rb', line 242

def self.subcommand_name
  "util:restore_db"
end

Instance Method Details

#executeObject



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
# File 'lib/rubber/commands/util.rb', line 266

def execute
  signal_usage_error "DBUSER, DBHOST are required" unless dbuser && dbhost

  # extra variables for command interpolation
  file = filename
  user = dbuser
  pass = dbpass
  pass = nil if pass && pass.strip.size == 0
  host = dbhost
  name = dbname

  raise "No db_restore_cmd defined in rubber.yml" unless Rubber.config.db_restore_cmd
  db_restore_cmd = Rubber.config.db_restore_cmd.gsub(/%([^%]+)%/, '#{\1}')
  db_restore_cmd = eval('%Q{' + db_restore_cmd + '}')

  # try to fetch a matching file from the cloud (if backup_bucket given)
  backup_bucket = Rubber.cloud.env.backup_bucket
  raise "No backup_bucket defined in rubber.yml" unless backup_bucket
  
  key = nil
  cloud_prefix = "db/"
  if filename
    key = "#{cloud_prefix}#{filename}"
  else
    puts "trying to fetch last modified cloud backup"
    max = nil
    Rubber.cloud.storage(backup_bucket).walk_tree(cloud_prefix) do |f|
      if f.key =~ /#{Rubber.env}_dump_/
        max = f if max.nil? || f.last_modified > max.last_modified
      end
    end
    key = max.key if max
  end
  
  raise "could not access backup file from cloud" unless key

  puts "piping restore data from #{backup_bucket}:#{key} to command [#{db_restore_cmd}]"
  
  IO.popen(db_restore_cmd, 'wb') do |p|
    Rubber.cloud.storage(backup_bucket).fetch(key) do |chunk|
      p.write chunk
    end
  end

end