2021-06-14 23:10:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Backup
|
|
|
|
# Backup and restores repositories using gitaly-backup
|
|
|
|
class GitalyBackup
|
2021-07-12 14:09:09 -04:00
|
|
|
def initialize(progress, parallel: nil, parallel_storage: nil)
|
2021-06-14 23:10:34 -04:00
|
|
|
@progress = progress
|
2021-06-29 08:08:48 -04:00
|
|
|
@parallel = parallel
|
2021-07-12 14:09:09 -04:00
|
|
|
@parallel_storage = parallel_storage
|
2021-06-14 23:10:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start(type)
|
|
|
|
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 = []
|
|
|
|
args += ['-parallel', @parallel.to_s] if type == :create && @parallel
|
2021-07-12 14:09:09 -04:00
|
|
|
args += ['-parallel-storage', @parallel_storage.to_s] if type == :create && @parallel_storage
|
2021-06-29 08:08:48 -04:00
|
|
|
|
2021-07-20 20:09:07 -04:00
|
|
|
@stdin, stdout, @thread = Open3.popen2(ENV, bin_path, command, '-path', backup_repos_path, *args)
|
|
|
|
|
|
|
|
@out_reader = Thread.new do
|
|
|
|
IO.copy_stream(stdout, @progress)
|
|
|
|
end
|
2021-06-14 23:10:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def wait
|
|
|
|
return unless started?
|
|
|
|
|
2021-07-20 20:09:07 -04:00
|
|
|
@stdin.close
|
|
|
|
[@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)
|
|
|
|
|
2021-07-20 20:09:07 -04:00
|
|
|
@stdin.puts({
|
2021-06-14 23:10:34 -04:00
|
|
|
storage_name: repository.storage,
|
|
|
|
relative_path: repository.relative_path,
|
|
|
|
gl_project_path: repository.gl_project_path,
|
|
|
|
always_create: repo_type.project?
|
|
|
|
}.merge(Gitlab::GitalyClient.connection_data(repository.storage)).to_json)
|
|
|
|
end
|
|
|
|
|
2021-06-29 08:08:48 -04:00
|
|
|
def parallel_enqueue?
|
|
|
|
false
|
|
|
|
end
|
2021-06-14 23:10:34 -04:00
|
|
|
|
2021-06-29 08:08:48 -04:00
|
|
|
private
|
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 backup_repos_path
|
|
|
|
File.absolute_path(File.join(Gitlab.config.backup.path, 'repositories'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def bin_path
|
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
|