BigW Consortium Gitlab

backup.rake 7.76 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: :gitlab_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
Kamil Trzcinski committed
16
      Rake::Task["gitlab:backup:pages:create"].invoke
17
      Rake::Task["gitlab:backup:lfs:create"].invoke
18
      Rake::Task["gitlab:backup:registry:create"].invoke
19

20 21 22 23
      backup = Backup::Manager.new
      backup.pack
      backup.cleanup
      backup.remove_old
24 25
    end

Nihad Abbasov committed
26
    # Restore backup of GitLab system
27
    desc 'GitLab | Restore a previously created backup'
28
    task restore: :gitlab_environment do
29
      warn_user_is_not_gitlab
30
      configure_cron_mode
31

32 33
      backup = Backup::Manager.new
      backup.unpack
34

35
      unless backup.skipped?('db')
36 37 38 39 40 41 42 43 44 45 46 47 48
        begin
          unless ENV['force'] == 'yes'
            warning = <<-MSG.strip_heredoc
              Before restoring the database, we will remove all existing
              tables to avoid future upgrade problems. Be aware that if you have
              custom tables in the GitLab database these tables and all data will be
              removed.
            MSG
            puts warning.color(:red)
            ask_to_continue
            puts 'Removing all tables. Press `Ctrl-C` within 5 seconds to abort'.color(:yellow)
            sleep(5)
          end
49

50 51 52 53 54 55 56 57 58
          # Drop all tables Load the schema to ensure we don't have any newer tables
          # hanging out from a failed upgrade
          $progress.puts 'Cleaning the database ... '.color(:blue)
          Rake::Task['gitlab:db:drop_tables'].invoke
          $progress.puts 'done'.color(:green)
          Rake::Task['gitlab:backup:db:restore'].invoke
        rescue Gitlab::TaskAbortedByUserError
          puts "Quitting...".color(:red)
          exit 1
59 60
        end
      end
61

62 63 64 65
      Rake::Task['gitlab:backup:repo:restore'].invoke unless backup.skipped?('repositories')
      Rake::Task['gitlab:backup:uploads:restore'].invoke unless backup.skipped?('uploads')
      Rake::Task['gitlab:backup:builds:restore'].invoke unless backup.skipped?('builds')
      Rake::Task['gitlab:backup:artifacts:restore'].invoke unless backup.skipped?('artifacts')
66
      Rake::Task["gitlab:backup:pages:restore"].invoke unless backup.skipped?('pages')
67
      Rake::Task['gitlab:backup:lfs:restore'].invoke unless backup.skipped?('lfs')
68
      Rake::Task['gitlab:backup:registry:restore'].invoke unless backup.skipped?('registry')
69
      Rake::Task['gitlab:shell:setup'].invoke
70
      Rake::Task['cache:clear'].invoke
71

72
      backup.cleanup
73 74
    end

75
    namespace :repo do
76
      task create: :gitlab_environment do
77
        $progress.puts "Dumping repositories ...".color(:blue)
78 79

        if ENV["SKIP"] && ENV["SKIP"].include?("repositories")
80
          $progress.puts "[SKIPPED]".color(:cyan)
81 82
        else
          Backup::Repository.new.dump
83
          $progress.puts "done".color(:green)
84
        end
85 86
      end

87
      task restore: :gitlab_environment do
88
        $progress.puts "Restoring repositories ...".color(:blue)
89
        Backup::Repository.new.restore
90
        $progress.puts "done".color(:green)
91 92 93
      end
    end

94
    namespace :db do
95
      task create: :gitlab_environment do
96
        $progress.puts "Dumping database ... ".color(:blue)
97 98

        if ENV["SKIP"] && ENV["SKIP"].include?("db")
99
          $progress.puts "[SKIPPED]".color(:cyan)
100 101
        else
          Backup::Database.new.dump
102
          $progress.puts "done".color(:green)
103
        end
104 105
      end

106
      task restore: :gitlab_environment do
107
        $progress.puts "Restoring database ... ".color(:blue)
108
        Backup::Database.new.restore
109
        $progress.puts "done".color(:green)
110 111
      end
    end
112

113
    namespace :builds do
114
      task create: :gitlab_environment do
115
        $progress.puts "Dumping builds ... ".color(:blue)
116 117

        if ENV["SKIP"] && ENV["SKIP"].include?("builds")
118
          $progress.puts "[SKIPPED]".color(:cyan)
119 120
        else
          Backup::Builds.new.dump
121
          $progress.puts "done".color(:green)
122 123 124
        end
      end

125
      task restore: :gitlab_environment do
126
        $progress.puts "Restoring builds ... ".color(:blue)
127
        Backup::Builds.new.restore
128
        $progress.puts "done".color(:green)
129 130 131
      end
    end

132
    namespace :uploads do
133
      task create: :gitlab_environment do
134
        $progress.puts "Dumping uploads ... ".color(:blue)
135 136

        if ENV["SKIP"] && ENV["SKIP"].include?("uploads")
137
          $progress.puts "[SKIPPED]".color(:cyan)
138 139
        else
          Backup::Uploads.new.dump
140
          $progress.puts "done".color(:green)
141
        end
142 143
      end

144
      task restore: :gitlab_environment do
145
        $progress.puts "Restoring uploads ... ".color(:blue)
146
        Backup::Uploads.new.restore
147
        $progress.puts "done".color(:green)
148 149 150
      end
    end

151
    namespace :artifacts do
152
      task create: :gitlab_environment do
153
        $progress.puts "Dumping artifacts ... ".color(:blue)
154 155

        if ENV["SKIP"] && ENV["SKIP"].include?("artifacts")
156
          $progress.puts "[SKIPPED]".color(:cyan)
157 158
        else
          Backup::Artifacts.new.dump
159
          $progress.puts "done".color(:green)
160 161 162
        end
      end

163
      task restore: :gitlab_environment do
164
        $progress.puts "Restoring artifacts ... ".color(:blue)
165
        Backup::Artifacts.new.restore
166
        $progress.puts "done".color(:green)
167 168 169
      end
    end

Kamil Trzcinski committed
170
    namespace :pages do
171
      task create: :gitlab_environment do
172
        $progress.puts "Dumping pages ... ".color(:blue)
Kamil Trzcinski committed
173 174

        if ENV["SKIP"] && ENV["SKIP"].include?("pages")
175
          $progress.puts "[SKIPPED]".color(:cyan)
Kamil Trzcinski committed
176 177
        else
          Backup::Pages.new.dump
178
          $progress.puts "done".color(:green)
Kamil Trzcinski committed
179 180 181
        end
      end

182
      task restore: :gitlab_environment do
183
        $progress.puts "Restoring pages ... ".color(:blue)
Kamil Trzcinski committed
184
        Backup::Pages.new.restore
185
        $progress.puts "done".color(:green)
Kamil Trzcinski committed
186 187 188
      end
    end

189
    namespace :lfs do
190
      task create: :gitlab_environment do
191
        $progress.puts "Dumping lfs objects ... ".color(:blue)
192 193

        if ENV["SKIP"] && ENV["SKIP"].include?("lfs")
194
          $progress.puts "[SKIPPED]".color(:cyan)
195 196
        else
          Backup::Lfs.new.dump
197
          $progress.puts "done".color(:green)
198 199 200
        end
      end

201
      task restore: :gitlab_environment do
202
        $progress.puts "Restoring lfs objects ... ".color(:blue)
203
        Backup::Lfs.new.restore
204
        $progress.puts "done".color(:green)
205 206 207
      end
    end

208
    namespace :registry do
209
      task create: :gitlab_environment do
210
        $progress.puts "Dumping container registry images ... ".color(:blue)
211

212 213
        if Gitlab.config.registry.enabled
          if ENV["SKIP"] && ENV["SKIP"].include?("registry")
214
            $progress.puts "[SKIPPED]".color(:cyan)
215 216
          else
            Backup::Registry.new.dump
217
            $progress.puts "done".color(:green)
218
          end
219
        else
220
          $progress.puts "[DISABLED]".color(:cyan)
221 222 223
        end
      end

224
      task restore: :gitlab_environment do
225
        $progress.puts "Restoring container registry images ... ".color(:blue)
226

227 228
        if Gitlab.config.registry.enabled
          Backup::Registry.new.restore
229
          $progress.puts "done".color(:green)
230
        else
231
          $progress.puts "[DISABLED]".color(:cyan)
232
        end
233 234 235
      end
    end

236 237
    def configure_cron_mode
      if ENV['CRON']
238 239
        # We need an object we can say 'puts' and 'print' to; let's use a
        # StringIO.
240 241 242 243
        require 'stringio'
        $progress = StringIO.new
      else
        $progress = $stdout
244 245
      end
    end
246
  end # namespace end: backup
247
end # namespace end: gitlab