Class: BackupList

Inherits:
Array
  • Object
show all
Defined in:
lib/backup_management.rb

Overview

This class handles a collection of #Backup for the same object “something”. This is useful to manipulate backups as a whole

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder, name) ⇒ BackupList

This is populating the array with #Backup objects It is sorted at the end for later manipulation on a date sorted array.



37
38
39
40
41
42
43
44
45
# File 'lib/backup_management.rb', line 37

def initialize(folder,name)
	@name = name
	@folder = folder
  list = Dir.glob("#{folder}/#{name}*")
	list.each do |path|
	  self << Backup.new(path)
	end
	self.sort_by! {|bck| bck.date}
end

Instance Attribute Details

#folderObject (readonly)

The object you want to manage the backups has

  • a #name

  • the backups are saved in a #folder



33
34
35
# File 'lib/backup_management.rb', line 33

def folder
  @folder
end

#nameObject (readonly)

The object you want to manage the backups has

  • a #name

  • the backups are saved in a #folder



33
34
35
# File 'lib/backup_management.rb', line 33

def name
  @name
end

Instance Method Details

#listObject

A short method to print the list of backups



48
49
50
51
52
53
# File 'lib/backup_management.rb', line 48

def list
	puts "The list of backups for #{self.name}"
	self.each do |bck|
	  puts bck.filepath
	end
end

#rotate!(nb_to_keep, down_date, up_date) ⇒ Object

A useful method to rotate the files

  • #nb_to_keep is the number of backups to keep betwwen the two #down_date and #up_date

(number above #number_to_keep will be deleted)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/backup_management.rb', line 58

def rotate!(nb_to_keep, down_date, up_date)
	range = down_date..up_date
  list = []
	self.each do |bck|
		list << bck if range === bck.date
	end

  if list.length == 0
    puts "No backup between #{down_date} and #{up_date}."
  elsif list.length <= nb_to_keep
    puts "There is no need to delete backups."
  else
    nb_to_delete = list.length - nb_to_keep
    puts "#{nb_to_keep} backup(s) will be kept and #{nb_to_delete} backup(s) will be deleted."
    list.pop(nb_to_keep)
    list.each {|bck| bck.delete!}
  end
end

#rotate_gfs!(nb_g, nb_f, nb_s) ⇒ Object

A method to do an automatic GrandFather Father Son rotation of the backups Here a month is 30 days

  • #nb_g is the number of GrandFather backups to keep (Monthly)

  • #nb_f is the number of Father backups to keep (weekly)

  • #nb_s is the number of son backups to keep (daily)

/!\ It checks until the prev year but not before.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/backup_management.rb', line 83

def rotate_gfs!(nb_g, nb_f, nb_s)

  puts "Rotate GrandFather"
  self.rotate!(nb_g,Date.today.prev_year, Date.today.prev_day(30))

  puts "Rotate Father"
  self.rotate!(nb_f,Date.today.prev_day(29), Date.today.prev_day(7))

  puts "Rotate Son"
  self.rotate!(nb_s,Date.today.prev_day(6),Date.today)

end

#rsync!(host, remote_folder) ⇒ Object

A method to rsync the backup folder with a remote host describe by #host #host should be username@machine the ssh_keys have to be exchanged before using this (if not, it will failed) /!\ the folder has to exist on the remote host.



99
100
101
102
# File 'lib/backup_management.rb', line 99

def rsync!(host, remote_folder)
	puts "rsync #{folder}/#{name}* #{host}:#{remote_folder}"
	`rsync #{folder}/#{name}* #{host}:#{remote_folder}`
end