BigW Consortium Gitlab

repository.rb 6.28 KB
Newer Older
1 2 3 4
require 'yaml'

module Backup
  class Repository
5

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)
13 14 15 16

        # Create namespace dir if missing
        FileUtils.mkdir_p(File.join(backup_repos_path, project.namespace.path)) if project.namespace

17
        if project.empty_repo?
18
          $progress.puts "[SKIPPED]".color(:cyan)
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
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
67 68 69 70
      end
    end

    def restore
71
      Gitlab.config.repositories.storages.each do |name, path|
72
        next unless File.exist?(path)
73

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

      Project.find_each(batch_size: 1000) do |project|
82
        $progress.print " * #{project.path_with_namespace} ... "
83 84
        path_to_project_repo = path_to_repo(project)
        path_to_project_bundle = path_to_bundle(project)
85

86
        project.ensure_dir_exist
87

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

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

104 105 106 107 108 109 110 111 112 113 114 115
        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

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

120
        if File.exist?(path_to_wiki_bundle)
121 122 123 124 125
          $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'.
126 127
          FileUtils.rm_rf(path_to_wiki_repo)
          cmd = %W(#{Gitlab.config.git.bin_path} clone --bare #{path_to_wiki_bundle} #{path_to_wiki_repo})
128

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

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

      output, status = Gitlab::Popen.popen(cmd)
      if status.zero?
146
        $progress.puts " [DONE]".color(:green)
147
      else
148
        puts " [FAILED]".color(:red)
149
        puts "failed: #{cmd}"
150
        puts output
151
      end
152 153 154 155 156
    end

    protected

    def path_to_repo(project)
157
      project.repository.path_to_repo
158 159 160
    end

    def path_to_bundle(project)
161 162 163 164 165 166 167 168 169 170 171
      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
172 173 174
    end

    def backup_repos_path
175 176 177 178 179 180 181 182 183 184
      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
185 186 187 188
    end

    def prepare
      FileUtils.rm_rf(backup_repos_path)
189 190 191 192
      # 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)
193
    end
194 195 196 197

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

    private

    def repository_storage_paths_args
      Gitlab.config.repositories.storages.values
    end
204 205
  end
end