diff --git a/lib/evm_database_ops.rb b/lib/evm_database_ops.rb index 3eaaa7e02e6..45f13c52a2e 100644 --- a/lib/evm_database_ops.rb +++ b/lib/evm_database_ops.rb @@ -53,11 +53,9 @@ def self.backup(db_opts, connect_opts = {}) # :password => 'Zug-drep5s', # :remote_file_name => "backup_1", - Provide a base file name for the uploaded file - uri = with_mount_session(:backup, db_opts, connect_opts) do |database_opts, session, remote_file_uri| + uri = with_mount_session(:backup, db_opts, connect_opts) do |database_opts| validate_free_space(database_opts) - backup_result = PostgresAdmin.backup(database_opts) - session&.add(database_opts[:local_file], remote_file_uri) - backup_result + PostgresAdmin.backup(database_opts) end _log.info("[#{merged_db_opts(db_opts)[:dbname]}] database has been backed up to file: [#{uri}]") uri @@ -66,7 +64,7 @@ def self.backup(db_opts, connect_opts = {}) def self.dump(db_opts, connect_opts = {}) # db_opts and connect_opts similar to .backup - uri = with_mount_session(:dump, db_opts, connect_opts) do |database_opts, _session, _remote_file_uri| + uri = with_mount_session(:dump, db_opts, connect_opts) do |database_opts| # For database dumps, this isn't going to be as accurate (since the dump # size will probably be larger than the calculated BD size), but it still # won't hurt to do as a generic way to get a rough idea if we have enough @@ -89,10 +87,7 @@ def self.restore(db_opts, connect_opts = {}) # :username => 'samba_one', # :password => 'Zug-drep5s', - uri = with_mount_session(:restore, db_opts, connect_opts) do |database_opts, session, remote_file_uri| - if session && !File.exist?(database_opts[:local_file]) - database_opts[:local_file] = session.download(database_opts[:local_file], remote_file_uri) - end + uri = with_mount_session(:restore, db_opts, connect_opts) do |database_opts| prepare_for_restore(database_opts[:local_file]) # remove all the connections before we restore; AR will reconnect on the next query @@ -124,12 +119,27 @@ def self.restore(db_opts, connect_opts = {}) db_opts[:local_file] = session.uri_to_local_path(uri) end - block_result = yield(db_opts, session, uri) if block_given? + download_from_mount_if_needed(action, session, uri, db_opts) + block_result = yield(db_opts) if block_given? + upload_to_mount_if_needed(action, session, uri, db_opts) uri || block_result ensure session.disconnect if session end + private_class_method def self.download_from_mount_if_needed(action, session, uri, db_opts) + if action == :restore && session.kind_of?(MiqS3Session) && !File.exist?(db_opts[:local_file]) + db_opts[:local_file] = session.download(db_opts[:local_file], uri) + end + end + + private_class_method def self.upload_to_mount_if_needed(action, session, uri, db_opts) + if action == :backup && session.kind_of?(MiqS3Session) + session.add(db_opts[:local_file], uri) + FileUtils.rm_rf db_opts[:local_file] if false # TODO: consider doing this + end + end + private_class_method def self.prepare_for_restore(filename) backup_type = validate_backup_file_type(filename) diff --git a/spec/lib/evm_database_ops_spec.rb b/spec/lib/evm_database_ops_spec.rb index a121e1f920d..c2c53778b30 100644 --- a/spec/lib/evm_database_ops_spec.rb +++ b/spec/lib/evm_database_ops_spec.rb @@ -37,14 +37,12 @@ it "remotely" do @db_opts[:local_file] = nil @connect_opts[:remote_file_name] = "custom_backup" - expect(session).to receive(:add).and_return("smb://myserver.com/share/db_backup/custom_backup") expect(EvmDatabaseOps.backup(@db_opts, @connect_opts)).to eq("smb://myserver.com/share/db_backup/custom_backup") end it "remotely without a remote file name" do @db_opts[:local_file] = nil @connect_opts[:remote_file_name] = nil - expect(session).to receive(:add) expect(EvmDatabaseOps.backup(@db_opts, @connect_opts)).to match(/smb:\/\/myserver.com\/share\/db_backup\/miq_backup_.*/) end @@ -58,7 +56,6 @@ expect(described_class).to receive(:_log).twice.and_return(log_stub) expect(log_stub).to receive(:info).with(any_args) expect(log_stub).to receive(:info).with("[vmdb_production] database has been backed up to file: [smb://myserver.com/share/db_backup/miq_backup]") - expect(session).to receive(:add).and_return("smb://myserver.com/share/db_backup/miq_backup") EvmDatabaseOps.backup(@db_opts, @connect_opts) end