2015-10-06 08:43:19 -04:00
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
module Backup
|
|
|
|
class Files
|
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, '..'))
|
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)
|
2015-10-16 11:16:17 -04:00
|
|
|
run_pipeline!([%W(tar -C #{app_files_dir} -cf - .), %W(gzip -c -1)], out: [backup_tarball, 'w', 0600])
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
|
|
|
backup_existing_files_dir
|
2015-10-16 11:16:17 -04:00
|
|
|
create_files_dir
|
2015-10-06 08:43:19 -04:00
|
|
|
|
2015-10-16 11:16:17 -04:00
|
|
|
run_pipeline!([%W(gzip -cd), %W(tar -C #{app_files_dir} -xf -)], in: backup_tarball)
|
2015-10-06 08:43:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def backup_existing_files_dir
|
|
|
|
timestamped_files_path = File.join(files_parent_dir, "#{name}.#{Time.now.to_i}")
|
2016-08-09 17:23:25 -04:00
|
|
|
if File.exist?(app_files_dir)
|
2015-10-06 08:43:19 -04:00
|
|
|
FileUtils.mv(app_files_dir, File.expand_path(timestamped_files_path))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_pipeline!(cmd_list, options={})
|
|
|
|
status_list = Open3.pipeline(*cmd_list, options)
|
|
|
|
abort 'Backup failed' unless status_list.compact.all?(&:success?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|