repository.rb 6.4 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4
require 'yaml'

module Backup
  class Repository
5
    # rubocop:disable Metrics/AbcSize
D
Dmitriy Zaporozhets 已提交
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)
D
Dmitriy Zaporozhets 已提交
13 14

        # Create namespace dir if missing
15
        FileUtils.mkdir_p(File.join(backup_repos_path, project.namespace.full_path)) if project.namespace
D
Dmitriy Zaporozhets 已提交
16

17
        if project.empty_repo?
C
Connor Shea 已提交
18
          $progress.puts "[SKIPPED]".color(:cyan)
D
Dmitriy Zaporozhets 已提交
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

J
Jacob Vosmaer 已提交
36
          if status.zero?
C
Connor Shea 已提交
37
            $progress.puts "[DONE]".color(:green)
J
Jacob Vosmaer 已提交
38
          else
C
Connor Shea 已提交
39
            puts "[FAILED]".color(:red)
40
            puts "failed: #{cmd.join(' ')}"
J
Jacob Vosmaer 已提交
41 42 43
            puts output
            abort 'Backup failed'
          end
D
Dmitriy Zaporozhets 已提交
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?
C
Connor Shea 已提交
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)
J
Jacob Vosmaer 已提交
57
            if status.zero?
C
Connor Shea 已提交
58
              $progress.puts " [DONE]".color(:green)
J
Jacob Vosmaer 已提交
59
            else
C
Connor Shea 已提交
60
              puts " [FAILED]".color(:red)
61
              puts "failed: #{cmd.join(' ')}"
62
              puts output
J
Jacob Vosmaer 已提交
63 64
              abort 'Backup failed'
            end
65 66
          end
        end
D
Dmitriy Zaporozhets 已提交
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

D
Dmitriy Zaporozhets 已提交
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)
S
Stan Hu 已提交
78
        # This is expected from gitlab:check
79
        FileUtils.mkdir_p(path, mode: 02770)
D
Dmitriy Zaporozhets 已提交
80 81 82
      end

      Project.find_each(batch_size: 1000) do |project|
A
Achilleas Pipinellis 已提交
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)
D
Dmitriy Zaporozhets 已提交
86

87
        project.ensure_dir_exist
D
Dmitriy Zaporozhets 已提交
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?
C
Connor Shea 已提交
97
          $progress.puts "[DONE]".color(:green)
D
Dmitriy Zaporozhets 已提交
98
        else
C
Connor Shea 已提交
99
          puts "[FAILED]".color(:red)
100
          puts "failed: #{cmd.join(' ')}"
101
          puts output
J
Jacob Vosmaer 已提交
102
          abort 'Restore failed'
D
Dmitriy Zaporozhets 已提交
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)
A
Achilleas Pipinellis 已提交
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?
C
Connor Shea 已提交
132
            $progress.puts " [DONE]".color(:green)
A
Achilleas Pipinellis 已提交
133
          else
C
Connor Shea 已提交
134
            puts " [FAILED]".color(:red)
A
Achilleas Pipinellis 已提交
135
            puts "failed: #{cmd.join(' ')}"
136
            puts output
A
Achilleas Pipinellis 已提交
137 138
            abort 'Restore failed'
          end
139
        end
D
Dmitriy Zaporozhets 已提交
140
      end
141

C
Connor Shea 已提交
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?
C
Connor Shea 已提交
147
        $progress.puts " [DONE]".color(:green)
148
      else
C
Connor Shea 已提交
149
        puts " [FAILED]".color(:red)
150
        puts "failed: #{cmd}"
151
        puts output
152
      end
D
Dmitriy Zaporozhets 已提交
153
    end
154
    # rubocop:enable Metrics/AbcSize
D
Dmitriy Zaporozhets 已提交
155 156 157 158

    protected

    def path_to_repo(project)
159
      project.repository.path_to_repo
D
Dmitriy Zaporozhets 已提交
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
D
Dmitriy Zaporozhets 已提交
174 175 176
    end

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

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

      dir_entries = Dir.entries(path)
P
Pawel Chojnacki 已提交
184 185

      yield('custom_hooks') if dir_entries.include?('custom_hooks')
D
Dmitriy Zaporozhets 已提交
186 187 188 189
    end

    def prepare
      FileUtils.rm_rf(backup_repos_path)
190 191 192 193
      # 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)
D
Dmitriy Zaporozhets 已提交
194
    end
195 196

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

    private

    def repository_storage_paths_args
203
      Gitlab.config.repositories.storages.values.map { |rs| rs['path'] }
204
    end
D
Dmitriy Zaporozhets 已提交
205 206
  end
end