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 Sep 17, 2018
1 parent c0feb74 commit f12f99c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/evm_database_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def self.restore(db_opts, connect_opts = {})
end

MiqFileStorage.with_interface_class(connect_opts) do |file_storage|
send_args = [uri, db_opts[:byte_count]].compact

# Note: `input_path` will always be a fifo stream (input coming from
# PostgresAdmin, and the output going to the `uri`), since we want to
Expand All @@ -143,7 +144,7 @@ def self.restore(db_opts, connect_opts = {})
# to whatever endpoint it might be the resulting `uri`.
#
# This is why we set `db_opts` local file to that stream.
file_storage.send(STORAGE_ACTIONS_TO_METHODS[action], uri) do |input_path|
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, :aws, :remote_file, :db_credentials, :exclude_table_data)
opts = EvmDba.with_options(:remote_uri, :aws, :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 f12f99c

Please sign in to comment.