Class: Backup

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/pg_s3_dumper/backup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3_object) ⇒ Backup

Returns a new instance of Backup.



15
16
17
18
19
# File 'lib/pg_s3_dumper/backup.rb', line 15

def initialize(s3_object)
  @s3_object = s3_object
  @ts = Time.parse(s3_object.key.split('/').last)
  @keep = false
end

Instance Attribute Details

#keepObject (readonly)

Returns the value of attribute keep.



9
10
11
# File 'lib/pg_s3_dumper/backup.rb', line 9

def keep
  @keep
end

#s3_objectObject (readonly)

Returns the value of attribute s3_object.



9
10
11
# File 'lib/pg_s3_dumper/backup.rb', line 9

def s3_object
  @s3_object
end

#tsObject (readonly)

Returns the value of attribute ts.



9
10
11
# File 'lib/pg_s3_dumper/backup.rb', line 9

def ts
  @ts
end

Class Method Details

.generate_idObject



3
4
5
# File 'lib/pg_s3_dumper/backup.rb', line 3

def self.generate_id
  SecureRandom.hex(32)
end

Instance Method Details

#<=>(other) ⇒ Object



11
12
13
# File 'lib/pg_s3_dumper/backup.rb', line 11

def <=>(other)
  ts <=> other.ts
end

#basenameObject



70
71
72
# File 'lib/pg_s3_dumper/backup.rb', line 70

def basename
  File.basename(s3_object.key)
end

#deleteObject



57
58
59
60
# File 'lib/pg_s3_dumper/backup.rb', line 57

def delete
  puts "Deleting backup: #{self}"
  s3_object.delete
end

#idObject



62
63
64
# File 'lib/pg_s3_dumper/backup.rb', line 62

def id
  @id ||= s3_object.['backup_id']
end

#keep_on_day(date) ⇒ Object



37
38
39
# File 'lib/pg_s3_dumper/backup.rb', line 37

def keep_on_day(date)
  @keep = true if on_day?(date)
end

#keep_on_month(date) ⇒ Object



45
46
47
# File 'lib/pg_s3_dumper/backup.rb', line 45

def keep_on_month(date)
  @keep = true if on_month?(date)
end

#keep_on_week(date) ⇒ Object



41
42
43
# File 'lib/pg_s3_dumper/backup.rb', line 41

def keep_on_week(date)
  @keep = true if on_week?(date)
end

#on_day?(date) ⇒ Boolean

Returns:

  • (Boolean)


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

def on_day?(date)
  ts.strftime('%F') == date.strftime('%F')
end

#on_month?(date) ⇒ Boolean

Returns:

  • (Boolean)


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

def on_month?(date)
  ts.strftime('%Y-%m') == date.strftime('%Y-%m')
end

#on_week?(date) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pg_s3_dumper/backup.rb', line 29

def on_week?(date)
  ts.strftime('%Y-%U') == date.strftime('%Y-%U')
end

#pruneObject



49
50
51
52
53
54
55
# File 'lib/pg_s3_dumper/backup.rb', line 49

def prune
  if keep
    puts "Keeping backup: #{self}"
  else
    delete
  end
end

#short_idObject



66
67
68
# File 'lib/pg_s3_dumper/backup.rb', line 66

def short_id
  id[0..8]
end

#to_sObject



21
22
23
# File 'lib/pg_s3_dumper/backup.rb', line 21

def to_s
  "id: #{short_id}, key: #{basename}"
end