BigW Consortium Gitlab

backup.rake 4.77 KB
Newer Older
1 2 3
require 'active_record/fixtures'

namespace :gitlab do
4
  namespace :backup do
Nihad Abbasov committed
5
    # Create backup of GitLab system
6
    desc "GitLab | Create a backup of the GitLab system"
7
    task create: :environment do
8
      warn_user_is_not_gitlab
9
      configure_cron_mode
10

11 12
      Rake::Task["gitlab:backup:db:create"].invoke
      Rake::Task["gitlab:backup:repo:create"].invoke
13
      Rake::Task["gitlab:backup:uploads:create"].invoke
14
      Rake::Task["gitlab:backup:builds:create"].invoke
15
      Rake::Task["gitlab:backup:artifacts:create"].invoke
16
      Rake::Task["gitlab:backup:lfs:create"].invoke
17

18 19 20 21
      backup = Backup::Manager.new
      backup.pack
      backup.cleanup
      backup.remove_old
22 23
    end

Nihad Abbasov committed
24
    # Restore backup of GitLab system
25
    desc "GitLab | Restore a previously created backup"
26
    task restore: :environment do
27
      warn_user_is_not_gitlab
28
      configure_cron_mode
29

30 31
      backup = Backup::Manager.new
      backup.unpack
32

33 34 35
      Rake::Task["gitlab:backup:db:restore"].invoke unless backup.skipped?("db")
      Rake::Task["gitlab:backup:repo:restore"].invoke unless backup.skipped?("repositories")
      Rake::Task["gitlab:backup:uploads:restore"].invoke unless backup.skipped?("uploads")
36
      Rake::Task["gitlab:backup:builds:restore"].invoke unless backup.skipped?("builds")
37
      Rake::Task["gitlab:backup:artifacts:restore"].invoke unless backup.skipped?("artifacts")
38
      Rake::Task["gitlab:backup:lfs:restore"].invoke unless backup.skipped?("lfs")
39
      Rake::Task["gitlab:shell:setup"].invoke
40

41
      backup.cleanup
42 43
    end

44
    namespace :repo do
45
      task create: :environment do
46
        $progress.puts "Dumping repositories ...".blue
47 48 49 50 51 52 53

        if ENV["SKIP"] && ENV["SKIP"].include?("repositories")
          $progress.puts "[SKIPPED]".cyan
        else
          Backup::Repository.new.dump
          $progress.puts "done".green
        end
54 55
      end

56
      task restore: :environment do
57
        $progress.puts "Restoring repositories ...".blue
58
        Backup::Repository.new.restore
59
        $progress.puts "done".green
60 61 62
      end
    end

63
    namespace :db do
64
      task create: :environment do
65
        $progress.puts "Dumping database ... ".blue
66 67 68 69 70 71 72

        if ENV["SKIP"] && ENV["SKIP"].include?("db")
          $progress.puts "[SKIPPED]".cyan
        else
          Backup::Database.new.dump
          $progress.puts "done".green
        end
73 74
      end

75
      task restore: :environment do
76
        $progress.puts "Restoring database ... ".blue
77
        Backup::Database.new.restore
78
        $progress.puts "done".green
79 80
      end
    end
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    namespace :builds do
      task create: :environment do
        $progress.puts "Dumping builds ... ".blue

        if ENV["SKIP"] && ENV["SKIP"].include?("builds")
          $progress.puts "[SKIPPED]".cyan
        else
          Backup::Builds.new.dump
          $progress.puts "done".green
        end
      end

      task restore: :environment do
        $progress.puts "Restoring builds ... ".blue
        Backup::Builds.new.restore
        $progress.puts "done".green
      end
    end

101
    namespace :uploads do
102
      task create: :environment do
103
        $progress.puts "Dumping uploads ... ".blue
104 105 106 107 108 109 110

        if ENV["SKIP"] && ENV["SKIP"].include?("uploads")
          $progress.puts "[SKIPPED]".cyan
        else
          Backup::Uploads.new.dump
          $progress.puts "done".green
        end
111 112
      end

113
      task restore: :environment do
114
        $progress.puts "Restoring uploads ... ".blue
115
        Backup::Uploads.new.restore
116 117 118 119
        $progress.puts "done".green
      end
    end

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    namespace :artifacts do
      task create: :environment do
        $progress.puts "Dumping artifacts ... ".blue

        if ENV["SKIP"] && ENV["SKIP"].include?("artifacts")
          $progress.puts "[SKIPPED]".cyan
        else
          Backup::Artifacts.new.dump
          $progress.puts "done".green
        end
      end

      task restore: :environment do
        $progress.puts "Restoring artifacts ... ".blue
        Backup::Artifacts.new.restore
        $progress.puts "done".green
      end
    end

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    namespace :lfs do
      task create: :environment do
        $progress.puts "Dumping lfs objects ... ".blue

        if ENV["SKIP"] && ENV["SKIP"].include?("lfs")
          $progress.puts "[SKIPPED]".cyan
        else
          Backup::Lfs.new.dump
          $progress.puts "done".green
        end
      end

      task restore: :environment do
        $progress.puts "Restoring lfs objects ... ".blue
        Backup::Lfs.new.restore
        $progress.puts "done".green
      end
    end

158 159
    def configure_cron_mode
      if ENV['CRON']
160 161
        # We need an object we can say 'puts' and 'print' to; let's use a
        # StringIO.
162 163 164 165
        require 'stringio'
        $progress = StringIO.new
      else
        $progress = $stdout
166 167
      end
    end
168
  end # namespace end: backup
169
end # namespace end: gitlab