BigW Consortium Gitlab

backup.rake 7.64 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
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: :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 49 50 51 52 53 54 55 56 57
        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
          # 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
58 59
        end
      end
60

61 62 63 64
      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')
65
      Rake::Task["gitlab:backup:pages:restore"].invoke unless backup.skipped?('pages')
66
      Rake::Task['gitlab:backup:lfs:restore'].invoke unless backup.skipped?('lfs')
67
      Rake::Task['gitlab:backup:registry:restore'].invoke unless backup.skipped?('registry')
68
      Rake::Task['gitlab:shell:setup'].invoke
69
      Rake::Task['cache:clear'].invoke
70

71
      backup.cleanup
72 73
    end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      task restore: :environment do
224
        $progress.puts "Restoring container registry images ... ".color(:blue)
225 226
        if Gitlab.config.registry.enabled
          Backup::Registry.new.restore
227
          $progress.puts "done".color(:green)
228
        else
229
          $progress.puts "[DISABLED]".color(:cyan)
230
        end
231 232 233
      end
    end

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