Skip to content

Commit

Permalink
Add file splitting support to evm_dba tasks
Browse files Browse the repository at this point in the history
Adds the `--byte-count` flag to evm:db:dump:* and evm:db:backup:* rake
tasks which passes the `:byte_count` option to the MiqFileStorage for
handling file splitting.

Works with all classes that conform to the MiqFileStorage interface.
  • Loading branch information
NickLaMuro committed Aug 22, 2018
1 parent 953a224 commit b087e1e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/evm_database_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def self.restore(db_opts, connect_opts = {})
DEFAULT_OPTS.merge(db_opts)
end

STORAGE_ACTIONS_TO_METHODS = { :backup => :upload, :dump => :upload, :restore => :download }.freeze
STORAGE_ACTIONS_TO_METHODS = { :backup => :add, :dump => :add, :restore => :download }.freeze
private_class_method def self.with_file_storage(action, db_opts, connect_opts)
db_opts = merged_db_opts(db_opts)
connect_opts = connect_opts.reverse_merge(:uri => "file://")
Expand All @@ -123,8 +123,8 @@ def self.restore(db_opts, connect_opts = {})
end

MiqFileStorage.with_interface_class(connect_opts) do |file_storage|
file_storage.connect
file_storage.send(STORAGE_ACTIONS_TO_METHODS[action], uri) do |input_path|
send_args = [uri, db_opts[:byte_count]].compact
file_storage.send(STORAGE_ACTIONS_TO_METHODS[action], *send_args) do |input_path|
db_opts[:local_file] = input_path
yield(db_opts)
end
Expand Down
12 changes: 7 additions & 5 deletions lib/tasks/evm_dba.rake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module EvmDba
opt :local_file, "Destination file", :type => :string, :required => true
when :remote_file
opt :remote_file_name, "Destination depot filename", :type => :string
when :splitable
opt :byte_count, "Size to split files into", :type => :string
when :remote_uri
opt :uri, "Destination depot URI", :type => :string, :required => true
opt :uri_username, "Destination depot username", :type => :string
Expand All @@ -48,7 +50,7 @@ module EvmDba
end.delete_nils
end

DB_OPT_KEYS = [:dbname, :username, :password, :hostname, :exclude_table_data].freeze
DB_OPT_KEYS = [:dbname, :username, :password, :hostname, :exclude_table_data, :byte_count].freeze
def self.collect_db_opts(opts)
db_opts = {}
DB_OPT_KEYS.each { |k| db_opts[k] = opts[k] if opts[k] }
Expand Down Expand Up @@ -189,7 +191,7 @@ namespace :evm do
require File.expand_path(File.join(Rails.root, "lib", "evm_database_ops"))
desc 'Backup the local ManageIQ EVM Database (VMDB) to a local file'
task :local do
opts = EvmDba.with_options(:local_file, :db_credentials)
opts = EvmDba.with_options(:local_file, :splitable, :db_credentials)

EvmDatabaseOps.backup(opts)

Expand All @@ -198,7 +200,7 @@ namespace :evm do

desc 'Backup the local ManageIQ EVM Database (VMDB) to a remote file'
task :remote do
opts = EvmDba.with_options(:remote_uri, :aws, :remote_file, :db_credentials)
opts = EvmDba.with_options(:remote_uri, :aws, :remote_file, :splitable, :db_credentials)

db_opts = EvmDba.collect_db_opts(opts)
connect_opts = EvmDba.collect_connect_opts(opts)
Expand All @@ -213,7 +215,7 @@ namespace :evm do
require Rails.root.join("lib", "evm_database_ops").expand_path.to_s
desc 'Dump the local ManageIQ EVM Database (VMDB) to a local file'
task :local do
opts = EvmDba.with_options(:local_file, :db_credentials, :exclude_table_data)
opts = EvmDba.with_options(:local_file, :splitable, :db_credentials, :exclude_table_data)

EvmDatabaseOps.dump(opts)

Expand All @@ -222,7 +224,7 @@ namespace :evm do

desc 'Dump the local ManageIQ EVM Database (VMDB) to a remote file'
task :remote do
opts = EvmDba.with_options(:remote_uri, :remote_file, :db_credentials, :exclude_table_data)
opts = EvmDba.with_options(:remote_uri, :remote_file, :splitable, :db_credentials, :exclude_table_data)

db_opts = EvmDba.collect_db_opts(opts)
connect_opts = EvmDba.collect_connect_opts(opts)
Expand Down
25 changes: 20 additions & 5 deletions spec/lib/evm_database_ops_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'util/runcmd'
describe EvmDatabaseOps do
let(:file_storage) { double("MiqSmbSession", :disconnect => nil) }
let(:local_backup) { "/tmp/backup_1" }
let(:input_path) { "foo/bar/mkfifo" }
let(:run_db_ops) { @db_opts.dup.merge(:local_file => input_path) }
let(:tmpdir) { Rails.root.join("tmp") }
Expand All @@ -23,19 +24,26 @@
end

it "locally" do
local_backup = "/tmp/backup_1"
@db_opts[:local_file] = local_backup
expect(PostgresAdmin).to receive(:backup).with(run_db_ops)
expect(EvmDatabaseOps.backup(@db_opts, @connect_opts)).to eq(local_backup)
end

it "defaults" do
local_backup = "/tmp/backup_1"
@db_opts[:local_file] = local_backup
expect(PostgresAdmin).to receive(:backup).with(run_db_ops)
expect(EvmDatabaseOps.backup(@db_opts, {})).to eq(local_backup)
end

it "splits files with a local file" do
@db_opts[:local_file] = local_backup
@db_opts[:byte_count] = "200M"

allow(file_storage).to receive(:send).with(:add, local_backup, "200M").and_yield(input_path)
expect(PostgresAdmin).to receive(:backup).with(run_db_ops)
EvmDatabaseOps.backup(@db_opts, {})
end

it "without enough free space" do
EvmSpecHelper.create_guid_miq_server_zone
allow(EvmDatabaseOps).to receive(:backup_destination_free_space).and_return(100.megabytes)
Expand Down Expand Up @@ -76,6 +84,7 @@
end

context "#dump" do
let(:local_dump) { "/tmp/dump_1" }
before do
@connect_opts = {:username => 'blah', :password => 'blahblah', :uri => "smb://myserver.com/share"}
@db_opts = {:dbname => 'vmdb_production', :username => 'root'}
Expand All @@ -91,19 +100,26 @@
end

it "locally" do
local_dump = "/tmp/dump_1"
@db_opts[:local_file] = local_dump
expect(PostgresAdmin).to receive(:backup_pg_dump).with(run_db_ops)
expect(EvmDatabaseOps.dump(@db_opts, @connect_opts)).to eq(local_dump)
end

it "defaults" do
local_dump = "/tmp/dump_1"
@db_opts[:local_file] = local_dump
expect(PostgresAdmin).to receive(:backup_pg_dump).with(run_db_ops)
expect(EvmDatabaseOps.dump(@db_opts, {})).to eq(local_dump)
end

it "splits files with a local file" do
@db_opts[:local_file] = local_dump
@db_opts[:byte_count] = "200M"

allow(file_storage).to receive(:send).with(:add, local_dump, "200M").and_yield(input_path)
expect(PostgresAdmin).to receive(:backup_pg_dump).with(run_db_ops)
EvmDatabaseOps.dump(@db_opts, {})
end

it "without enough free space" do
EvmSpecHelper.create_guid_miq_server_zone
allow(EvmDatabaseOps).to receive(:backup_destination_free_space).and_return(100.megabytes)
Expand Down Expand Up @@ -146,7 +162,6 @@
end

it "from local backup" do
local_backup = "/tmp/backup_1"
@db_opts[:local_file] = local_backup
expect(EvmDatabaseOps.restore(@db_opts, @connect_opts)).to eq(local_backup)
end
Expand Down

0 comments on commit b087e1e

Please sign in to comment.