BigW Consortium Gitlab

repository.rb 4.39 KB
Newer Older
1 2 3 4 5 6 7 8
require 'yaml'

module Backup
  class Repository
    def dump
      prepare

      Project.find_each(batch_size: 1000) do |project|
9
        $progress.print " * #{project.path_with_namespace} ... "
10 11 12 13

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

14
        if project.empty_repo?
15
          $progress.puts "[SKIPPED]".color(:cyan)
16
        else
17
          cmd = %W(tar -cf #{path_to_bundle(project)} -C #{path_to_repo(project)} .)
18
          output, status = Gitlab::Popen.popen(cmd)
19
          if status.zero?
20
            $progress.puts "[DONE]".color(:green)
21
          else
22
            puts "[FAILED]".color(:red)
23
            puts "failed: #{cmd.join(' ')}"
24 25 26
            puts output
            abort 'Backup failed'
          end
27
        end
28

29
        wiki = ProjectWiki.new(project)
30

31
        if File.exist?(path_to_repo(wiki))
32
          $progress.print " * #{wiki.path_with_namespace} ... "
33
          if wiki.repository.empty?
34
            $progress.puts " [SKIPPED]".color(:cyan)
35
          else
36
            cmd = %W(#{Gitlab.config.git.bin_path} --git-dir=#{path_to_repo(wiki)} bundle create #{path_to_bundle(wiki)} --all)
37
            output, status = Gitlab::Popen.popen(cmd)
38
            if status.zero?
39
              $progress.puts " [DONE]".color(:green)
40
            else
41
              puts " [FAILED]".color(:red)
42
              puts "failed: #{cmd.join(' ')}"
43 44
              abort 'Backup failed'
            end
45 46
          end
        end
47 48 49 50
      end
    end

    def restore
51
      Gitlab.config.repositories.storages.each do |name, path|
52
        next unless File.exist?(path)
53

54
        # Move repos dir to 'repositories.old' dir
55 56
        bk_repos_path = File.join(path, '..', 'repositories.old.' + Time.now.to_i.to_s)
        FileUtils.mv(path, bk_repos_path)
Stan Hu committed
57 58
        # This is expected from gitlab:check
        FileUtils.mkdir_p(path, mode: 2770)
59 60 61
      end

      Project.find_each(batch_size: 1000) do |project|
62
        $progress.print " * #{project.path_with_namespace} ... "
63

64
        project.ensure_dir_exist
65

66
        if File.exist?(path_to_bundle(project))
67 68
          FileUtils.mkdir_p(path_to_repo(project))
          cmd = %W(tar -xf #{path_to_bundle(project)} -C #{path_to_repo(project)})
69
        else
70
          cmd = %W(#{Gitlab.config.git.bin_path} init --bare #{path_to_repo(project)})
71 72 73
        end

        if system(*cmd, silent)
74
          $progress.puts "[DONE]".color(:green)
75
        else
76
          puts "[FAILED]".color(:red)
77
          puts "failed: #{cmd.join(' ')}"
78
          abort 'Restore failed'
79
        end
80

81
        wiki = ProjectWiki.new(project)
82

83
        if File.exist?(path_to_bundle(wiki))
84 85 86 87 88 89
          $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'.
          FileUtils.rm_rf(path_to_repo(wiki))
90
          cmd = %W(#{Gitlab.config.git.bin_path} clone --bare #{path_to_bundle(wiki)} #{path_to_repo(wiki)})
91

92
          if system(*cmd, silent)
93
            $progress.puts " [DONE]".color(:green)
94
          else
95
            puts " [FAILED]".color(:red)
96 97 98
            puts "failed: #{cmd.join(' ')}"
            abort 'Restore failed'
          end
99
        end
100
      end
101

102
      $progress.print 'Put GitLab hooks in repositories dirs'.color(:yellow)
103 104
      cmd = %W(#{Gitlab.config.gitlab_shell.path}/bin/create-hooks) + repository_storage_paths_args
      if system(*cmd)
105
        $progress.puts " [DONE]".color(:green)
106
      else
107
        puts " [FAILED]".color(:red)
108
        puts "failed: #{cmd}"
109 110
      end

111 112 113 114 115
    end

    protected

    def path_to_repo(project)
116
      project.repository.path_to_repo
117 118 119 120 121 122 123 124 125 126 127 128
    end

    def path_to_bundle(project)
      File.join(backup_repos_path, project.path_with_namespace + ".bundle")
    end

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

    def prepare
      FileUtils.rm_rf(backup_repos_path)
129 130 131 132
      # 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)
133
    end
134 135 136 137

    def silent
      {err: '/dev/null', out: '/dev/null'}
    end
138 139 140 141 142 143

    private

    def repository_storage_paths_args
      Gitlab.config.repositories.storages.values
    end
144 145
  end
end