Class: Backup::Files
Constant Summary collapse
- DEFAULT_EXCLUDE =
'lost+found'
Instance Attribute Summary collapse
-
#app_files_dir ⇒ Object
readonly
Returns the value of attribute app_files_dir.
-
#backup_tarball ⇒ Object
readonly
Returns the value of attribute backup_tarball.
-
#excludes ⇒ Object
readonly
Returns the value of attribute excludes.
-
#files_parent_dir ⇒ Object
readonly
Returns the value of attribute files_parent_dir.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #backup_existing_files_dir ⇒ Object
-
#dump ⇒ Object
Copy files from public/files to backup/files.
- #exclude_dirs(fmt) ⇒ Object
-
#initialize(name, app_files_dir, excludes: []) ⇒ Files
constructor
A new instance of Files.
- #restore ⇒ Object
- #run_pipeline!(cmd_list, options = {}) ⇒ Object
- #tar ⇒ Object
Methods included from Helper
#access_denied_error, #gzip_cmd, #resource_busy_error
Constructor Details
#initialize(name, app_files_dir, excludes: []) ⇒ Files
Returns a new instance of Files.
14 15 16 17 18 19 20 21 |
# File 'lib/backup/files.rb', line 14 def initialize(name, app_files_dir, excludes: []) @name = name @app_files_dir = File.realpath(app_files_dir) @files_parent_dir = File.realpath(File.join(@app_files_dir, '..')) @backup_files_dir = File.join(Gitlab.config.backup.path, File.basename(@app_files_dir) ) @backup_tarball = File.join(Gitlab.config.backup.path, name + '.tar.gz') @excludes = [DEFAULT_EXCLUDE].concat(excludes) end |
Instance Attribute Details
#app_files_dir ⇒ Object (readonly)
Returns the value of attribute app_files_dir
12 13 14 |
# File 'lib/backup/files.rb', line 12 def app_files_dir @app_files_dir end |
#backup_tarball ⇒ Object (readonly)
Returns the value of attribute backup_tarball
12 13 14 |
# File 'lib/backup/files.rb', line 12 def backup_tarball @backup_tarball end |
#excludes ⇒ Object (readonly)
Returns the value of attribute excludes
12 13 14 |
# File 'lib/backup/files.rb', line 12 def excludes @excludes end |
#files_parent_dir ⇒ Object (readonly)
Returns the value of attribute files_parent_dir
12 13 14 |
# File 'lib/backup/files.rb', line 12 def files_parent_dir @files_parent_dir end |
#name ⇒ Object (readonly)
Returns the value of attribute name
12 13 14 |
# File 'lib/backup/files.rb', line 12 def name @name end |
Instance Method Details
#backup_existing_files_dir ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/backup/files.rb', line 61 def backup_existing_files_dir = File.join(Gitlab.config.backup.path, "tmp", "#{name}.#{Time.now.to_i}") if File.exist?(app_files_dir) # Move all files in the existing repos directory except . and .. to # repositories.old.<timestamp> directory FileUtils.mkdir_p(, 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, ) rescue Errno::EACCES access_denied_error(app_files_dir) rescue Errno::EBUSY resource_busy_error(app_files_dir) end end end |
#dump ⇒ Object
Copy files from public/files to backup/files
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/backup/files.rb', line 24 def dump FileUtils.mkdir_p(Gitlab.config.backup.path) FileUtils.rm_f(backup_tarball) if ENV['STRATEGY'] == 'copy' cmd = [%w(rsync -a), exclude_dirs(:rsync), %W(#{app_files_dir} #{Gitlab.config.backup.path})].flatten output, status = Gitlab::Popen.popen(cmd) unless status == 0 puts output raise Backup::Error, 'Backup failed' end tar_cmd = [tar, exclude_dirs(:tar), %W(-C #{@backup_files_dir} -cf - .)].flatten run_pipeline!([tar_cmd, gzip_cmd], out: [backup_tarball, 'w', 0600]) FileUtils.rm_rf(@backup_files_dir) else tar_cmd = [tar, exclude_dirs(:tar), %W(-C #{app_files_dir} -cf - .)].flatten run_pipeline!([tar_cmd, gzip_cmd], out: [backup_tarball, 'w', 0600]) end end |
#exclude_dirs(fmt) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/backup/files.rb', line 90 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 |
#restore ⇒ Object
46 47 48 49 50 |
# File 'lib/backup/files.rb', line 46 def restore backup_existing_files_dir run_pipeline!([%w(gzip -cd), %W(#{tar} --unlink-first --recursive-unlink -C #{app_files_dir} -xf -)], in: backup_tarball) end |
#run_pipeline!(cmd_list, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/backup/files.rb', line 78 def run_pipeline!(cmd_list, = {}) err_r, err_w = IO.pipe [:err] = err_w status = Open3.pipeline(*cmd_list, ) err_w.close return if status.compact.all?(&:success?) regex = /^g?tar: \.: Cannot mkdir: No such file or directory$/ error = err_r.read raise Backup::Error, "Backup failed. #{error}" unless error =~ regex end |
#tar ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/backup/files.rb', line 52 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 end |