Class: Pgchief::Database::Backups

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pgchief/database/backups.rb

Overview

Get a list of all backups for a given database

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, remote:) ⇒ Backups

Returns a new instance of Backups.



19
20
21
22
# File 'lib/pgchief/database/backups.rb', line 19

def initialize(database, remote:)
  @database = database
  @remote = remote
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



17
18
19
# File 'lib/pgchief/database/backups.rb', line 17

def database
  @database
end

#remoteObject (readonly)

Returns the value of attribute remote.



17
18
19
# File 'lib/pgchief/database/backups.rb', line 17

def remote
  @remote
end

Class Method Details

.for(database, remote:) ⇒ Object



13
14
15
# File 'lib/pgchief/database/backups.rb', line 13

def self.for(database, remote:)
  new(database, remote: remote).for
end

Instance Method Details

#forObject



24
25
26
# File 'lib/pgchief/database/backups.rb', line 24

def for
  remote ? remote_backups : local_backups
end

#local_backupsObject



40
41
42
43
44
45
46
# File 'lib/pgchief/database/backups.rb', line 40

def local_backups
  Dir["#{Pgchief::Config.backup_dir}#{database}-*.dump"]
    .sort_by { |f| File.mtime(f) }
    .reverse
    .last(3)
    .map { |f| File.basename(f) }
end

#remote_backupsObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pgchief/database/backups.rb', line 28

def remote_backups
  @remote_backups ||= client.list_objects(
    bucket: bucket,
    prefix: "#{path}#{database}-"
  ).contents
                            .map(&:key)
                            .sort
                            .last(3)
                            .reverse
                            .map { |f| File.basename(f) }
end