From c58c6bcfa14400f9363c7ec53017c800d66339f6 Mon Sep 17 00:00:00 2001
From: Gabriel Mazetto <gabriel@gitlab.com>
Date: Wed, 11 Dec 2024 17:17:50 +0100
Subject: [PATCH] Improve backup and restore error handling

---
 .../cli/errors/database_restore_error.rb      | 29 +++++++++++++++++++
 .../gitlab/backup/cli/services/database.rb    |  1 -
 .../lib/gitlab/backup/cli/targets/database.rb |  9 ++++--
 3 files changed, 35 insertions(+), 4 deletions(-)
 create mode 100644 gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_restore_error.rb

diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_restore_error.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_restore_error.rb
new file mode 100644
index 000000000000..d62ae8883366
--- /dev/null
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_restore_error.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Gitlab
+  module Backup
+    module Cli
+      module Errors
+        class DatabaseRestoreError < StandardError
+          attr_reader :config, :db_file_name
+
+          def initialize(config, db_file_name)
+            @config = config
+            @db_file_name = db_file_name
+
+            super(build_message)
+          end
+
+          private
+
+          def build_message
+            "Failed to restore from database backup file '#{db_file_name}' \n" \
+              "- host: '#{config[:host]}' \n" \
+              "- port: '#{config[:port]}' \n" \
+              "- database: '#{config[:database]}'"
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb
index 671e5497834a..611dd773ead2 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb
@@ -73,7 +73,6 @@ def release_snapshot!
 
             connection.rollback_transaction
             @snapshot_id = nil
-            restore_timeouts!
           end
 
           def disable_timeouts!
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/database.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/database.rb
index 859b25d019f0..cd4ac9d35881 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/database.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/database.rb
@@ -56,9 +56,10 @@ def dump(destination_dir)
                 Gitlab::Backup::Cli::Output.error "------ END ERRORS -------"
               end
 
-              database.release_snapshot! if database.snapshot_id
-
               raise Errors::DatabaseBackupError.new(database.connection_params, dump_file_name) unless status.success?
+            ensure
+              database.release_snapshot!
+              database.restore_timeouts!
             end
           end
 
@@ -103,7 +104,9 @@ def restore(source)
                 Gitlab::Backup::Cli::Output.error "------ END ERRORS -------"
               end
 
-              raise Gitlab::Backup::Cli::Error, 'Restore failed' unless status.success?
+              unless status.success?
+                raise Gitlab::Backup::Cli::Errors::DatabaseRestoreError.new(database.connection_params, db_file_name)
+              end
             end
           end
 
-- 
GitLab