bard-backup 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 55ece259f7be3aae82e05c75b510f18bb1d7f70b9d0c77b3b9f6e71a641ddf4c
4
+ data.tar.gz: aed5da390d8f657fe3db65b009cde22ef0e6baf545fd4aafb2222e62258c3ad1
5
+ SHA512:
6
+ metadata.gz: dcc9f83eb793a5ed76446c862ce95a4b2b019eb38aa2f5c6589f5e6a516642f5cbad1292054e2f72f4a676d40f91c79b11cb6f26457fcdea565509c337a72dc2
7
+ data.tar.gz: 9a61e8264d5f3e4fe0247e88aba019b9971099a4325ff523c0d2cb195b104d51d2c0a2b3b497b1a15016c9a83d7e2f97c1a2e5920686fd5da732d579689d139a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ bard-backup
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Micah Geisel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Bard::Backup
2
+
3
+ Bard::Backup does 3 things in a bard project
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
6
+ 3. Raises an error if we don't have a backup from the previous hour
7
+
8
+ ## Installation
9
+
10
+ ## Usage
11
+
12
+ Run with `Bard::Backup.call s3_path, access_key: "...", secret_key: "..."`
13
+
14
+ Or just run via the `bard-rake` gem: `rake db:backup`, which wires up the above for you.
15
+
16
+ ## Development
17
+
18
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
19
+
20
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
21
+
22
+ ## Contributing
23
+
24
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bard-backup.
25
+
26
+ ## License
27
+
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ # RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "openssl"
5
+ require "base64"
6
+ require "backhoe"
7
+
8
+ 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"
89
+ end
90
+ end
91
+ end
@@ -0,0 +1 @@
1
+ require "bard/backup"
@@ -0,0 +1,6 @@
1
+ module Bard
2
+ module Backup
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bard-backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Micah Geisel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - micah@botandrose.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".ruby-gemset"
22
+ - ".ruby-version"
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/bard-backup.rb
27
+ - lib/bard/backup.rb
28
+ - sig/bard/backup.rbs
29
+ homepage: https://github.com/botandrose/bard-backup
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/botandrose/bard-backup
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.0.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.4.19
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Provides automated db backups for bard projects
53
+ test_files: []