diff options
Diffstat (limited to 'lib/backup')
-rw-r--r-- | lib/backup/database.rb | 20 | ||||
-rw-r--r-- | lib/backup/gitaly_backup.rb | 13 | ||||
-rw-r--r-- | lib/backup/manager.rb | 44 |
3 files changed, 51 insertions, 26 deletions
diff --git a/lib/backup/database.rb b/lib/backup/database.rb index 3cbe3cf7d88..cf19b4fa8ff 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -18,9 +18,10 @@ module Backup ].freeze IGNORED_ERRORS_REGEXP = Regexp.union(IGNORED_ERRORS).freeze - def initialize(progress, force:) + def initialize(database_name, progress, force:) super(progress) - @config = ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash + @database_name = database_name + @config = base_model.connection_db_config.configuration_hash @force = force end @@ -67,6 +68,13 @@ module Backup override :restore def restore(db_file_name) + unless File.exist?(db_file_name) + raise(Backup::Error, "Source database file does not exist #{db_file_name}") if main_database? + + progress.puts "Source backup for the database #{@database_name} doesn't exist. Skipping the task" + return + end + unless force progress.puts 'Removing all tables. Press `Ctrl-C` within 5 seconds to abort'.color(:yellow) sleep(5) @@ -140,6 +148,14 @@ module Backup @config[:database] end + def base_model + Gitlab::Database.database_base_models[@database_name] + end + + def main_database? + @database_name == :main + end + def ignore_error?(line) IGNORED_ERRORS_REGEXP.match?(line) end diff --git a/lib/backup/gitaly_backup.rb b/lib/backup/gitaly_backup.rb index a995f308c2b..57dd74c7950 100644 --- a/lib/backup/gitaly_backup.rb +++ b/lib/backup/gitaly_backup.rb @@ -32,15 +32,12 @@ module Backup raise Error, "unknown backup type: #{type}" end - args = [] + args = ['-layout', 'pointer'] args += ['-parallel', @max_parallelism.to_s] if @max_parallelism args += ['-parallel-storage', @storage_parallelism.to_s] if @storage_parallelism - if Feature.enabled?(:incremental_repository_backup) - args += ['-layout', 'pointer'] - if type == :create - args += ['-incremental'] if incremental? - args += ['-id', backup_id] if backup_id - end + if type == :create + args += ['-incremental'] if incremental? + args += ['-id', backup_id] if backup_id end @input_stream, stdout, @thread = Open3.popen2(build_env, bin_path, command, '-path', backup_repos_path, *args) @@ -97,7 +94,7 @@ module Backup def build_env { 'SSL_CERT_FILE' => Gitlab::X509::Certificate.default_cert_file, - 'SSL_CERT_DIR' => Gitlab::X509::Certificate.default_cert_dir + 'SSL_CERT_DIR' => Gitlab::X509::Certificate.default_cert_dir }.merge(ENV) end diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb index 16b8f21c9e9..902eb8f6659 100644 --- a/lib/backup/manager.rb +++ b/lib/backup/manager.rb @@ -22,6 +22,7 @@ module Backup :destination_optional, # `true` if the destination might not exist on a successful backup. :cleanup_path, # Path to remove after a successful backup. Uses `destination_path` when not specified. :task, + :task_group, keyword_init: true ) do def enabled? @@ -33,11 +34,7 @@ module Backup def initialize(progress, definitions: nil) @progress = progress - - @incremental = Feature.feature_flags_available? && - Feature.enabled?(:incremental_repository_backup) && - Gitlab::Utils.to_boolean(ENV['INCREMENTAL'], default: false) - + @incremental = Gitlab::Utils.to_boolean(ENV['INCREMENTAL'], default: false) @definitions = definitions end @@ -122,13 +119,22 @@ module Backup @definitions ||= build_definitions end - def build_definitions + def build_definitions # rubocop:disable Metrics/AbcSize { - 'db' => TaskDefinition.new( - human_name: _('database'), + 'main_db' => TaskDefinition.new( + human_name: _('main_database'), destination_path: 'db/database.sql.gz', cleanup_path: 'db', - task: build_db_task + task: build_db_task(:main), + task_group: 'db' + ), + 'ci_db' => TaskDefinition.new( + human_name: _('ci_database'), + destination_path: 'db/ci_database.sql.gz', + cleanup_path: 'db', + task: build_db_task(:ci), + enabled: Gitlab::Database.has_config?(:ci), + task_group: 'db' ), 'repositories' => TaskDefinition.new( human_name: _('repositories'), @@ -180,10 +186,11 @@ module Backup }.freeze end - def build_db_task - force = Gitlab::Utils.to_boolean(ENV['force'], default: false) + def build_db_task(database_name) + return unless Gitlab::Database.has_config?(database_name) # It will be disabled for a single db setup - Database.new(progress, force: force) + force = Gitlab::Utils.to_boolean(ENV['force'], default: false) + Database.new(database_name, progress, force: force) end def build_repositories_task @@ -233,7 +240,9 @@ module Backup verify_backup_version definitions.keys.each do |task_name| - run_restore_task(task_name) if !skipped?(task_name) && enabled_task?(task_name) + if !skipped?(task_name) && enabled_task?(task_name) + run_restore_task(task_name) + end end Rake::Task['gitlab:shell:setup'].invoke @@ -254,7 +263,9 @@ module Backup def write_backup_information # Make sure there is a connection - ActiveRecord::Base.connection.reconnect! + ::Gitlab::Database.database_base_models.values.each do |base_model| + base_model.connection.reconnect! + end Dir.chdir(backup_path) do File.open("#{backup_path}/#{MANIFEST_NAME}", "w+") do |file| @@ -472,7 +483,7 @@ module Backup end def skipped?(item) - skipped.include?(item) + skipped.include?(item) || skipped.include?(definitions[item]&.task_group) end def skipped @@ -483,6 +494,7 @@ module Backup list = ENV.fetch(LIST_ENVS[name], '').split(',') list += backup_information[name].split(',') if backup_information[name] list.uniq! + list.compact! list end @@ -507,7 +519,7 @@ module Backup end def available_timestamps - @backup_file_list.map {|item| item.gsub("#{FILE_NAME_SUFFIX}", "")} + @backup_file_list.map { |item| item.gsub("#{FILE_NAME_SUFFIX}", "") } end def object_storage_config |