Class: Backup::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/files.rb

Direct Known Subclasses

Artifacts, Builds, Lfs, Uploads

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, app_files_dir) ⇒ Files

Returns a new instance of Files


7
8
9
10
11
12
# File 'lib/backup/files.rb', line 7

def initialize(name, app_files_dir)
  @name = name
  @app_files_dir = File.realpath(app_files_dir)
  @files_parent_dir = File.realpath(File.join(@app_files_dir, '..'))
  @backup_tarball = File.join(Gitlab.config.backup.path, name + '.tar.gz')
end

Instance Attribute Details

#app_files_dirObject (readonly)

Returns the value of attribute app_files_dir


5
6
7
# File 'lib/backup/files.rb', line 5

def app_files_dir
  @app_files_dir
end

#backup_tarballObject (readonly)

Returns the value of attribute backup_tarball


5
6
7
# File 'lib/backup/files.rb', line 5

def backup_tarball
  @backup_tarball
end

#files_parent_dirObject (readonly)

Returns the value of attribute files_parent_dir


5
6
7
# File 'lib/backup/files.rb', line 5

def files_parent_dir
  @files_parent_dir
end

#nameObject (readonly)

Returns the value of attribute name


5
6
7
# File 'lib/backup/files.rb', line 5

def name
  @name
end

Instance Method Details

#backup_existing_files_dirObject


28
29
30
31
32
33
# File 'lib/backup/files.rb', line 28

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

#dumpObject

Copy files from public/files to backup/files


15
16
17
18
19
# File 'lib/backup/files.rb', line 15

def dump
  FileUtils.mkdir_p(Gitlab.config.backup.path)
  FileUtils.rm_f(backup_tarball)
  run_pipeline!([%W(tar -C #{app_files_dir} -cf - .), %W(gzip -c -1)], out: [backup_tarball, 'w', 0600])
end

#restoreObject


21
22
23
24
25
26
# File 'lib/backup/files.rb', line 21

def restore
  backup_existing_files_dir
  create_files_dir

  run_pipeline!([%W(gzip -cd), %W(tar -C #{app_files_dir} -xf -)], in: backup_tarball)
end

#run_pipeline!(cmd_list, options = {}) ⇒ Object


35
36
37
38
# File 'lib/backup/files.rb', line 35

def run_pipeline!(cmd_list, options={})
  status_list = Open3.pipeline(*cmd_list, options)
  abort 'Backup failed' unless status_list.compact.all?(&:success?)
end