2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-06 08:43:19 -04:00
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
module Backup
|
2022-03-14 23:08:45 -04:00
|
|
|
class Files < Task
|
|
|
|
extend ::Gitlab::Utils::Override
|
2018-03-05 03:25:02 -05:00
|
|
|
include Backup::Helper
|
|
|
|
|
2020-09-14 08:09:34 -04:00
|
|
|
DEFAULT_EXCLUDE = 'lost+found'
|
2015-10-06 08:43:19 -04:00
|
|
|
|
2022-04-08 17:09:52 -04:00
|
|
|
attr_reader :excludes
|
2022-03-14 23:08:45 -04:00
|
|
|
|
2022-04-08 17:09:52 -04:00
|
|
|
def initialize(progress, app_files_dir, excludes: [])
|
2022-03-14 23:08:45 -04:00
|
|
|
super(progress)
|
2020-09-14 08:09:34 -04:00
|
|
|
|
2022-02-23 01:18:49 -05:00
|
|
|
@app_files_dir = app_files_dir
|
2020-09-14 08:09:34 -04:00
|
|
|
@excludes = [DEFAULT_EXCLUDE].concat(excludes)
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Copy files from public/files to backup/files
|
2022-03-14 23:08:45 -04:00
|
|
|
override :dump
|
2022-03-31 08:08:17 -04:00
|
|
|
def dump(backup_tarball, backup_id)
|
2015-10-06 08:43:19 -04:00
|
|
|
FileUtils.mkdir_p(Gitlab.config.backup.path)
|
2015-10-06 09:10:13 -04:00
|
|
|
FileUtils.rm_f(backup_tarball)
|
2017-01-23 17:42:34 -05:00
|
|
|
|
|
|
|
if ENV['STRATEGY'] == 'copy'
|
2022-02-23 01:18:49 -05:00
|
|
|
cmd = [%w[rsync -a --delete], exclude_dirs(:rsync), %W[#{app_files_realpath} #{Gitlab.config.backup.path}]].flatten
|
2017-01-23 17:42:34 -05:00
|
|
|
output, status = Gitlab::Popen.popen(cmd)
|
|
|
|
|
2020-12-13 19:10:01 -05:00
|
|
|
# Retry if rsync source files vanish
|
|
|
|
if status == 24
|
|
|
|
$stdout.puts "Warning: files vanished during rsync, retrying..."
|
|
|
|
output, status = Gitlab::Popen.popen(cmd)
|
|
|
|
end
|
|
|
|
|
2020-08-05 11:09:59 -04:00
|
|
|
unless status == 0
|
2017-01-23 17:42:34 -05:00
|
|
|
puts output
|
2022-03-14 23:08:45 -04:00
|
|
|
raise_custom_error(backup_tarball)
|
2017-01-23 17:42:34 -05:00
|
|
|
end
|
|
|
|
|
2022-02-23 01:18:49 -05:00
|
|
|
tar_cmd = [tar, exclude_dirs(:tar), %W[-C #{backup_files_realpath} -cf - .]].flatten
|
2020-10-29 05:08:38 -04:00
|
|
|
status_list, output = run_pipeline!([tar_cmd, gzip_cmd], out: [backup_tarball, 'w', 0600])
|
2022-02-23 01:18:49 -05:00
|
|
|
FileUtils.rm_rf(backup_files_realpath)
|
2017-01-23 17:42:34 -05:00
|
|
|
else
|
2022-02-23 01:18:49 -05:00
|
|
|
tar_cmd = [tar, exclude_dirs(:tar), %W[-C #{app_files_realpath} -cf - .]].flatten
|
2020-10-29 05:08:38 -04:00
|
|
|
status_list, output = run_pipeline!([tar_cmd, gzip_cmd], out: [backup_tarball, 'w', 0600])
|
|
|
|
end
|
|
|
|
|
|
|
|
unless pipeline_succeeded?(tar_status: status_list[0], gzip_status: status_list[1], output: output)
|
2022-03-14 23:08:45 -04:00
|
|
|
raise_custom_error(backup_tarball)
|
2017-01-23 17:42:34 -05:00
|
|
|
end
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
|
2022-03-14 23:08:45 -04:00
|
|
|
override :restore
|
|
|
|
def restore(backup_tarball)
|
2022-04-08 17:09:52 -04:00
|
|
|
backup_existing_files_dir(backup_tarball)
|
2015-10-06 08:43:19 -04:00
|
|
|
|
2022-02-23 01:18:49 -05:00
|
|
|
cmd_list = [%w[gzip -cd], %W[#{tar} --unlink-first --recursive-unlink -C #{app_files_realpath} -xf -]]
|
2020-10-29 05:08:38 -04:00
|
|
|
status_list, output = run_pipeline!(cmd_list, in: backup_tarball)
|
|
|
|
unless pipeline_succeeded?(gzip_status: status_list[0], tar_status: status_list[1], output: output)
|
|
|
|
raise Backup::Error, "Restore operation failed: #{output}"
|
|
|
|
end
|
2018-06-05 11:51:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tar
|
2018-06-06 05:49:34 -04:00
|
|
|
if system(*%w[gtar --version], out: '/dev/null')
|
|
|
|
# It looks like we can get GNU tar by running 'gtar'
|
|
|
|
'gtar'
|
|
|
|
else
|
|
|
|
'tar'
|
|
|
|
end
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
|
2022-04-08 17:09:52 -04:00
|
|
|
def backup_existing_files_dir(backup_tarball)
|
|
|
|
name = File.basename(backup_tarball, '.tar.gz')
|
|
|
|
|
2018-03-05 03:25:02 -05:00
|
|
|
timestamped_files_path = File.join(Gitlab.config.backup.path, "tmp", "#{name}.#{Time.now.to_i}")
|
2022-02-23 01:18:49 -05:00
|
|
|
if File.exist?(app_files_realpath)
|
2018-03-05 03:25:02 -05:00
|
|
|
# Move all files in the existing repos directory except . and .. to
|
2022-04-08 17:09:52 -04:00
|
|
|
# repositories.<timestamp> directory
|
2018-03-05 03:25:02 -05:00
|
|
|
FileUtils.mkdir_p(timestamped_files_path, mode: 0700)
|
2022-02-23 01:18:49 -05:00
|
|
|
files = Dir.glob(File.join(app_files_realpath, "*"), File::FNM_DOTMATCH) - [File.join(app_files_realpath, "."), File.join(app_files_realpath, "..")]
|
2018-03-05 03:25:02 -05:00
|
|
|
begin
|
|
|
|
FileUtils.mv(files, timestamped_files_path)
|
|
|
|
rescue Errno::EACCES
|
2022-02-23 01:18:49 -05:00
|
|
|
access_denied_error(app_files_realpath)
|
2018-04-23 05:40:55 -04:00
|
|
|
rescue Errno::EBUSY
|
2022-02-23 01:18:49 -05:00
|
|
|
resource_busy_error(app_files_realpath)
|
2018-03-05 03:25:02 -05:00
|
|
|
end
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-22 13:18:40 -05:00
|
|
|
def run_pipeline!(cmd_list, options = {})
|
2018-12-21 15:55:26 -05:00
|
|
|
err_r, err_w = IO.pipe
|
|
|
|
options[:err] = err_w
|
2020-10-29 05:08:38 -04:00
|
|
|
status_list = Open3.pipeline(*cmd_list, options)
|
2019-01-09 11:44:26 -05:00
|
|
|
err_w.close
|
2019-01-15 12:51:41 -05:00
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
[status_list, err_r.read]
|
|
|
|
end
|
|
|
|
|
|
|
|
def noncritical_warning?(warning)
|
|
|
|
noncritical_warnings = [
|
|
|
|
/^g?tar: \.: Cannot mkdir: No such file or directory$/
|
|
|
|
]
|
|
|
|
|
|
|
|
noncritical_warnings.map { |w| warning =~ w }.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_succeeded?(tar_status:, gzip_status:, output:)
|
|
|
|
return false unless gzip_status&.success?
|
|
|
|
|
|
|
|
tar_status&.success? || tar_ignore_non_success?(tar_status.exitstatus, output)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tar_ignore_non_success?(exitstatus, output)
|
|
|
|
# tar can exit with nonzero code:
|
|
|
|
# 1 - if some files changed (i.e. a CI job is currently writes to log)
|
|
|
|
# 2 - if it cannot create `.` directory (see issue https://gitlab.com/gitlab-org/gitlab/-/issues/22442)
|
|
|
|
# http://www.gnu.org/software/tar/manual/html_section/tar_19.html#Synopsis
|
|
|
|
# so check tar status 1 or stderr output against some non-critical warnings
|
|
|
|
if exitstatus == 1
|
|
|
|
$stdout.puts "Ignoring tar exit status 1 'Some files differ': #{output}"
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# allow tar to fail with other non-success status if output contain non-critical warning
|
|
|
|
if noncritical_warning?(output)
|
|
|
|
$stdout.puts "Ignoring non-success exit status #{exitstatus} due to output of non-critical warning(s): #{output}"
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
2020-09-14 08:09:34 -04:00
|
|
|
|
|
|
|
def exclude_dirs(fmt)
|
|
|
|
excludes.map do |s|
|
|
|
|
if s == DEFAULT_EXCLUDE
|
|
|
|
'--exclude=' + s
|
|
|
|
elsif fmt == :rsync
|
2022-02-23 01:18:49 -05:00
|
|
|
'--exclude=/' + File.join(File.basename(app_files_realpath), s)
|
2020-09-14 08:09:34 -04:00
|
|
|
elsif fmt == :tar
|
|
|
|
'--exclude=./' + s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-06 13:13:45 -05:00
|
|
|
|
2022-03-14 23:08:45 -04:00
|
|
|
def raise_custom_error(backup_tarball)
|
2022-02-23 01:18:49 -05:00
|
|
|
raise FileBackupError.new(app_files_realpath, backup_tarball)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def app_files_realpath
|
|
|
|
@app_files_realpath ||= File.realpath(@app_files_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def backup_files_realpath
|
|
|
|
@backup_files_realpath ||= File.join(Gitlab.config.backup.path, File.basename(@app_files_dir) )
|
2022-01-06 13:13:45 -05:00
|
|
|
end
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
end
|