summaryrefslogtreecommitdiff
path: root/lib/backup/gitaly_backup.rb
blob: 55fd68fd6e8b7cdd36b0d567a6f2582b43f601a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

module Backup
  # Backup and restores repositories using gitaly-backup
  class GitalyBackup
    def initialize(progress, parallel: nil, parallel_storage: nil)
      @progress = progress
      @parallel = parallel
      @parallel_storage = parallel_storage
    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

      args = []
      args += ['-parallel', @parallel.to_s] if type == :create && @parallel
      args += ['-parallel-storage', @parallel_storage.to_s] if type == :create && @parallel_storage

      @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
    end

    def wait
      return unless started?

      @stdin.close
      [@thread, @out_reader].each(&:join)
      status =  @thread.value

      @thread = nil

      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)

      @stdin.puts({
        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

    def parallel_enqueue?
      false
    end

    private

    def started?
      @thread.present?
    end

    def backup_repos_path
      File.absolute_path(File.join(Gitlab.config.backup.path, 'repositories'))
    end

    def bin_path
      File.absolute_path(Gitlab.config.backup.gitaly_backup_path)
    end
  end
end