2021-06-14 23:10:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Backup
|
|
|
|
# Backup and restores repositories using gitaly-backup
|
2022-01-11 01:10:58 -05:00
|
|
|
#
|
|
|
|
# gitaly-backup can work in parallel and accepts a list of repositories
|
|
|
|
# through input pipe using a specific json format for both backup and restore
|
2021-06-14 23:10:34 -04:00
|
|
|
class GitalyBackup
|
2022-01-11 01:10:58 -05:00
|
|
|
# @param [StringIO] progress IO interface to output progress
|
|
|
|
# @param [Integer] max_parallelism max parallelism when running backups
|
|
|
|
# @param [Integer] storage_parallelism max parallelism per storage (is affected by max_parallelism)
|
2022-03-18 02:08:29 -04:00
|
|
|
def initialize(progress, max_parallelism: nil, storage_parallelism: nil, incremental: false, backup_id: nil)
|
2021-06-14 23:10:34 -04:00
|
|
|
@progress = progress
|
2022-01-11 01:10:58 -05:00
|
|
|
@max_parallelism = max_parallelism
|
|
|
|
@storage_parallelism = storage_parallelism
|
2022-03-18 02:08:29 -04:00
|
|
|
@incremental = incremental
|
2021-06-14 23:10:34 -04:00
|
|
|
end
|
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
def start(type, backup_repos_path, backup_id: nil)
|
2021-06-14 23:10:34 -04:00
|
|
|
raise Error, 'already started' if started?
|
|
|
|
|
|
|
|
command = case type
|
|
|
|
when :create
|
|
|
|
'create'
|
|
|
|
when :restore
|
|
|
|
'restore'
|
|
|
|
else
|
|
|
|
raise Error, "unknown backup type: #{type}"
|
|
|
|
end
|
|
|
|
|
2021-06-29 08:08:48 -04:00
|
|
|
args = []
|
2022-01-11 01:10:58 -05:00
|
|
|
args += ['-parallel', @max_parallelism.to_s] if @max_parallelism
|
|
|
|
args += ['-parallel-storage', @storage_parallelism.to_s] if @storage_parallelism
|
2022-03-18 02:08:29 -04:00
|
|
|
if Feature.enabled?(:incremental_repository_backup, default_enabled: :yaml)
|
|
|
|
args += ['-layout', 'pointer']
|
|
|
|
if type == :create
|
|
|
|
args += ['-incremental'] if @incremental
|
2022-03-31 08:08:17 -04:00
|
|
|
args += ['-id', backup_id] if backup_id
|
2022-03-18 02:08:29 -04:00
|
|
|
end
|
|
|
|
end
|
2021-06-29 08:08:48 -04:00
|
|
|
|
2022-01-11 01:10:58 -05:00
|
|
|
@input_stream, stdout, @thread = Open3.popen2(build_env, bin_path, command, '-path', backup_repos_path, *args)
|
2021-07-20 20:09:07 -04:00
|
|
|
|
|
|
|
@out_reader = Thread.new do
|
|
|
|
IO.copy_stream(stdout, @progress)
|
|
|
|
end
|
2021-06-14 23:10:34 -04:00
|
|
|
end
|
|
|
|
|
2022-01-11 01:10:58 -05:00
|
|
|
def finish!
|
2021-06-14 23:10:34 -04:00
|
|
|
return unless started?
|
|
|
|
|
2022-01-11 01:10:58 -05:00
|
|
|
@input_stream.close
|
2021-07-20 20:09:07 -04:00
|
|
|
[@thread, @out_reader].each(&:join)
|
|
|
|
status = @thread.value
|
2021-06-14 23:10:34 -04:00
|
|
|
|
2021-07-20 20:09:07 -04:00
|
|
|
@thread = nil
|
2021-06-14 23:10:34 -04:00
|
|
|
|
|
|
|
raise Error, "gitaly-backup exit status #{status.exitstatus}" if status.exitstatus != 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue(container, repo_type)
|
|
|
|
raise Error, 'not started' unless started?
|
|
|
|
|
|
|
|
repository = repo_type.repository_for(container)
|
|
|
|
|
2022-01-11 01:10:58 -05:00
|
|
|
schedule_backup_job(repository, always_create: repo_type.project?)
|
2021-06-14 23:10:34 -04:00
|
|
|
end
|
|
|
|
|
2021-06-29 08:08:48 -04:00
|
|
|
private
|
2021-06-14 23:10:34 -04:00
|
|
|
|
2022-01-11 01:10:58 -05:00
|
|
|
# Schedule a new backup job through a non-blocking JSON based pipe protocol
|
|
|
|
#
|
|
|
|
# @see https://gitlab.com/gitlab-org/gitaly/-/blob/master/doc/gitaly-backup.md
|
|
|
|
def schedule_backup_job(repository, always_create:)
|
|
|
|
connection_params = Gitlab::GitalyClient.connection_data(repository.storage)
|
|
|
|
|
|
|
|
json_job = {
|
|
|
|
address: connection_params['address'],
|
|
|
|
token: connection_params['token'],
|
|
|
|
storage_name: repository.storage,
|
|
|
|
relative_path: repository.relative_path,
|
|
|
|
gl_project_path: repository.gl_project_path,
|
|
|
|
always_create: always_create
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
@input_stream.puts(json_job)
|
|
|
|
end
|
|
|
|
|
2021-09-28 11:11:30 -04:00
|
|
|
def build_env
|
|
|
|
{
|
|
|
|
'SSL_CERT_FILE' => OpenSSL::X509::DEFAULT_CERT_FILE,
|
|
|
|
'SSL_CERT_DIR' => OpenSSL::X509::DEFAULT_CERT_DIR
|
|
|
|
}.merge(ENV)
|
|
|
|
end
|
|
|
|
|
2021-06-14 23:10:34 -04:00
|
|
|
def started?
|
2021-07-20 20:09:07 -04:00
|
|
|
@thread.present?
|
2021-06-14 23:10:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def bin_path
|
2022-03-28 11:07:51 -04:00
|
|
|
raise Error, 'gitaly-backup binary not found and gitaly_backup_path is not configured' unless Gitlab.config.backup.gitaly_backup_path.present?
|
|
|
|
|
2021-06-30 17:07:26 -04:00
|
|
|
File.absolute_path(Gitlab.config.backup.gitaly_backup_path)
|
2021-06-14 23:10:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|