Class: Bard::Backup::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/bard/backup/finder.rb

Overview

Locates database backups across the configured destinations and selects one by timestamp.

Instance Method Summary collapse

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

#allObject



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|
      timestamp = parse_timestamp(filename)
      next unless timestamp
      { timestamp:, destination:, filename: }
    end
  end
end

#find(at: nil) ⇒ Object

Raises:



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

#latestObject

Raises:



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

#timestampsObject



25
26
27
# File 'lib/bard/backup/finder.rb', line 25

def timestamps
  all.map { |backup| backup[:timestamp] }.uniq.sort
end