debian-mirror-gitlab/lib/tasks/gitlab/backup.rake

309 lines
10 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'active_record/fixtures'
namespace :gitlab do
namespace :backup do
# Create backup of GitLab system
2020-03-13 15:44:24 +05:30
desc 'GitLab | Backup | Create a backup of the GitLab system'
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2014-09-02 18:07:02 +05:30
warn_user_is_not_gitlab
2020-03-13 15:44:24 +05:30
Rake::Task['gitlab:backup:db:create'].invoke
Rake::Task['gitlab:backup:repo:create'].invoke
Rake::Task['gitlab:backup:uploads:create'].invoke
Rake::Task['gitlab:backup:builds:create'].invoke
Rake::Task['gitlab:backup:artifacts:create'].invoke
Rake::Task['gitlab:backup:pages:create'].invoke
Rake::Task['gitlab:backup:lfs:create'].invoke
Rake::Task['gitlab:backup:registry:create'].invoke
2014-09-02 18:07:02 +05:30
2018-11-08 19:23:39 +05:30
backup = Backup::Manager.new(progress)
2020-04-08 14:13:33 +05:30
backup.write_info
if ENV['SKIP'] && ENV['SKIP'].include?('tar')
backup.upload
else
backup.pack
backup.upload
backup.cleanup
backup.remove_old
end
2019-09-04 21:01:54 +05:30
progress.puts "Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data \n" \
"and are not included in this backup. You will need these files to restore a backup.\n" \
"Please back them up manually.".color(:red)
progress.puts "Backup task is done."
2014-09-02 18:07:02 +05:30
end
# Restore backup of GitLab system
2020-03-13 15:44:24 +05:30
desc 'GitLab | Backup | Restore a previously created backup'
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2014-09-02 18:07:02 +05:30
warn_user_is_not_gitlab
2018-11-08 19:23:39 +05:30
backup = Backup::Manager.new(progress)
2020-04-08 14:13:33 +05:30
cleanup_required = backup.unpack
backup.verify_backup_version
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
unless backup.skipped?('db')
2018-03-17 18:26:18 +05:30
begin
unless ENV['force'] == 'yes'
warning = <<-MSG.strip_heredoc
2020-11-24 15:15:51 +05:30
Be sure to stop Puma, Sidekiq, and any other process that
connects to the database before proceeding. For Omnibus
installs, see the following link for more information:
https://docs.gitlab.com/ee/raketasks/backup_restore.html#restore-for-omnibus-gitlab-installations
2018-03-17 18:26:18 +05:30
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
2019-03-02 22:35:43 +05:30
puts_time 'Cleaning the database ... '.color(:blue)
2018-03-17 18:26:18 +05:30
Rake::Task['gitlab:db:drop_tables'].invoke
2019-03-02 22:35:43 +05:30
puts_time 'done'.color(:green)
2018-03-17 18:26:18 +05:30
Rake::Task['gitlab:backup:db:restore'].invoke
rescue Gitlab::TaskAbortedByUserError
puts "Quitting...".color(:red)
exit 1
2016-06-02 11:05:42 +05:30
end
end
2017-08-17 22:00:37 +05:30
2016-06-02 11:05:42 +05:30
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')
2019-07-07 11:18:12 +05:30
Rake::Task['gitlab:backup:pages:restore'].invoke unless backup.skipped?('pages')
2016-06-02 11:05:42 +05:30
Rake::Task['gitlab:backup:lfs:restore'].invoke unless backup.skipped?('lfs')
Rake::Task['gitlab:backup:registry:restore'].invoke unless backup.skipped?('registry')
Rake::Task['gitlab:shell:setup'].invoke
2017-08-17 22:00:37 +05:30
Rake::Task['cache:clear'].invoke
2014-09-02 18:07:02 +05:30
2020-04-08 14:13:33 +05:30
if cleanup_required
backup.cleanup
end
2019-09-04 21:01:54 +05:30
puts "Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data \n" \
"and are not included in this backup. You will need to restore these files manually.".color(:red)
puts "Restore task is done."
2014-09-02 18:07:02 +05:30
end
namespace :repo do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping repositories ...".color(:blue)
2015-04-26 12:48:37 +05:30
2020-10-24 23:57:45 +05:30
max_concurrency = ENV.fetch('GITLAB_BACKUP_MAX_CONCURRENCY', 1).to_i
max_storage_concurrency = ENV.fetch('GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY', 1).to_i
2015-04-26 12:48:37 +05:30
if ENV["SKIP"] && ENV["SKIP"].include?("repositories")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2020-10-24 23:57:45 +05:30
elsif max_concurrency < 1 || max_storage_concurrency < 1
puts "GITLAB_BACKUP_MAX_CONCURRENCY and GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY must have a value of at least 1".color(:red)
exit 1
2015-04-26 12:48:37 +05:30
else
2021-09-04 01:27:46 +05:30
Backup::Repositories.new(progress, strategy: repository_backup_strategy).dump(
2020-10-24 23:57:45 +05:30
max_concurrency: max_concurrency,
max_storage_concurrency: max_storage_concurrency
)
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring repositories ...".color(:blue)
2021-09-04 01:27:46 +05:30
Backup::Repositories.new(progress, strategy: repository_backup_strategy).restore
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2014-09-02 18:07:02 +05:30
end
end
namespace :db do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping database ... ".color(:blue)
2015-04-26 12:48:37 +05:30
if ENV["SKIP"] && ENV["SKIP"].include?("db")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2015-04-26 12:48:37 +05:30
else
2018-11-08 19:23:39 +05:30
Backup::Database.new(progress).dump
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring database ... ".color(:blue)
2020-11-24 15:15:51 +05:30
errors = Backup::Database.new(progress).restore
if errors.present?
warning = <<~MSG
There were errors in restoring the schema. This may cause
issues if this results in missing indexes, constraints, or
columns. Please record the errors above and contact GitLab
Support if you have questions:
https://about.gitlab.com/support/
MSG
warn warning.color(:red)
ask_to_continue
end
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2014-09-02 18:07:02 +05:30
end
end
2015-09-25 12:07:36 +05:30
namespace :builds do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping builds ... ".color(:blue)
2015-09-25 12:07:36 +05:30
if ENV["SKIP"] && ENV["SKIP"].include?("builds")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2015-09-25 12:07:36 +05:30
else
2018-11-08 19:23:39 +05:30
Backup::Builds.new(progress).dump
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-09-25 12:07:36 +05:30
end
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring builds ... ".color(:blue)
2018-11-08 19:23:39 +05:30
Backup::Builds.new(progress).restore
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-09-25 12:07:36 +05:30
end
end
2014-09-02 18:07:02 +05:30
namespace :uploads do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping uploads ... ".color(:blue)
2015-04-26 12:48:37 +05:30
if ENV["SKIP"] && ENV["SKIP"].include?("uploads")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2015-04-26 12:48:37 +05:30
else
2018-11-08 19:23:39 +05:30
Backup::Uploads.new(progress).dump
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring uploads ... ".color(:blue)
2018-11-08 19:23:39 +05:30
Backup::Uploads.new(progress).restore
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-04-26 12:48:37 +05:30
end
end
2015-11-26 14:37:03 +05:30
namespace :artifacts do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping artifacts ... ".color(:blue)
2015-11-26 14:37:03 +05:30
if ENV["SKIP"] && ENV["SKIP"].include?("artifacts")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2015-11-26 14:37:03 +05:30
else
2018-11-08 19:23:39 +05:30
Backup::Artifacts.new(progress).dump
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-11-26 14:37:03 +05:30
end
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring artifacts ... ".color(:blue)
2018-11-08 19:23:39 +05:30
Backup::Artifacts.new(progress).restore
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-11-26 14:37:03 +05:30
end
end
2017-08-17 22:00:37 +05:30
namespace :pages do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping pages ... ".color(:blue)
2017-08-17 22:00:37 +05:30
if ENV["SKIP"] && ENV["SKIP"].include?("pages")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2017-08-17 22:00:37 +05:30
else
2018-11-08 19:23:39 +05:30
Backup::Pages.new(progress).dump
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring pages ... ".color(:blue)
2018-11-08 19:23:39 +05:30
Backup::Pages.new(progress).restore
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2017-08-17 22:00:37 +05:30
end
end
2015-11-26 14:37:03 +05:30
namespace :lfs do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping lfs objects ... ".color(:blue)
2015-11-26 14:37:03 +05:30
if ENV["SKIP"] && ENV["SKIP"].include?("lfs")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2015-11-26 14:37:03 +05:30
else
2018-11-08 19:23:39 +05:30
Backup::Lfs.new(progress).dump
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-11-26 14:37:03 +05:30
end
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring lfs objects ... ".color(:blue)
2018-11-08 19:23:39 +05:30
Backup::Lfs.new(progress).restore
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2015-11-26 14:37:03 +05:30
end
end
2016-06-02 11:05:42 +05:30
namespace :registry do
2018-03-17 18:26:18 +05:30
task create: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Dumping container registry images ... ".color(:blue)
2016-06-02 11:05:42 +05:30
if Gitlab.config.registry.enabled
if ENV["SKIP"] && ENV["SKIP"].include?("registry")
2019-03-02 22:35:43 +05:30
puts_time "[SKIPPED]".color(:cyan)
2016-06-02 11:05:42 +05:30
else
2018-11-08 19:23:39 +05:30
Backup::Registry.new(progress).dump
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2016-06-02 11:05:42 +05:30
end
else
2019-03-02 22:35:43 +05:30
puts_time "[DISABLED]".color(:cyan)
2016-06-02 11:05:42 +05:30
end
end
2018-03-17 18:26:18 +05:30
task restore: :gitlab_environment do
2019-03-02 22:35:43 +05:30
puts_time "Restoring container registry images ... ".color(:blue)
2018-03-17 18:26:18 +05:30
2016-06-02 11:05:42 +05:30
if Gitlab.config.registry.enabled
2018-11-08 19:23:39 +05:30
Backup::Registry.new(progress).restore
2019-03-02 22:35:43 +05:30
puts_time "done".color(:green)
2016-06-02 11:05:42 +05:30
else
2019-03-02 22:35:43 +05:30
puts_time "[DISABLED]".color(:cyan)
2016-06-02 11:05:42 +05:30
end
end
end
2019-03-02 22:35:43 +05:30
def puts_time(msg)
progress.puts "#{Time.now} -- #{msg}"
end
2018-11-08 19:23:39 +05:30
def progress
2015-04-26 12:48:37 +05:30
if ENV['CRON']
# We need an object we can say 'puts' and 'print' to; let's use a
# StringIO.
require 'stringio'
2018-11-08 19:23:39 +05:30
StringIO.new
2015-04-26 12:48:37 +05:30
else
2018-11-08 19:23:39 +05:30
$stdout
2014-09-02 18:07:02 +05:30
end
end
2021-09-04 01:27:46 +05:30
def repository_backup_strategy
if Feature.enabled?(:gitaly_backup)
Backup::GitalyBackup.new(progress)
else
Backup::GitalyRpcBackup.new(progress)
end
end
2020-11-24 15:15:51 +05:30
end
# namespace end: backup
end
# namespace end: gitlab