summaryrefslogtreecommitdiff
path: root/lib/backup/files.rb
diff options
context:
space:
mode:
authorJacob Vosmaer <contact@jacobvosmaer.nl>2015-10-06 14:43:19 +0200
committerJacob Vosmaer <contact@jacobvosmaer.nl>2015-10-06 14:43:19 +0200
commitd3734fbd89c069d35856b440f12109af8a7ef9c9 (patch)
tree1d9489c8291a577b0d0d6955cbd9c7a7c9f274e4 /lib/backup/files.rb
parenta2af080a06b3439d37258d88ac6d6db9ef51c6a5 (diff)
downloadgitlab-ce-d3734fbd89c069d35856b440f12109af8a7ef9c9.tar.gz
Use tar for intermediate backup storage
During the backup we create an intermediate copy of two directories: builds and uploads. Instead of creating many small files with 'cp -r', we now use tar (and fast gzip) to create single intermediate files. This saves on disk IO and disk space while creating a backup.
Diffstat (limited to 'lib/backup/files.rb')
-rw-r--r--lib/backup/files.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/backup/files.rb b/lib/backup/files.rb
new file mode 100644
index 00000000000..d0a6e8f27be
--- /dev/null
+++ b/lib/backup/files.rb
@@ -0,0 +1,39 @@
+require 'open3'
+
+module Backup
+ class Files
+ attr_reader :name, :app_files_dir, :backup_tarball, :backup_dir, :files_parent_dir
+
+ def initialize(app_files_dir)
+ @app_files_dir = File.realpath(app_files_dir)
+ @name = File.basename(app_files_dir)
+ @files_parent_dir = File.realpath(File.join(@app_files_dir, '..'))
+ @backup_dir = Gitlab.config.backup.path
+ @backup_tarball = File.join(@backup_dir, name + '.tar.gz')
+ end
+
+ # Copy files from public/files to backup/files
+ def dump
+ FileUtils.mkdir_p(Gitlab.config.backup.path)
+ run_pipeline!([%W(tar -C #{files_parent_dir} -cf - #{name}), %W(gzip -c -1)], out: [backup_tarball, 'w', 0600])
+ end
+
+ def restore
+ backup_existing_files_dir
+
+ run_pipeline!([%W(gzip -cd), %W(tar -C #{files_parent_dir} -xf -)], in: backup_tarball)
+ end
+
+ def backup_existing_files_dir
+ timestamped_files_path = File.join(files_parent_dir, "#{name}.#{Time.now.to_i}")
+ if File.exists?(app_files_dir)
+ 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