2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-26 14:37:03 +05:30
|
|
|
require 'open3'
|
2018-05-09 12:01:36 +05:30
|
|
|
require_relative 'helper'
|
2015-11-26 14:37:03 +05:30
|
|
|
|
|
|
|
module Backup
|
|
|
|
class Files
|
2018-05-09 12:01:36 +05:30
|
|
|
include Backup::Helper
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
DEFAULT_EXCLUDE = 'lost+found'
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
attr_reader :name, :app_files_dir, :backup_tarball, :excludes, :files_parent_dir
|
|
|
|
|
|
|
|
def initialize(name, app_files_dir, excludes: [])
|
2015-11-26 14:37:03 +05:30
|
|
|
@name = name
|
|
|
|
@app_files_dir = File.realpath(app_files_dir)
|
|
|
|
@files_parent_dir = File.realpath(File.join(@app_files_dir, '..'))
|
2017-08-17 22:00:37 +05:30
|
|
|
@backup_files_dir = File.join(Gitlab.config.backup.path, File.basename(@app_files_dir) )
|
2015-11-26 14:37:03 +05:30
|
|
|
@backup_tarball = File.join(Gitlab.config.backup.path, name + '.tar.gz')
|
2020-11-24 15:15:51 +05:30
|
|
|
@excludes = [DEFAULT_EXCLUDE].concat(excludes)
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Copy files from public/files to backup/files
|
|
|
|
def dump
|
|
|
|
FileUtils.mkdir_p(Gitlab.config.backup.path)
|
|
|
|
FileUtils.rm_f(backup_tarball)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
if ENV['STRATEGY'] == 'copy'
|
2020-11-24 15:15:51 +05:30
|
|
|
cmd = [%w(rsync -a), exclude_dirs(:rsync), %W(#{app_files_dir} #{Gitlab.config.backup.path})].flatten
|
2017-08-17 22:00:37 +05:30
|
|
|
output, status = Gitlab::Popen.popen(cmd)
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
unless status == 0
|
2017-08-17 22:00:37 +05:30
|
|
|
puts output
|
2018-11-08 19:23:39 +05:30
|
|
|
raise Backup::Error, 'Backup failed'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
tar_cmd = [tar, exclude_dirs(:tar), %W(-C #{@backup_files_dir} -cf - .)].flatten
|
|
|
|
run_pipeline!([tar_cmd, gzip_cmd], out: [backup_tarball, 'w', 0600])
|
2017-08-17 22:00:37 +05:30
|
|
|
FileUtils.rm_rf(@backup_files_dir)
|
|
|
|
else
|
2020-11-24 15:15:51 +05:30
|
|
|
tar_cmd = [tar, exclude_dirs(:tar), %W(-C #{app_files_dir} -cf - .)].flatten
|
|
|
|
run_pipeline!([tar_cmd, gzip_cmd], out: [backup_tarball, 'w', 0600])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
|
|
|
backup_existing_files_dir
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
run_pipeline!([%w(gzip -cd), %W(#{tar} --unlink-first --recursive-unlink -C #{app_files_dir} -xf -)], in: backup_tarball)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tar
|
|
|
|
if system(*%w[gtar --version], out: '/dev/null')
|
|
|
|
# It looks like we can get GNU tar by running 'gtar'
|
|
|
|
'gtar'
|
|
|
|
else
|
|
|
|
'tar'
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def backup_existing_files_dir
|
2018-05-09 12:01:36 +05:30
|
|
|
timestamped_files_path = File.join(Gitlab.config.backup.path, "tmp", "#{name}.#{Time.now.to_i}")
|
2016-09-13 17:45:13 +05:30
|
|
|
if File.exist?(app_files_dir)
|
2018-05-09 12:01:36 +05:30
|
|
|
# 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-10-15 14:42:47 +05:30
|
|
|
rescue Errno::EBUSY
|
|
|
|
resource_busy_error(app_files_dir)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def run_pipeline!(cmd_list, options = {})
|
2019-03-02 22:35:43 +05:30
|
|
|
err_r, err_w = IO.pipe
|
|
|
|
options[:err] = err_w
|
|
|
|
status = Open3.pipeline(*cmd_list, options)
|
|
|
|
err_w.close
|
|
|
|
return if status.compact.all?(&:success?)
|
|
|
|
|
|
|
|
regex = /^g?tar: \.: Cannot mkdir: No such file or directory$/
|
2020-06-23 00:09:42 +05:30
|
|
|
error = err_r.read
|
|
|
|
raise Backup::Error, "Backup failed. #{error}" unless error =~ regex
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def exclude_dirs(fmt)
|
|
|
|
excludes.map do |s|
|
|
|
|
if s == DEFAULT_EXCLUDE
|
|
|
|
'--exclude=' + s
|
|
|
|
elsif fmt == :rsync
|
|
|
|
'--exclude=/' + s
|
|
|
|
elsif fmt == :tar
|
|
|
|
'--exclude=./' + s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
end
|