PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/backup/files.rb

https://gitlab.com/vicvega/gitlab-ce
Ruby | 40 lines | 32 code | 7 blank | 1 comment | 1 complexity | 346a5e9c689ec6068abc5281545ee068 MD5 | raw file
 1require 'open3'
 2
 3module Backup
 4  class Files
 5    attr_reader :name, :app_files_dir, :backup_tarball, :files_parent_dir
 6
 7    def initialize(name, app_files_dir)
 8      @name = name
 9      @app_files_dir = File.realpath(app_files_dir)
10      @files_parent_dir = File.realpath(File.join(@app_files_dir, '..'))
11      @backup_tarball = File.join(Gitlab.config.backup.path, name + '.tar.gz')
12    end
13
14    # Copy files from public/files to backup/files
15    def dump
16      FileUtils.mkdir_p(Gitlab.config.backup.path)
17      FileUtils.rm_f(backup_tarball)
18      run_pipeline!([%W(tar -C #{app_files_dir} -cf - .), %W(gzip -c -1)], out: [backup_tarball, 'w', 0600])
19    end
20
21    def restore
22      backup_existing_files_dir
23      create_files_dir
24
25      run_pipeline!([%W(gzip -cd), %W(tar -C #{app_files_dir} -xf -)], in: backup_tarball)
26    end
27
28    def backup_existing_files_dir
29      timestamped_files_path = File.join(files_parent_dir, "#{name}.#{Time.now.to_i}")
30      if File.exist?(app_files_dir)
31        FileUtils.mv(app_files_dir, File.expand_path(timestamped_files_path))
32      end
33    end
34
35    def run_pipeline!(cmd_list, options={})
36      status_list = Open3.pipeline(*cmd_list, options)
37      abort 'Backup failed' unless status_list.compact.all?(&:success?)
38    end
39  end
40end