bard-backup 0.1.0 → 0.2.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: 55ece259f7be3aae82e05c75b510f18bb1d7f70b9d0c77b3b9f6e71a641ddf4c
4
- data.tar.gz: aed5da390d8f657fe3db65b009cde22ef0e6baf545fd4aafb2222e62258c3ad1
3
+ metadata.gz: 78ccac53e890df00e494ba9fb1d67ef861bb53d234e4848494b3b6d2392c5689
4
+ data.tar.gz: 566ff7ef397fd6ad36c3f508a666fd0037b45eafe02d0a6956c44828bab486b3
5
5
  SHA512:
6
- metadata.gz: dcc9f83eb793a5ed76446c862ce95a4b2b019eb38aa2f5c6589f5e6a516642f5cbad1292054e2f72f4a676d40f91c79b11cb6f26457fcdea565509c337a72dc2
7
- data.tar.gz: 9a61e8264d5f3e4fe0247e88aba019b9971099a4325ff523c0d2cb195b104d51d2c0a2b3b497b1a15016c9a83d7e2f97c1a2e5920686fd5da732d579689d139a
6
+ metadata.gz: 1088cbdcb2c7de2fc108731489ad567b2be2abc7fd59bd9b3edbff2fd6f4c5f043e1a6b59863fc9b8c54f472eb56b62e2841be7e80228d787267ff8a9588e69a
7
+ data.tar.gz: 5d0d7e3334ceb562e766da8fa9c736e0800d56b69e74b1b333fa5ffccfb21a4333ef9dd9ae0072f262de3c6cba3d64b2c956032cad4f1490bc5a06099ecc8bc1
@@ -0,0 +1,15 @@
1
+ require "backhoe"
2
+ require "bard/backup/s3_dir"
3
+
4
+ module Bard
5
+ module Backup
6
+ class Controller < Struct.new(:s3_path, :filename, :access_key, :secret_key)
7
+ def call
8
+ path = "/tmp/#{filename}"
9
+ Backhoe.dump path
10
+ s3_dir = S3Dir.new(path: s3_path, access_key:, secret_key:)
11
+ s3_dir.put path
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,57 @@
1
+ require "aws-sdk-s3"
2
+ require "rexml"
3
+
4
+ module Bard
5
+ module Backup
6
+ class S3Dir < Data.define(:path, :access_key, :secret_key)
7
+ def keys
8
+ response = client.list_objects_v2({
9
+ bucket: bucket_name,
10
+ prefix: folder_prefix,
11
+ })
12
+ raise if response.is_truncated
13
+ response.contents.map(&:key)
14
+ end
15
+
16
+ def put file_path, body: File.read(file_path)
17
+ client.put_object({
18
+ bucket: bucket_name,
19
+ key: "#{folder_prefix}/#{File.basename(file_path)}",
20
+ body: body,
21
+ })
22
+ end
23
+
24
+ def empty!
25
+ 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
+ })
34
+ end
35
+ end
36
+
37
+ def bucket_name
38
+ path.split("/").first
39
+ end
40
+
41
+ def folder_prefix
42
+ path.split("/")[1..].join("/")
43
+ end
44
+
45
+ private
46
+
47
+ def client
48
+ Aws::S3::Client.new({
49
+ region: "us-west-2",
50
+ access_key_id: access_key,
51
+ secret_access_key: secret_key,
52
+ })
53
+ end
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,6 @@
1
+ module Bard
2
+ module Backup
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
6
+
data/lib/bard/backup.rb CHANGED
@@ -1,91 +1,10 @@
1
- # frozen_string_literal: true
2
-
3
- require "net/http"
4
- require "openssl"
5
- require "base64"
6
- require "backhoe"
1
+ require "bard/backup/controller"
7
2
 
8
3
  module Bard
9
- class Backup < Struct.new(:s3_path, :access_key, :secret_key)
10
- def self.call s3_path, access_key:, secret_key:
11
- new(s3_path, access_key, secret_key).call
12
- end
13
-
14
- def call
15
- @time = Time.now
16
-
17
- Backhoe.dump path
18
-
19
- self.full_s3_path = "/#{s3_path}/#{filename}"
20
-
21
- if s3_path.include?("/")
22
- s3_bucket = s3_path.split("/").first
23
- self.s3_path = s3_path.split("/")[1..].join("/")
24
- uri = URI("https://#{s3_bucket}.s3.amazonaws.com/#{s3_path}/#{filename}")
25
- else
26
- uri = URI("https://#{s3_path}.s3.amazonaws.com/#{filename}")
27
- end
28
-
29
- request = Net::HTTP::Put.new(uri, {
30
- "Content-Length": File.size(path).to_s,
31
- "Content-Type": content_type,
32
- "Date": date,
33
- "Authorization": "AWS #{access_key}:#{signature}",
34
- "x-amz-storage-class": "STANDARD",
35
- "x-amz-acl": "private",
36
- })
37
- request.body_stream = File.open(path)
38
-
39
- Net::HTTP.start(uri.hostname) do |http|
40
- response = http.request(request)
41
- response.value # raises if not success
42
- end
43
- end
44
-
45
- private
46
-
47
- attr_accessor :full_s3_path
48
-
49
- def signature
50
- digester = OpenSSL::Digest::SHA1.new
51
- digest = OpenSSL::HMAC.digest(digester, secret_key, key)
52
- Base64.strict_encode64(digest)
53
- end
54
-
55
- def key
56
- [
57
- "PUT",
58
- "",
59
- content_type,
60
- date,
61
- acl,
62
- storage_type,
63
- full_s3_path,
64
- ].join("\n")
65
- end
66
-
67
- def content_type
68
- "application/gzip"
69
- end
70
-
71
- def date
72
- @time.rfc2822
73
- end
74
-
75
- def acl
76
- "x-amz-acl:private"
77
- end
78
-
79
- def storage_type
80
- "x-amz-storage-class:STANDARD"
81
- end
82
-
83
- def path
84
- "/tmp/#{filename}"
85
- end
86
-
87
- def filename
88
- "#{@time.utc.iso8601}.sql.gz"
4
+ 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
89
7
  end
90
8
  end
91
9
  end
10
+
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bard-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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-22 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: backhoe
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-s3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rexml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  description:
14
56
  email:
15
57
  - micah@botandrose.com
@@ -25,6 +67,9 @@ files:
25
67
  - Rakefile
26
68
  - lib/bard-backup.rb
27
69
  - lib/bard/backup.rb
70
+ - lib/bard/backup/controller.rb
71
+ - lib/bard/backup/s3_dir.rb
72
+ - lib/bard/backup/version.rb
28
73
  - sig/bard/backup.rbs
29
74
  homepage: https://github.com/botandrose/bard-backup
30
75
  licenses: