2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
namespace :gitlab do
|
2023-04-23 21:23:45 +05:30
|
|
|
require 'active_record/fixtures'
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
namespace :backup do
|
2022-11-25 23:54:43 +05:30
|
|
|
PID = Process.pid.freeze
|
|
|
|
PID_FILE = "#{Rails.application.root}/tmp/backup_restore.pid"
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
# 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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
warn_user_is_not_gitlab
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
Backup::Manager.new(progress).create
|
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
warn_user_is_not_gitlab
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
Backup::Manager.new(progress).restore
|
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('repositories')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('repositories')
|
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
2023-04-23 21:23:45 +05:30
|
|
|
Backup::Manager.new(progress).run_create_task('db')
|
2022-11-25 23:54:43 +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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
2023-04-23 21:23:45 +05:30
|
|
|
Backup::Manager.new(progress).run_restore_task('db')
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('builds')
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('builds')
|
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('uploads')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('uploads')
|
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('artifacts')
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('artifacts')
|
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('pages')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('pages')
|
|
|
|
end
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('lfs')
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('lfs')
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
namespace :terraform_state do
|
|
|
|
task create: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('terraform_state')
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('terraform_state')
|
|
|
|
end
|
2022-03-02 08:16:31 +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
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('registry')
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('registry')
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
namespace :packages do
|
|
|
|
task create: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_create_task('packages')
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
task restore: :gitlab_environment do
|
2022-11-25 23:54:43 +05:30
|
|
|
lock do
|
|
|
|
Backup::Manager.new(progress).run_restore_task('packages')
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
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
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
def lock
|
|
|
|
File.open(PID_FILE, File::RDWR | File::CREAT, 0644) do |f|
|
|
|
|
f.flock(File::LOCK_EX)
|
|
|
|
|
|
|
|
unless f.read.empty?
|
|
|
|
# There is a PID inside so the process fails
|
|
|
|
progress.puts(<<~HEREDOC.color(:red))
|
|
|
|
Backup and restore in progress:
|
|
|
|
There is a backup and restore task in progress. Please, try to run the current task once the previous one ends.
|
|
|
|
If there is no other process running, please remove the PID file manually: rm #{PID_FILE}
|
|
|
|
HEREDOC
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
f.write(PID)
|
|
|
|
f.flush
|
|
|
|
ensure
|
|
|
|
f.flock(File::LOCK_UN)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
progress.puts "#{Time.now} " + "-- Deleting backup and restore lock file".color(:blue)
|
|
|
|
File.delete(PID_FILE)
|
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
# namespace end: backup
|
|
|
|
end
|
|
|
|
# namespace end: gitlab
|