Commit f3f7a62e authored by James Fargher's avatar James Fargher

Port repository backup to use GlRepository

parent 62a2315a
......@@ -41,24 +41,6 @@ module Backup
raise errors.pop unless errors.empty?
end
def backup_project(project)
path_to_project_bundle = path_to_bundle(project)
Gitlab::GitalyClient::RepositoryService.new(project.repository)
.create_bundle(path_to_project_bundle)
backup_custom_hooks(project)
rescue => e
progress_warn(project, e, 'Failed to backup repo')
end
def backup_custom_hooks(project)
FileUtils.mkdir_p(project_backup_path(project))
custom_hooks_path = custom_hooks_tar(project)
Gitlab::GitalyClient::RepositoryService.new(project.repository)
.backup_custom_hooks(custom_hooks_path)
end
def restore_custom_hooks(project)
return unless Dir.exist?(project_backup_path(project))
return if Dir.glob("#{project_backup_path(project)}/custom_hooks*").none?
......@@ -107,7 +89,7 @@ module Backup
restore_object_pools
end
protected
private
def try_restore_repository(project)
path_to_project_bundle = path_to_bundle(project)
......@@ -145,8 +127,6 @@ module Backup
FileUtils.mkdir(backup_repos_path, mode: 0700)
end
private
def dump_consecutive
Project.includes(:route, :group, namespace: :owner).find_each(batch_size: 1000) do |project|
dump_project(project)
......@@ -193,29 +173,32 @@ module Backup
end
def dump_project(project)
progress.puts " * #{display_repo_path(project)} ... "
backup_repository(project, Gitlab::GlRepository::PROJECT)
backup_repository(project, Gitlab::GlRepository::WIKI)
end
if project.hashed_storage?(:repository)
FileUtils.mkdir_p(File.dirname(File.join(backup_repos_path, project.disk_path)))
else
FileUtils.mkdir_p(File.join(backup_repos_path, project.namespace.full_path)) if project.namespace
end
def backup_repository(container, type)
repository = type.repository_for(container)
if !empty_repo?(project)
backup_project(project)
progress.puts " * #{display_repo_path(project)} ... " + "[DONE]".color(:green)
else
progress.puts " * #{display_repo_path(project)} ... " + "[SKIPPED]".color(:cyan)
progress.puts " * #{display_repo_path(repository)} ... "
if empty_repository?(repository)
progress.puts " * #{display_repo_path(repository)} ... " + "[SKIPPED]".color(:cyan)
return
end
wiki = ProjectWiki.new(project)
FileUtils.mkdir_p(project_backup_path(repository))
if !empty_repo?(wiki)
backup_project(wiki)
progress.puts " * #{display_repo_path(project)} ... " + "[DONE] Wiki".color(:green)
else
progress.puts " * #{display_repo_path(project)} ... " + "[SKIPPED] Wiki".color(:cyan)
end
path_to_project_bundle = path_to_bundle(repository)
repository.bundle_to_disk(path_to_project_bundle)
custom_hooks_path = custom_hooks_tar(repository)
repository.gitaly_repository_client.backup_custom_hooks(custom_hooks_path)
progress.puts " * #{display_repo_path(repository)} ... " + "[DONE]".color(:green)
rescue => e
progress_warn(container, e, 'Failed to backup repo')
end
def progress_warn(project, cmd, output)
......@@ -223,13 +206,13 @@ module Backup
progress.puts "Ignoring error on #{display_repo_path(project)} - #{output}".color(:orange)
end
def empty_repo?(project_or_wiki)
project_or_wiki.repository.expire_emptiness_caches
project_or_wiki.repository.empty?
def empty_repository?(repository)
repository.expire_emptiness_caches
repository.empty?
end
def display_repo_path(project)
project.hashed_storage?(:repository) ? "#{project.full_path} (#{project.disk_path})" : project.full_path
"#{project.full_path} (#{project.disk_path})"
end
def restore_object_pools
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe Backup::Repository do
let_it_be(:project) { create(:project, :wiki_repo) }
let_it_be(:project) { create(:project, :repository, :wiki_repo) }
let(:progress) { StringIO.new }
......@@ -29,6 +29,12 @@ RSpec.describe Backup::Repository do
let(:storage_keys) { %w[default test_second_storage] }
context 'no concurrency' do
it 'creates repository bundle' do
subject.dump(max_concurrency: 1, max_storage_concurrency: 1)
expect(File.exist?(File.join(Gitlab.config.backup.path, 'repositories', project.disk_path + '.bundle'))).to be_truthy
end
it 'creates the expected number of threads' do
expect(Thread).not_to receive(:new)
......@@ -64,6 +70,16 @@ RSpec.describe Backup::Repository do
subject.dump(max_concurrency: 1, max_storage_concurrency: 1)
end.not_to exceed_query_limit(control_count)
end
context 'legacy storage' do
let_it_be(:project) { create(:project, :repository, :legacy_storage, :wiki_repo) }
it 'creates repository bundle' do
subject.dump(max_concurrency: 1, max_storage_concurrency: 1)
expect(File.exist?(File.join(Gitlab.config.backup.path, 'repositories', project.disk_path + '.bundle'))).to be_truthy
end
end
end
[4, 10].each do |max_storage_concurrency|
......@@ -204,27 +220,28 @@ RSpec.describe Backup::Repository do
end
end
describe '#empty_repo?' do
describe '#empty_repository?' do
context 'for a wiki' do
let(:wiki) { create(:project_wiki) }
let(:repository) { wiki.repository }
it 'invalidates the emptiness cache' do
expect(wiki.repository).to receive(:expire_emptiness_caches).once
expect(repository).to receive(:expire_emptiness_caches).once
subject.send(:empty_repo?, wiki)
subject.send(:empty_repository?, repository)
end
context 'wiki repo has content' do
let!(:wiki_page) { create(:wiki_page, wiki: wiki) }
it 'returns true, regardless of bad cache value' do
expect(subject.send(:empty_repo?, wiki)).to be(false)
it 'returns false, regardless of bad cache value' do
expect(subject.send(:empty_repository?, repository)).to be_falsy
end
end
context 'wiki repo does not have content' do
it 'returns true, regardless of bad cache value' do
expect(subject.send(:empty_repo?, wiki)).to be_truthy
expect(subject.send(:empty_repository?, repository)).to be_truthy
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment