Read about our upcoming Code of Conduct on this issue

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

namespace :gitlab do
4
  namespace :backup do
Nihad Abbasov's avatar
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13
      Rake::Task["gitlab:backup:uploads:create"].invoke
14
      Rake::Task["gitlab:backup:builds:create"].invoke
Kamil Trzcinski's avatar
Kamil Trzcinski committed
15
      Rake::Task["gitlab:backup:artifacts:create"].invoke
Kamil Trzcinski's avatar
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's avatar
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
36
      unless backup.skipped?('db')
        unless ENV['force'] == 'yes'
Drew Blessing's avatar
Drew Blessing committed
37
          warning = <<-MSG.strip_heredoc
38
39
40
41
42
            Before restoring the database we recommend removing 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
Drew Blessing's avatar
Drew Blessing committed
43
          puts warning.color(:red)
44
          ask_to_continue
45
          puts 'Removing all tables. Press `Ctrl-C` within 5 seconds to abort'.color(:yellow)
46
47
48
49
          sleep(5)
        end
        # Drop all tables Load the schema to ensure we don't have any newer tables
        # hanging out from a failed upgrade
50
        $progress.puts 'Cleaning the database ... '.color(:blue)
51
        Rake::Task['gitlab:db:drop_tables'].invoke
52
        $progress.puts 'done'.color(:green)
53
54
        Rake::Task['gitlab:backup:db:restore'].invoke
      end
55

56
57
58
59
      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')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
60
      Rake::Task["gitlab:backup:pages:restore"].invoke unless backup.skipped?("pages")
61
      Rake::Task['gitlab:backup:lfs:restore'].invoke unless backup.skipped?('lfs')
62
      Rake::Task['gitlab:backup:registry:restore'].invoke unless backup.skipped?('registry')
63
      Rake::Task['gitlab:shell:setup'].invoke
64
      Rake::Task['cache:clear'].invoke
65

66
      backup.cleanup
67
68
    end

69
    namespace :repo do
70
      task create: :environment do
71
        $progress.puts "Dumping repositories ...".color(:blue)
72
73

        if ENV["SKIP"] && ENV["SKIP"].include?("repositories")
74
          $progress.puts "[SKIPPED]".color(:cyan)
75
76
        else
          Backup::Repository.new.dump
77
          $progress.puts "done".color(:green)
78
        end
79
80
      end

81
      task restore: :environment do
82
        $progress.puts "Restoring repositories ...".color(:blue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83
        Backup::Repository.new.restore
84
        $progress.puts "done".color(:green)
85
86
87
      end
    end

88
    namespace :db do
89
      task create: :environment do
90
        $progress.puts "Dumping database ... ".color(:blue)
91
92

        if ENV["SKIP"] && ENV["SKIP"].include?("db")
93
          $progress.puts "[SKIPPED]".color(:cyan)
94
95
        else
          Backup::Database.new.dump
96
          $progress.puts "done".color(:green)
97
        end
98
99
      end

100
      task restore: :environment do
101
        $progress.puts "Restoring database ... ".color(:blue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
102
        Backup::Database.new.restore
103
        $progress.puts "done".color(:green)
104
105
      end
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
106

107
108
    namespace :builds do
      task create: :environment do
109
        $progress.puts "Dumping builds ... ".color(:blue)
110
111

        if ENV["SKIP"] && ENV["SKIP"].include?("builds")
112
          $progress.puts "[SKIPPED]".color(:cyan)
113
114
        else
          Backup::Builds.new.dump
115
          $progress.puts "done".color(:green)
116
117
118
119
        end
      end

      task restore: :environment do
120
        $progress.puts "Restoring builds ... ".color(:blue)
121
        Backup::Builds.new.restore
122
        $progress.puts "done".color(:green)
123
124
125
      end
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
126
    namespace :uploads do
127
      task create: :environment do
128
        $progress.puts "Dumping uploads ... ".color(:blue)
129
130

        if ENV["SKIP"] && ENV["SKIP"].include?("uploads")
131
          $progress.puts "[SKIPPED]".color(:cyan)
132
133
        else
          Backup::Uploads.new.dump
134
          $progress.puts "done".color(:green)
135
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
136
137
      end

138
      task restore: :environment do
139
        $progress.puts "Restoring uploads ... ".color(:blue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
140
        Backup::Uploads.new.restore
141
        $progress.puts "done".color(:green)
142
143
144
      end
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
145
146
    namespace :artifacts do
      task create: :environment do
147
        $progress.puts "Dumping artifacts ... ".color(:blue)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
148
149

        if ENV["SKIP"] && ENV["SKIP"].include?("artifacts")
150
          $progress.puts "[SKIPPED]".color(:cyan)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
151
152
        else
          Backup::Artifacts.new.dump
153
          $progress.puts "done".color(:green)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
154
155
156
157
        end
      end

      task restore: :environment do
158
        $progress.puts "Restoring artifacts ... ".color(:blue)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
159
        Backup::Artifacts.new.restore
160
        $progress.puts "done".color(:green)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
161
162
163
      end
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    namespace :pages do
      task create: :environment do
        $progress.puts "Dumping pages ... ".blue

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

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

183
184
    namespace :lfs do
      task create: :environment do
185
        $progress.puts "Dumping lfs objects ... ".color(:blue)
186
187

        if ENV["SKIP"] && ENV["SKIP"].include?("lfs")
188
          $progress.puts "[SKIPPED]".color(:cyan)
189
190
        else
          Backup::Lfs.new.dump
191
          $progress.puts "done".color(:green)
192
193
194
195
        end
      end

      task restore: :environment do
196
        $progress.puts "Restoring lfs objects ... ".color(:blue)
197
        Backup::Lfs.new.restore
198
        $progress.puts "done".color(:green)
199
200
201
      end
    end

202
203
    namespace :registry do
      task create: :environment do
204
        $progress.puts "Dumping container registry images ... ".color(:blue)
205

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

      task restore: :environment do
219
        $progress.puts "Restoring container registry images ... ".color(:blue)
220
221
        if Gitlab.config.registry.enabled
          Backup::Registry.new.restore
222
          $progress.puts "done".color(:green)
223
        else
224
          $progress.puts "[DISABLED]".color(:cyan)
225
        end
226
227
228
      end
    end

229
230
    def configure_cron_mode
      if ENV['CRON']
231
232
        # We need an object we can say 'puts' and 'print' to; let's use a
        # StringIO.
233
234
235
236
        require 'stringio'
        $progress = StringIO.new
      else
        $progress = $stdout
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
237
238
      end
    end
239
  end # namespace end: backup
240
end # namespace end: gitlab