Class: Bard::Backup::Finder
- Inherits:
-
Object
- Object
- Bard::Backup::Finder
- Defined in:
- lib/bard/backup/finder.rb
Overview
Locates database backups across the configured destinations and selects one by timestamp.
Instance Method Summary collapse
- #all ⇒ Object
- #find(at: nil) ⇒ Object
-
#initialize(destinations = nil) ⇒ Finder
constructor
A new instance of Finder.
- #latest ⇒ Object
- #timestamps ⇒ Object
Constructor Details
#initialize(destinations = nil) ⇒ Finder
Returns a new instance of Finder.
11 12 13 |
# File 'lib/bard/backup/finder.rb', line 11 def initialize(destinations = nil) @destinations = Destination.resolve(destinations) end |
Instance Method Details
#all ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/bard/backup/finder.rb', line 15 def all @destinations.flat_map do |destination| destination.s3_tree.list_objects.keys.filter_map do |filename| = (filename) next unless { timestamp:, destination:, filename: } end end end |
#find(at: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bard/backup/finder.rb', line 29 def find(at: nil) backups = all raise NotFound, "No backups found" if backups.empty? if at.nil? backups.max_by { |backup| backup[:timestamp] } else # returns nearest backup (exact timestamp match is not guaranteed) target = at.is_a?(Time) ? at : Time.parse(at.to_s) backups.min_by { |backup| (backup[:timestamp] - target).abs } end end |
#latest ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bard/backup/finder.rb', line 42 def latest backups = all raise NotFound, "No backups found" if backups.empty? newest = backups.max_by { |backup| backup[:timestamp] } Bard::Backup.new( timestamp: newest[:timestamp], size: file_size(newest[:destination].s3_tree, newest[:filename]), destinations: backups .select { |backup| backup[:timestamp] == newest[:timestamp] } .map { |backup| backup[:destination].info }, ) end |
#timestamps ⇒ Object
25 26 27 |
# File 'lib/bard/backup/finder.rb', line 25 def all.map { |backup| backup[:timestamp] }.uniq.sort end |