2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
module Tasks
|
|
|
|
module Gitlab
|
|
|
|
module Backup
|
|
|
|
PID = Process.pid.freeze
|
|
|
|
PID_FILE = "#{Rails.application.root}/tmp/backup_restore.pid"
|
|
|
|
|
|
|
|
def self.create_backup
|
|
|
|
lock_backup do
|
|
|
|
::Gitlab::TaskHelpers.warn_user_is_not_gitlab
|
|
|
|
|
|
|
|
::Backup::Manager.new(backup_progress).create
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.restore_backup
|
|
|
|
lock_backup do
|
|
|
|
::Gitlab::TaskHelpers.warn_user_is_not_gitlab
|
|
|
|
|
|
|
|
::Backup::Manager.new(backup_progress).restore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_task(task)
|
|
|
|
lock_backup do
|
|
|
|
::Backup::Manager.new(backup_progress).run_create_task(task)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.restore_task(task)
|
|
|
|
lock_backup do
|
|
|
|
::Backup::Manager.new(backup_progress).run_restore_task(task)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.backup_progress
|
|
|
|
if ENV['CRON']
|
|
|
|
# We need an object we can say 'puts' and 'print' to; let's use a StringIO.
|
|
|
|
StringIO.new
|
|
|
|
else
|
|
|
|
$stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lock_backup
|
|
|
|
File.open(PID_FILE, File::RDWR | File::CREAT) do |f|
|
|
|
|
f.flock(File::LOCK_EX)
|
|
|
|
|
|
|
|
file_content = f.read
|
|
|
|
|
|
|
|
read_pid(file_content) unless file_content.blank?
|
|
|
|
|
|
|
|
f.rewind
|
|
|
|
f.write(PID)
|
|
|
|
f.flush
|
|
|
|
ensure
|
|
|
|
f.flock(File::LOCK_UN)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
backup_progress.puts(
|
|
|
|
"#{Time.current} " + '-- Deleting backup and restore PID file ... '.color(:blue) + 'done'.color(:green)
|
|
|
|
)
|
|
|
|
File.delete(PID_FILE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.read_pid(file_content)
|
|
|
|
Process.getpgid(file_content.to_i)
|
|
|
|
|
|
|
|
backup_progress.puts(<<~MESSAGE.color(:red))
|
|
|
|
Backup and restore in progress:
|
|
|
|
There is a backup and restore task in progress (PID #{file_content}).
|
|
|
|
Try to run the current task once the previous one ends.
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
backup_progress.puts(<<~MESSAGE.color(:blue))
|
|
|
|
The PID file #{PID_FILE} exists and contains #{file_content}, but the process is not running.
|
|
|
|
The PID file will be rewritten with the current process ID #{PID}.
|
|
|
|
MESSAGE
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :backup_progress, :lock_backup, :read_pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
namespace :gitlab do
|
2023-04-23 21:23:45 +05:30
|
|
|
require 'active_record/fixtures'
|
2023-07-09 08:55:56 +05:30
|
|
|
require 'stringio'
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_backup
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_backup
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('repositories')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('repositories')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('db')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('db')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('builds')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('builds')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('uploads')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('uploads')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('artifacts')
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('artifacts')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('pages')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('pages')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('lfs')
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('lfs')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('terraform_state')
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('terraform_state')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('registry')
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('registry')
|
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
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.create_task('packages')
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
task restore: :gitlab_environment do
|
2023-07-09 08:55:56 +05:30
|
|
|
Tasks::Gitlab::Backup.restore_task('packages')
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
# namespace end: backup
|
|
|
|
end
|
|
|
|
# namespace end: gitlab
|