Class: BackupList
- Inherits:
-
Array
- Object
- Array
- BackupList
- Defined in:
- lib/bakman/backuplist.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
-
#folder ⇒ Object
readonly
The object you want to manage the backups has * a #name * the backups are saved in a #folder.
-
#lenght ⇒ Object
readonly
The object you want to manage the backups has * a #name * the backups are saved in a #folder.
-
#name ⇒ Object
readonly
The object you want to manage the backups has * a #name * the backups are saved in a #folder.
Instance Method Summary collapse
-
#clean_bck! ⇒ Object
A quick method to clean backups files, the one that are not to be kept ( Backup.to_keep == false ).
-
#create(folder, name) ⇒ Object
This is populating the array with #Backup objects It is sorted at the end for later manipulation on a date sorted array.
-
#keep_backup(nb_to_keep, down_date, up_date) ⇒ Object
A method to determine if a backup has to be kept or not.
-
#list ⇒ Object
A short method to print the list of backups.
-
#reverse ⇒ Object
a redefinition of the reverse method from the parent class array.
-
#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).
-
#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.
-
#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.
Instance Attribute Details
#folder ⇒ Object (readonly)
The object you want to manage the backups has
-
a #name
-
the backups are saved in a #folder
11 12 13 |
# File 'lib/bakman/backuplist.rb', line 11 def folder @folder end |
#lenght ⇒ Object (readonly)
The object you want to manage the backups has
-
a #name
-
the backups are saved in a #folder
11 12 13 |
# File 'lib/bakman/backuplist.rb', line 11 def lenght @lenght end |
#name ⇒ Object (readonly)
The object you want to manage the backups has
-
a #name
-
the backups are saved in a #folder
11 12 13 |
# File 'lib/bakman/backuplist.rb', line 11 def name @name end |
Instance Method Details
#clean_bck! ⇒ Object
A quick method to clean backups files, the one that are not to be kept ( Backup.to_keep == false )
89 90 91 92 93 94 95 |
# File 'lib/bakman/backuplist.rb', line 89 def clean_bck! self.each do |bck| if bck.to_keep == false bck.delete! end end end |
#create(folder, name) ⇒ Object
This is populating the array with #Backup objects It is sorted at the end for later manipulation on a date sorted array.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bakman/backuplist.rb', line 15 def create(folder,name) @name = name @folder = folder @lenght = 0 list = Dir.glob("#{folder}/#{name}*") list.each do |path| self << Backup.new(path) @lenght += 1 end self.sort_by! {|bck| bck.date} end |
#keep_backup(nb_to_keep, down_date, up_date) ⇒ Object
A method to determine if a backup has to be kept or not. We go thru the array in the reverse order. It allows to test the more recent entries first and keep them first. To be kept, a backup needs :
-
to have his ( #Backup.date ) between #down_date and #up_date
-
to be more recent
-
to not be already kept
It returns nb_kept for post analyses
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bakman/backuplist.rb', line 42 def keep_backup(nb_to_keep,down_date, up_date) range = down_date..up_date nb_kept = 0 self.reverse_each do |bck| if range === bck.date if nb_kept < nb_to_keep if bck.to_keep != true puts "#{bck.filepath} will be kept!" bck.to_keep = true nb_kept += 1 end end end end return nb_kept end |
#list ⇒ Object
A short method to print the list of backups
28 29 30 31 32 33 |
# File 'lib/bakman/backuplist.rb', line 28 def list puts "The list of backups for #{self.name}" self.each do |bck| puts bck.filepath end end |
#reverse ⇒ Object
a redefinition of the reverse method from the parent class array
79 80 81 82 83 84 85 |
# File 'lib/bakman/backuplist.rb', line 79 def reverse reversed = BackupList.new self.reverse_each do |bck| reversed << bck end return reversed 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)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bakman/backuplist.rb', line 62 def rotate(nb_to_keep, down_date, up_date) nb_kept = keep_backup(nb_to_keep,down_date, up_date) if nb_kept == nb_to_keep puts "There is enough backups for this time period." else puts "Not enough backups between #{down_date} and #{up_date}." if up_date == Date.today puts "Instead of #{nb_to_keep} backup(s), you will have #{nb_kept} backup(s)" else puts "We will look for a closer period to find backups if possible." self.reverse.rotate(nb_to_keep - nb_kept, up_date, Date.today) end 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/bakman/backuplist.rb', line 103 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) puts "Remove unecessary backups" self.clean_bck! 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.
123 124 125 126 |
# File 'lib/bakman/backuplist.rb', line 123 def rsync!(host, remote_folder) puts "rsync #{folder}/#{name}* #{host}:#{remote_folder}" `rsync --delete #{folder}/#{name}* #{host}:#{remote_folder}` end |