2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-06 08:43:19 -04:00
|
|
|
require 'open3'
|
2018-03-05 03:25:02 -05:00
|
|
|
require_relative 'helper'
|
2015-10-06 08:43:19 -04:00
|
|
|
|
|
|
|
module Backup
|
|
|
|
class Files
|
2018-03-05 03:25:02 -05:00
|
|
|
include Backup::Helper
|
|
|
|
|
2015-10-06 09:22:08 -04:00
|
|
|
attr_reader :name, :app_files_dir, :backup_tarball, :files_parent_dir
|
2015-10-06 08:43:19 -04:00
|
|
|
|
2015-10-16 11:16:17 -04:00
|
|
|
def initialize(name, app_files_dir)
|
|
|
|
@name = name
|
2015-10-06 08:43:19 -04:00
|
|
|
@app_files_dir = File.realpath(app_files_dir)
|
|
|
|
@files_parent_dir = File.realpath(File.join(@app_files_dir, '..'))
|
2017-01-23 17:42:34 -05:00
|
|
|
@backup_files_dir = File.join(Gitlab.config.backup.path, File.basename(@app_files_dir) )
|
2015-10-06 09:22:08 -04:00
|
|
|
@backup_tarball = File.join(Gitlab.config.backup.path, name + '.tar.gz')
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Copy files from public/files to backup/files
|
|
|
|
def dump
|
|
|
|
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'
|
2017-12-20 03:11:13 -05:00
|
|
|
cmd = %W(rsync -a --exclude=lost+found #{app_files_dir} #{Gitlab.config.backup.path})
|
2017-01-23 17:42:34 -05:00
|
|
|
output, status = Gitlab::Popen.popen(cmd)
|
|
|
|
|
|
|
|
unless status.zero?
|
|
|
|
puts output
|
2018-06-05 11:51:14 -04:00
|
|
|
raise Backup::Error, 'Backup failed'
|
2017-01-23 17:42:34 -05:00
|
|
|
end
|
|
|
|
|
2019-03-07 03:38:14 -05:00
|
|
|
run_pipeline!([%W(#{tar} --exclude=lost+found -C #{@backup_files_dir} -cf - .), gzip_cmd], out: [backup_tarball, 'w', 0600])
|
2017-01-23 17:42:34 -05:00
|
|
|
FileUtils.rm_rf(@backup_files_dir)
|
|
|
|
else
|
2019-03-07 03:38:14 -05:00
|
|
|
run_pipeline!([%W(#{tar} --exclude=lost+found -C #{app_files_dir} -cf - .), gzip_cmd], out: [backup_tarball, 'w', 0600])
|
2017-01-23 17:42:34 -05:00
|
|
|
end
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
|
|
|
backup_existing_files_dir
|
|
|
|
|
2018-06-05 11:51:14 -04:00
|
|
|
run_pipeline!([%w(gzip -cd), %W(#{tar} --unlink-first --recursive-unlink -C #{app_files_dir} -xf -)], in: backup_tarball)
|
|
|
|
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
|
|
|
|
|
|
|
|
def backup_existing_files_dir
|
2018-03-05 03:25:02 -05:00
|
|
|
timestamped_files_path = File.join(Gitlab.config.backup.path, "tmp", "#{name}.#{Time.now.to_i}")
|
2016-08-09 17:23:25 -04:00
|
|
|
if File.exist?(app_files_dir)
|
2018-03-05 03:25:02 -05:00
|
|
|
# Move all files in the existing repos directory except . and .. to
|
|
|
|
# repositories.old.<timestamp> directory
|
|
|
|
FileUtils.mkdir_p(timestamped_files_path, mode: 0700)
|
|
|
|
files = Dir.glob(File.join(app_files_dir, "*"), File::FNM_DOTMATCH) - [File.join(app_files_dir, "."), File.join(app_files_dir, "..")]
|
|
|
|
begin
|
|
|
|
FileUtils.mv(files, timestamped_files_path)
|
|
|
|
rescue Errno::EACCES
|
|
|
|
access_denied_error(app_files_dir)
|
2018-04-23 05:40:55 -04:00
|
|
|
rescue Errno::EBUSY
|
|
|
|
resource_busy_error(app_files_dir)
|
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
|
2019-01-16 12:11:58 -05:00
|
|
|
status = Open3.pipeline(*cmd_list, options)
|
2019-01-09 11:44:26 -05:00
|
|
|
err_w.close
|
2019-01-15 12:51:41 -05:00
|
|
|
return if status.compact.all?(&:success?)
|
|
|
|
|
|
|
|
regex = /^g?tar: \.: Cannot mkdir: No such file or directory$/
|
2020-05-22 02:08:41 -04:00
|
|
|
error = err_r.read
|
|
|
|
raise Backup::Error, "Backup failed. #{error}" unless error =~ regex
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|