Class: Backup::Archive

Inherits:
Object
  • Object
show all
Includes:
CLI::Helpers
Defined in:
lib/backup/archive.rb

Constant Summary

Constants included from CLI::Helpers

CLI::Helpers::UTILITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, &block) ⇒ Archive

Takes the name of the archive and the configuration block



25
26
27
28
29
30
31
32
33
# File 'lib/backup/archive.rb', line 25

def initialize(model, name, &block)
  @model    = model
  @name     = name.to_s
  @paths    = Array.new
  @excludes = Array.new
  @tar_args = ''

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#excludesObject

Stores an array of different paths/files to exclude



17
18
19
# File 'lib/backup/archive.rb', line 17

def excludes
  @excludes
end

#nameObject

Stores the name of the archive



9
10
11
# File 'lib/backup/archive.rb', line 9

def name
  @name
end

#pathsObject

Stores an array of different paths/files to store



13
14
15
# File 'lib/backup/archive.rb', line 13

def paths
  @paths
end

#tar_argsObject

String of additional arguments for the ‘tar` command



21
22
23
# File 'lib/backup/archive.rb', line 21

def tar_args
  @tar_args
end

Instance Method Details

#add(path) ⇒ Object

Adds new paths to the @paths instance variable array



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/backup/archive.rb', line 37

def add(path)
  path = File.expand_path(path)
  if File.exist?(path)
    @paths << path
  else
    Logger.warn Errors::Archive::NotFoundError.new(<<-EOS)
      The following path was not found:
      #{ path }
      This path will be omitted from the '#{ name }' Archive.
    EOS
  end
end

#exclude(path) ⇒ Object

Adds new paths to the @excludes instance variable array



52
53
54
# File 'lib/backup/archive.rb', line 52

def exclude(path)
  @excludes << File.expand_path(path)
end

#perform!Object

Archives all the provided paths in to a single .tar file and places that .tar file in the folder which later will be packaged If the model is configured with a Compressor, the tar command output will be piped through the Compressor command and the file extension will be adjusted to indicate the type of compression used.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/backup/archive.rb', line 69

def perform!
  Logger.message "#{ self.class } has started archiving:\n" +
      paths.map {|path| "  #{path}" }.join("\n")

  archive_path = File.join(Config.tmp_path, @model.trigger, 'archives')
  FileUtils.mkdir_p(archive_path)

  archive_ext = 'tar'
  pipeline = Pipeline.new

  pipeline << "#{ utility(:tar) } #{ tar_args } -cPf - " +
      "#{ paths_to_exclude } #{ paths_to_package }"

  if @model.compressor
    @model.compressor.compress_with do |command, ext|
      pipeline << command
      archive_ext << ext
    end
  end

  pipeline << "cat > '#{ File.join(archive_path, "#{name}.#{archive_ext}") }'"
  pipeline.run
  if pipeline.success?
    Logger.message "#{ self.class } Complete!"
  else
    raise Errors::Archive::PipelineError,
        "Failed to Create Backup Archive\n" +
        pipeline.error_messages
  end
end

#tar_options(options) ⇒ Object

Adds the given String of options to the ‘tar` command. e.g. ’-h –xattrs’



59
60
61
# File 'lib/backup/archive.rb', line 59

def tar_options(options)
  @tar_args = options
end