Class: TarsnapPruner::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/tarsnap_pruner/machine.rb

Overview

An instance of this class represents one machine that’s being backed up.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_file, tarsnap = nil) ⇒ Machine

Returns a new instance of Machine.



16
17
18
19
# File 'lib/tarsnap_pruner/machine.rb', line 16

def initialize(key_file, tarsnap=nil)
  @key_file = key_file
  @tarsnap = tarsnap || Tarsnap.new(@key_file)
end

Instance Attribute Details

#key_fileObject (readonly)

Returns the value of attribute key_file.



21
22
23
# File 'lib/tarsnap_pruner/machine.rb', line 21

def key_file
  @key_file
end

Class Method Details

.allObject



12
13
14
# File 'lib/tarsnap_pruner/machine.rb', line 12

def self.all
  key_files.map { |f| new(f) }
end

.key_filesObject



8
9
10
# File 'lib/tarsnap_pruner/machine.rb', line 8

def self.key_files
  Dir.glob(File.join(ENV['KEY_DIRECTORY'], '*.key')).sort
end

Instance Method Details

#archivesObject



27
28
29
# File 'lib/tarsnap_pruner/machine.rb', line 27

def archives
  @tarsnap.list_archives.split.sort.map { |name| Archive.new(name) }
end

#delete(archives_to_delete) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tarsnap_pruner/machine.rb', line 31

def delete(archives_to_delete)
  if archives_to_delete.any?
    Dir.mktmpdir("cache-", ENV['CACHES_DIRECTORY']) do |cache_directory|
      # fsck so that we have an up-to-date cache (required for pruning). This
      # means that each machine will have to fsck again, but there's apparently
      # no way around it.
      # Ref: http://mail.tarsnap.com/tarsnap-users/msg00512.html
      @tarsnap.fsck(cache_directory)
      archives_to_delete.each do |archive|
        @tarsnap.delete(archive, cache_directory)
      end
    end
  end
end

#hostnameObject



23
24
25
# File 'lib/tarsnap_pruner/machine.rb', line 23

def hostname
  key_file =~ /tarsnap-(.*)\.key$/ ? $1 : 'unknown'
end