Class: Backup::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/repository.rb

Defined Under Namespace

Classes: InterlockSizedQueue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(progress) ⇒ Repository

Returns a new instance of Repository.


9
10
11
# File 'lib/backup/repository.rb', line 9

def initialize(progress)
  @progress = progress
end

Instance Attribute Details

#progressObject (readonly)

Returns the value of attribute progress


7
8
9
# File 'lib/backup/repository.rb', line 7

def progress
  @progress
end

Instance Method Details

#backup_custom_hooks(project) ⇒ Object


54
55
56
57
58
59
60
# File 'lib/backup/repository.rb', line 54

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

#backup_project(project) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'lib/backup/repository.rb', line 44

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

#dump(max_concurrency:, max_storage_concurrency:) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/backup/repository.rb', line 13

def dump(max_concurrency:, max_storage_concurrency:)
  prepare

  if max_concurrency <= 1 && max_storage_concurrency <= 1
    return dump_consecutive
  end

  if Project.excluding_repository_storage(Gitlab.config.repositories.storages.keys).exists?
    raise Error, 'repositories.storages in gitlab.yml is misconfigured'
  end

  semaphore = Concurrent::Semaphore.new(max_concurrency)
  errors = Queue.new

  threads = Gitlab.config.repositories.storages.keys.map do |storage|
    Thread.new do
      Rails.application.executor.wrap do
        dump_storage(storage, semaphore, max_storage_concurrency: max_storage_concurrency)
      rescue => e
        errors << e
      end
    end
  end

  ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
    threads.each(&:join)
  end

  raise errors.pop unless errors.empty?
end

#restoreObject


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/backup/repository.rb', line 71

def restore
  Project.find_each(batch_size: 1000) do |project|
    progress.print " * #{project.full_path} ... "

    restore_repo_success =
      begin
        try_restore_repository(project)
      rescue => err
        progress.puts "Error: #{err}".color(:red)
        false
      end

    if restore_repo_success
      progress.puts "[DONE]".color(:green)
    else
      progress.puts "[Failed] restoring #{project.full_path} repository".color(:red)
    end

    wiki = ProjectWiki.new(project)
    wiki.repository.remove rescue nil
    path_to_wiki_bundle = path_to_bundle(wiki)

    if File.exist?(path_to_wiki_bundle)
      progress.print " * #{wiki.full_path} ... "
      begin
        wiki.repository.create_from_bundle(path_to_wiki_bundle)
        restore_custom_hooks(wiki)

        progress.puts "[DONE]".color(:green)
      rescue => e
        progress.puts "[Failed] restoring #{wiki.full_path} wiki".color(:red)
        progress.puts "Error #{e}".color(:red)
      end
    end
  end

  restore_object_pools
end

#restore_custom_hooks(project) ⇒ Object


62
63
64
65
66
67
68
69
# File 'lib/backup/repository.rb', line 62

def restore_custom_hooks(project)
  return unless Dir.exist?(project_backup_path(project))
  return if Dir.glob("#{project_backup_path(project)}/custom_hooks*").none?

  custom_hooks_path = custom_hooks_tar(project)
  Gitlab::GitalyClient::RepositoryService.new(project.repository)
    .restore_custom_hooks(custom_hooks_path)
end