Class: Backs3::FileInfo

Inherits:
Object
  • Object
show all
Includes:
Backs3
Defined in:
lib/backs3/file_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Backs3

#load_backup_info, #logger, #md5, #save_backup_info

Constructor Details

#initialize(backup, path) ⇒ FileInfo

Returns a new instance of FileInfo.



9
10
11
12
13
14
# File 'lib/backs3/file_info.rb', line 9

def initialize(backup, path)
  @backup_info = backup
  @path = path
  @md5sum = md5(@path)
  @options = @backup_info.options
end

Instance Attribute Details

#backup_infoObject (readonly)

Returns the value of attribute backup_info.



5
6
7
# File 'lib/backs3/file_info.rb', line 5

def backup_info
  @backup_info
end

#md5sumObject (readonly)

Returns the value of attribute md5sum.



7
8
9
# File 'lib/backs3/file_info.rb', line 7

def md5sum
  @md5sum
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/backs3/file_info.rb', line 6

def path
  @path
end

Instance Method Details

#==(other_obj) ⇒ Object



20
21
22
# File 'lib/backs3/file_info.rb', line 20

def ==(other_obj)
  other_obj.backup_info == self.backup_info && other_obj.path == self.path
end

#aws_filenameObject



24
25
26
# File 'lib/backs3/file_info.rb', line 24

def aws_filename
  File.join(@backup_info.key, path)
end

#backupObject



28
29
30
31
# File 'lib/backs3/file_info.rb', line 28

def backup
  logger.info "Backing up #{@path} to #{aws_filename}"
  storage.store(aws_filename, open(@path))
end

#restore(location = '/tmp') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/backs3/file_info.rb', line 33

def restore(location = '/tmp')
  restore_path = File.join(location, @path)
  
  if storage.exists?(aws_filename)
    $stdout.write "Restoring file #{@path}"
    FileUtils.mkdir_p File.dirname(restore_path)
    File.open(restore_path, 'w') do |f|
      storage.read(aws_filename) do |segment|
        $stdout.write "."
        f.write segment
      end
    end
    $stdout.write "\n"
  else
    logger.info "Could not restore #{@path} because file data could not be found!"
  end
end

#storageObject



16
17
18
# File 'lib/backs3/file_info.rb', line 16

def storage
  @backup_info.storage
end