repository.rb 6.46 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
2
3
4
require 'yaml'

module Backup
  class Repository
5
    # rubocop:disable Metrics/AbcSize
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
7
8
9
    def dump
      prepare

      Project.find_each(batch_size: 1000) do |project|
10
        $progress.print " * #{project.path_with_namespace} ... "
11
12
        path_to_project_repo = path_to_repo(project)
        path_to_project_bundle = path_to_bundle(project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13
14

        # Create namespace dir if missing
15
        FileUtils.mkdir_p(File.join(backup_repos_path, project.namespace.full_path)) if project.namespace
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
16

17
        if project.empty_repo?
18
          $progress.puts "[SKIPPED]".color(:cyan)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19
        else
20
21
22
23
24
25
26
27
28
29
30
31
32
33
          in_path(path_to_project_repo) do |dir|
            FileUtils.mkdir_p(path_to_tars(project))
            cmd = %W(tar -cf #{path_to_tars(project, dir)} -C #{path_to_project_repo} #{dir})
            output, status = Gitlab::Popen.popen(cmd)

            unless status.zero?
              puts "[FAILED]".color(:red)
              puts "failed: #{cmd.join(' ')}"
              puts output
              abort 'Backup failed'
            end
          end

          cmd = %W(#{Gitlab.config.git.bin_path} --git-dir=#{path_to_project_repo} bundle create #{path_to_project_bundle} --all)
34
          output, status = Gitlab::Popen.popen(cmd)
35

36
          if status.zero?
37
            $progress.puts "[DONE]".color(:green)
38
          else
39
            puts "[FAILED]".color(:red)
40
            puts "failed: #{cmd.join(' ')}"
41
42
43
            puts output
            abort 'Backup failed'
          end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44
        end
45

46
        wiki = ProjectWiki.new(project)
47
48
        path_to_wiki_repo = path_to_repo(wiki)
        path_to_wiki_bundle = path_to_bundle(wiki)
49

50
        if File.exist?(path_to_wiki_repo)
51
          $progress.print " * #{wiki.path_with_namespace} ... "
52
          if wiki.repository.empty?
53
            $progress.puts " [SKIPPED]".color(:cyan)
54
          else
55
            cmd = %W(#{Gitlab.config.git.bin_path} --git-dir=#{path_to_wiki_repo} bundle create #{path_to_wiki_bundle} --all)
56
            output, status = Gitlab::Popen.popen(cmd)
57
            if status.zero?
58
              $progress.puts " [DONE]".color(:green)
59
            else
60
              puts " [FAILED]".color(:red)
61
              puts "failed: #{cmd.join(' ')}"
62
              puts output
63
64
              abort 'Backup failed'
            end
65
66
          end
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
67
68
69
70
      end
    end

    def restore
71
72
      Gitlab.config.repositories.storages.each do |name, repository_storage|
        path = repository_storage['path']
73
        next unless File.exist?(path)
74

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
75
        # Move repos dir to 'repositories.old' dir
76
77
        bk_repos_path = File.join(path, '..', 'repositories.old.' + Time.now.to_i.to_s)
        FileUtils.mv(path, bk_repos_path)
Stan Hu's avatar
Stan Hu committed
78
        # This is expected from gitlab:check
79
        FileUtils.mkdir_p(path, mode: 02770)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
80
81
82
      end

      Project.find_each(batch_size: 1000) do |project|
83
        $progress.print " * #{project.path_with_namespace} ... "
84
85
        path_to_project_repo = path_to_repo(project)
        path_to_project_bundle = path_to_bundle(project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
86

87
        project.ensure_dir_exist
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
88

89
90
91
92
93
        cmd = if File.exist?(path_to_project_bundle)
                %W(#{Gitlab.config.git.bin_path} clone --bare #{path_to_project_bundle} #{path_to_project_repo})
              else
                %W(#{Gitlab.config.git.bin_path} init --bare #{path_to_project_repo})
              end
94

95
96
        output, status = Gitlab::Popen.popen(cmd)
        if status.zero?
97
          $progress.puts "[DONE]".color(:green)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98
        else
99
          puts "[FAILED]".color(:red)
100
          puts "failed: #{cmd.join(' ')}"
101
          puts output
102
          abort 'Restore failed'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
103
        end
104

105
106
107
108
109
110
111
112
113
114
115
116
        in_path(path_to_tars(project)) do |dir|
          cmd = %W(tar -xf #{path_to_tars(project, dir)} -C #{path_to_project_repo} #{dir})

          output, status = Gitlab::Popen.popen(cmd)
          unless status.zero?
            puts "[FAILED]".color(:red)
            puts "failed: #{cmd.join(' ')}"
            puts output
            abort 'Restore failed'
          end
        end

117
        wiki = ProjectWiki.new(project)
118
119
        path_to_wiki_repo = path_to_repo(wiki)
        path_to_wiki_bundle = path_to_bundle(wiki)
120

121
        if File.exist?(path_to_wiki_bundle)
122
123
124
125
126
          $progress.print " * #{wiki.path_with_namespace} ... "

          # If a wiki bundle exists, first remove the empty repo
          # that was initialized with ProjectWiki.new() and then
          # try to restore with 'git clone --bare'.
127
128
          FileUtils.rm_rf(path_to_wiki_repo)
          cmd = %W(#{Gitlab.config.git.bin_path} clone --bare #{path_to_wiki_bundle} #{path_to_wiki_repo})
129

130
131
          output, status = Gitlab::Popen.popen(cmd)
          if status.zero?
132
            $progress.puts " [DONE]".color(:green)
133
          else
134
            puts " [FAILED]".color(:red)
135
            puts "failed: #{cmd.join(' ')}"
136
            puts output
137
138
            abort 'Restore failed'
          end
139
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
140
      end
141

142
      $progress.print 'Put GitLab hooks in repositories dirs'.color(:yellow)
143
      cmd = %W(#{Gitlab.config.gitlab_shell.path}/bin/create-hooks) + repository_storage_paths_args
144
145
146

      output, status = Gitlab::Popen.popen(cmd)
      if status.zero?
147
        $progress.puts " [DONE]".color(:green)
148
      else
149
        puts " [FAILED]".color(:red)
150
        puts "failed: #{cmd}"
151
        puts output
152
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
153
    end
154
    # rubocop:enable Metrics/AbcSize
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
155
156
157
158

    protected

    def path_to_repo(project)
159
      project.repository.path_to_repo
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
160
161
162
    end

    def path_to_bundle(project)
163
164
165
166
167
168
169
170
171
172
173
      File.join(backup_repos_path, project.path_with_namespace + '.bundle')
    end

    def path_to_tars(project, dir = nil)
      path = File.join(backup_repos_path, project.path_with_namespace)

      if dir
        File.join(path, "#{dir}.tar")
      else
        path
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
174
175
176
    end

    def backup_repos_path
177
178
179
180
181
182
183
184
185
186
      File.join(Gitlab.config.backup.path, 'repositories')
    end

    def in_path(path)
      return unless Dir.exist?(path)

      dir_entries = Dir.entries(path)
      %w[annex custom_hooks].each do |entry|
        yield(entry) if dir_entries.include?(entry)
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
187
188
189
190
    end

    def prepare
      FileUtils.rm_rf(backup_repos_path)
191
192
193
194
      # Ensure the parent dir of backup_repos_path exists
      FileUtils.mkdir_p(Gitlab.config.backup.path)
      # Fail if somebody raced to create backup_repos_path before us
      FileUtils.mkdir(backup_repos_path, mode: 0700)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
195
    end
196
197

    def silent
198
      { err: '/dev/null', out: '/dev/null' }
199
    end
200
201
202
203

    private

    def repository_storage_paths_args
204
      Gitlab.config.repositories.storages.values.map { |rs| rs['path'] }
205
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
206
207
  end
end