Class: Backs3::Backup

Inherits:
Object
  • Object
show all
Includes:
Backs3, Storage
Defined in:
lib/backs3/backup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Backs3

#load_backup_info, #logger, #md5, #save_backup_info

Constructor Details

#initialize(previous, options) ⇒ Backup

Returns a new instance of Backup.



8
9
10
11
12
13
14
15
16
17
# File 'lib/backs3/backup.rb', line 8

def initialize(previous, options)
  @backups = previous.sort{|a,b| a.date <=> b.date } if previous
  @last_backup = self.backups.last
  @last_full_backup = self.backups.reverse.detect{|b| b.full == true }

  @date = Time.now.to_i
  @options = options
  @options['prefix'] ||= ''
  @full = @options['force-full'] || first_backup? || @date - @last_full_backup.date > (@options['full'] || 7).days
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#doneObject (readonly)

Returns the value of attribute done.



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

def done
  @done
end

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#fullObject (readonly)

Returns the value of attribute full.



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

def full
  @full
end

#last_backupObject (readonly)

Returns the value of attribute last_backup.



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

def last_backup
  @last_backup
end

#last_full_backupObject (readonly)

Returns the value of attribute last_full_backup.



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

def last_full_backup
  @last_full_backup
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#==(other_obj) ⇒ Object



19
20
21
# File 'lib/backs3/backup.rb', line 19

def ==(other_obj)
  other_obj.date == self.date && other_obj.full == self.full
end

#all_filesObject

All of the files for a backup. If the backup is partial this function will find the files from the last full backup to this one.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/backs3/backup.rb', line 29

def all_files
  if !full && @last_full_backup
    backups = self.backups.select{|b| b.date >= @last_full_backup.date && b.date <= self.date }
    backups << self unless backups.include?(self)

    rfiles = backups.collect{|b| b.files}.flatten.uniq
    rfiles.reject! do |first_file|
      rfiles.detect{|second_file| second_file.path == first_file.path && second_file.backup_info.date > first_file.backup_info.date }
    end
    rfiles
  else
    self.files
  end
end

#backupObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/backs3/backup.rb', line 52

def backup
  raise "Cannot backup again!" if @done

  logger.info "Backing up #{@options['folder']} in key #{self.key}"

  @files = collect_files
  @files.each do |file|
    file.backup
  end

  update_backup_info
  storage.flush

  @done = true
  logger.info "Backup finished!"
end

#backupsObject



23
24
25
# File 'lib/backs3/backup.rb', line 23

def backups
  @backups ||= load_backup_info.sort{|a,b| a.date <=> b.date } || []
end

#first_backup?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/backs3/backup.rb', line 48

def first_backup?
  @last_full_backup.nil?
end

#keyObject



44
45
46
# File 'lib/backs3/backup.rb', line 44

def key
  @options['prefix'] + @date.to_s
end

#restore(location = '/tmp', file = nil) ⇒ Object



69
70
71
72
73
74
# File 'lib/backs3/backup.rb', line 69

def restore(location = '/tmp', file = nil)
  files = file.nil? ? all_files : all_files.select{|f| f.path == file}
  files.each do |file|
    file.restore(location)
  end
end

#to_yaml_propertiesObject



76
77
78
# File 'lib/backs3/backup.rb', line 76

def to_yaml_properties
  instance_variables.reject{|i| %w(@backups).include?(i) }.sort
end