BigW Consortium Gitlab

backup.rake 5.54 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      unless backup.skipped?('db')
        unless ENV['force'] == 'yes'
          warning = warning = <<-MSG.strip_heredoc
            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
          ask_to_continue
          puts 'Removing all tables. Press `Ctrl-C` within 5 seconds to abort'.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 ... '.blue
        Rake::Task['gitlab:db:drop_tables'].invoke
        $progress.puts 'done'.green
        Rake::Task['gitlab:backup:db:restore'].invoke
      end
      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')
      Rake::Task['gitlab:backup:lfs:restore'].invoke unless backup.skipped?('lfs')
      Rake::Task['gitlab:shell:setup'].invoke
58

59
      backup.cleanup
60 61
    end

62
    namespace :repo do
63
      task create: :environment do
64
        $progress.puts "Dumping repositories ...".blue
65 66 67 68 69 70 71

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

74
      task restore: :environment do
75
        $progress.puts "Restoring repositories ...".blue
76
        Backup::Repository.new.restore
77
        $progress.puts "done".green
78 79 80
      end
    end

81
    namespace :db do
82
      task create: :environment do
83
        $progress.puts "Dumping database ... ".blue
84 85 86 87 88 89 90

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

93
      task restore: :environment do
94
        $progress.puts "Restoring database ... ".blue
95
        Backup::Database.new.restore
96
        $progress.puts "done".green
97 98
      end
    end
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    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

119
    namespace :uploads do
120
      task create: :environment do
121
        $progress.puts "Dumping uploads ... ".blue
122 123 124 125 126 127 128

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

131
      task restore: :environment do
132
        $progress.puts "Restoring uploads ... ".blue
133
        Backup::Uploads.new.restore
134 135 136 137
        $progress.puts "done".green
      end
    end

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    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

176 177
    def configure_cron_mode
      if ENV['CRON']
178 179
        # We need an object we can say 'puts' and 'print' to; let's use a
        # StringIO.
180 181 182 183
        require 'stringio'
        $progress = StringIO.new
      else
        $progress = $stdout
184 185
      end
    end
186
  end # namespace end: backup
187
end # namespace end: gitlab