bard-backup 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78ccac53e890df00e494ba9fb1d67ef861bb53d234e4848494b3b6d2392c5689
4
- data.tar.gz: 566ff7ef397fd6ad36c3f508a666fd0037b45eafe02d0a6956c44828bab486b3
3
+ metadata.gz: 30f8ca36634a8ee2f414e6aa238a27227d71b9033904b19ff55c649dd8bfe71b
4
+ data.tar.gz: 2c858195ad3e2e23d84cbce7be7d99ab0000fec2d39f2cd1d409774987067f2d
5
5
  SHA512:
6
- metadata.gz: 1088cbdcb2c7de2fc108731489ad567b2be2abc7fd59bd9b3edbff2fd6f4c5f043e1a6b59863fc9b8c54f472eb56b62e2841be7e80228d787267ff8a9588e69a
7
- data.tar.gz: 5d0d7e3334ceb562e766da8fa9c736e0800d56b69e74b1b333fa5ffccfb21a4333ef9dd9ae0072f262de3c6cba3d64b2c956032cad4f1490bc5a06099ecc8bc1
6
+ metadata.gz: 44346cf310237c1d24c60733328dff6ac819ce050a45a183af50b269e61d9ebef420af721e961e612d5fff5d083d53c70dbf8b214b1443d9eee265df6f5b25ba
7
+ data.tar.gz: 5bcf351a8697340af6e2388b42cf513a7dab8b76da456c0d4ba659dd846e32455eb447148cf77006dd32dd4992a4100ffd6b0f9b7662efe55ac633cf544fd3cf
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Bard::Backup does 3 things in a bard project
4
4
  1. Takes a database dump and uploads it to our s3 bucket
5
- 2. Deletes old backups using a backoff heuristic: 48 hours, 60 days, 48 months, then yearly
5
+ 2. Deletes old backups using a backoff heuristic: 72 hours, 60 days, 52 weeks, 48 months, then yearly
6
6
  3. Raises an error if we don't have a backup from the previous hour
7
7
 
8
8
  ## Installation
@@ -1,13 +1,10 @@
1
- require "backhoe"
2
- require "bard/backup/s3_dir"
3
-
4
1
  module Bard
5
2
  module Backup
6
- class Controller < Struct.new(:s3_path, :filename, :access_key, :secret_key)
3
+ class Controller < Struct.new(:dumper, :s3_dir, :now)
7
4
  def call
5
+ filename = "#{now.iso8601}.sql.gz"
8
6
  path = "/tmp/#{filename}"
9
- Backhoe.dump path
10
- s3_dir = S3Dir.new(path: s3_path, access_key:, secret_key:)
7
+ dumper.dump path
11
8
  s3_dir.put path
12
9
  end
13
10
  end
@@ -0,0 +1,41 @@
1
+ require "active_support"
2
+ require "active_support/core_ext/date_time/calculations"
3
+ require "active_support/core_ext/integer/time"
4
+
5
+ module Bard
6
+ module Backup
7
+ class Deleter < Struct.new(:s3_dir, :now)
8
+ def call
9
+ s3_dir.delete keys_to_delete
10
+ end
11
+
12
+ def keys_to_delete
13
+ s3_dir.keys.select do |key|
14
+ [
15
+ Filter.new(now, 72, :hours),
16
+ Filter.new(now, 60, :days),
17
+ Filter.new(now, 52, :weeks),
18
+ Filter.new(now, 48, :months),
19
+ Filter.new(now, 100, :years),
20
+ ].all? { |filter| !filter.cover?(key) }
21
+ end
22
+ end
23
+
24
+ class Filter < Struct.new(:now, :limit, :unit)
25
+ def cover? key
26
+ remote = DateTime.parse(key).beginning_of_hour
27
+ limit.times.any? do |count|
28
+ remote == ago(count)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def ago count
35
+ now.send(:"beginning_of_#{unit.to_s.singularize}") - count.send(unit)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -21,16 +21,20 @@ module Bard
21
21
  })
22
22
  end
23
23
 
24
+ def delete keys
25
+ objects_to_delete = Array(keys).map { |key| { key: key } }
26
+ client.delete_objects({
27
+ bucket: bucket_name,
28
+ delete: {
29
+ objects: objects_to_delete,
30
+ quiet: true,
31
+ }
32
+ })
33
+ end
34
+
24
35
  def empty!
25
36
  keys.each_slice(1000) do |key_batch|
26
- objects_to_delete = key_batch.map { |key| { key: key } }
27
- client.delete_objects({
28
- bucket: bucket_name,
29
- delete: {
30
- objects: objects_to_delete,
31
- quiet: true,
32
- }
33
- })
37
+ delete key_batch
34
38
  end
35
39
  end
36
40
 
@@ -1,6 +1,6 @@
1
1
  module Bard
2
2
  module Backup
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
6
6
 
data/lib/bard/backup.rb CHANGED
@@ -1,9 +1,15 @@
1
1
  require "bard/backup/controller"
2
+ require "bard/backup/deleter"
3
+ require "bard/backup/s3_dir"
4
+ require "backhoe"
2
5
 
3
6
  module Bard
4
7
  module Backup
5
- def self.call s3_path, access_key:, secret_key:, filename: "#{Time.now.utc.iso8601}.sql.gz"
6
- Controller.new(s3_path, filename, access_key, secret_key).call
8
+ def self.call s3_path, access_key:, secret_key:, now: Time.now.utc
9
+ dumper = Backhoe
10
+ s3_dir = S3Dir.new(path: s3_path, access_key:, secret_key:)
11
+ Controller.new(dumper, s3_dir, now).call
12
+ Deleter.new(s3_dir, now).call
7
13
  end
8
14
  end
9
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bard-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-23 00:00:00.000000000 Z
11
+ date: 2024-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backhoe
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  - micah@botandrose.com
@@ -68,6 +82,7 @@ files:
68
82
  - lib/bard-backup.rb
69
83
  - lib/bard/backup.rb
70
84
  - lib/bard/backup/controller.rb
85
+ - lib/bard/backup/deleter.rb
71
86
  - lib/bard/backup/s3_dir.rb
72
87
  - lib/bard/backup/version.rb
73
88
  - sig/bard/backup.rbs