Class: Backup::Archive
- Inherits:
-
Object
- Object
- Backup::Archive
- Includes:
- Utilities::Helpers
- Defined in:
- lib/backup/archive.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(model, name, &block) ⇒ Archive
constructor
Adds a new Archive to a Backup Model.
- #perform! ⇒ Object
Constructor Details
#initialize(model, name, &block) ⇒ Archive
Adds a new Archive to a Backup Model.
Backup::Model.new(:my_backup, 'My Backup') do
archive :my_archive do |archive|
archive.add 'path/to/archive'
archive.add '/another/path/to/archive'
archive.exclude 'path/to/exclude'
archive.exclude '/another/path/to/exclude'
end
end
All paths added using add or exclude will be expanded to their full paths from the root of the filesystem. Files will be added to the tar archive using these full paths, and their leading / will be preserved (using tar’s -P option).
/path/to/pwd/path/to/archive/...
/another/path/to/archive/...
When a root path is given, paths to add/exclude are taken as relative to the root path, unless given as absolute paths.
Backup::Model.new(:my_backup, 'My Backup') do
archive :my_archive do |archive|
archive.root '~/my_data'
archive.add 'path/to/archive'
archive.add '/another/path/to/archive'
archive.exclude 'path/to/exclude'
archive.exclude '/another/path/to/exclude'
end
end
This directs tar to change directories to the root path to create the archive. Unless paths were given as absolute, the paths within the archive will be relative to the root path.
path/to/archive/...
/another/path/to/archive/...
For absolute paths added to this archive, the leading / will be preserved. Take note that when archives are extracted, leading / are stripped by default, so care must be taken when extracting archives with mixed relative/absolute paths.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/backup/archive.rb', line 52 def initialize(model, name, &block) @model = model @name = name.to_s = { sudo: false, root: false, paths: [], excludes: [], tar_options: "" } DSL.new().instance_eval(&block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/backup/archive.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/backup/archive.rb', line 6 def end |
Instance Method Details
#perform! ⇒ Object
65 66 67 68 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 65 def perform! Logger.info "Creating Archive '#{name}'..." path = File.join(Config.tmp_path, @model.trigger, "archives") FileUtils.mkdir_p(path) pipeline = Pipeline.new with_files_from(paths_to_package) do |files_from| pipeline.add( "#{tar_command} #{tar_options} -cPf -#{tar_root} " \ "#{paths_to_exclude} #{files_from}", tar_success_codes ) extension = "tar" if @model.compressor @model.compressor.compress_with do |command, ext| pipeline << command extension << ext end end pipeline << "#{utility(:cat)} > " \ "'#{File.join(path, "#{name}.#{extension}")}'" pipeline.run end if pipeline.success? Logger.info "Archive '#{name}' Complete!" else raise Error, "Failed to Create Archive '#{name}'\n" + pipeline. end end |