heroku_pg_backups_archive 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 947869e18c118d8c6b5af03214407ce74bdd16a8
4
+ data.tar.gz: 24adac58e9eea4a0554ab6f1d65875cde97ba56f
5
+ SHA512:
6
+ metadata.gz: a73b5e1905f54a1c91fdd7694c71688b9df08a22404c7db22a0d794013a1a7af286cc0c070d66d9e4f4255a8f60993ff5bb88a03c0972ff0901f62801a1302fe
7
+ data.tar.gz: ffa5a6425fe3b4a4c8978e220cc23ee8553f2d624c2bd5c27217f5b23aa9697f8e7cbb574a50a993484b1ad863ef40f18c4cf696ce42cff09d093c46169973e5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in heroku_pg_backups_archive.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Brent Wheeldon
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,59 @@
1
+ # HerokuPgBackupsArchive
2
+
3
+ This gem allows you to backup your heroku postgres database and archive it to S3, optionally with [SSE-C](http://docs.aws.amazon.com/AmazonS3/latest/dev/ServerSideEncryptionCustomerKeys.html).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'heroku_pg_backups_archive'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install heroku_pg_backups_archive
20
+
21
+ ## Usage
22
+
23
+ There is some configuration required. For a rails application, create a file like `config/initializers/heroku_pg_backups_archive.rb` with contents like:
24
+
25
+ ```
26
+ HerokuPgBackupsArchive.configure do |config|
27
+ # Required
28
+ config.app_name = "your-heroku-app-name"
29
+ config.bucket_name = "your-s3-bucket-name"
30
+
31
+ # Optional
32
+ config.sse_customer_key = "your-sse-c-key" # leave blank to disable SSE-C
33
+ config.heroku_toolbelt_path = "path/to/heroku/executable" # defaults to `vendor/heroku-toolbelt/bin/heroku` when not explicitly set
34
+ config.aws_access_key_id = "aws-secret-key-id" # defaults to `ENV["AWS_ACCESS_KEY_ID"]` when not explicitly set
35
+ config.aws_secret_access_key = "aws-secret-access-key" # defaults to `ENV["AWS_SECRET_ACCESS_KEY"]` when not explicitly set
36
+ end
37
+ ```
38
+
39
+ To run the backup, simply run:
40
+
41
+ ```
42
+ rake heroku_pg_backups_archive
43
+ ```
44
+
45
+ To make the heroku toolbelt available on your dyno, you can use [heroku-buildpack-toolbelt](https://github.com/gregburek/heroku-buildpack-toolbelt).
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/[my-github-username]/heroku_pg_backups_archive/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "heroku_pg_backups_archive"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'heroku_pg_backups_archive/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "heroku_pg_backups_archive"
8
+ spec.version = HerokuPgBackupsArchive::VERSION
9
+ spec.authors = ["Hightower"]
10
+ spec.email = ["engineering@gethightower.com"]
11
+
12
+ spec.summary = %q{Backup your heroku PG database, and archive it to S3.}
13
+ spec.description = %q{Backup your heroku PG database, and archive it to S3 optionally with SSE-CA.}
14
+ spec.homepage = "https://www.github.com/hightower/heroku_pg_backups_archive"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "aws-sdk-v1", "~> 0"
23
+ spec.add_development_dependency "bundler", "~> 1.8"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "pry", "~> 0"
26
+ end
@@ -0,0 +1,24 @@
1
+ require "heroku_pg_backups_archive/version"
2
+ require "heroku_pg_backups_archive/config"
3
+ require "heroku_pg_backups_archive/backup"
4
+ require "heroku_pg_backups_archive/backup_archive"
5
+ require "heroku_pg_backups_archive/backup_failed_error"
6
+ require "heroku_pg_backups_archive/toolbelt_helper"
7
+ require "heroku_pg_backups_archive/railtie"
8
+
9
+ module HerokuPgBackupsArchive
10
+ class << self
11
+ def config
12
+ @config ||= Config.new
13
+ end
14
+
15
+ def configure
16
+ yield config
17
+ end
18
+
19
+ def backup_and_archive
20
+ backup = Backup.create
21
+ BackupArchive.perform(backup)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ module HerokuPgBackupsArchive
2
+ class Backup
3
+ def self.create
4
+ backup_output = ToolbeltHelper.capture_backup
5
+ new(backup_output)
6
+ end
7
+
8
+ attr_reader :id
9
+
10
+ def initialize(backup_output)
11
+ @id = extract_id(backup_output)
12
+ end
13
+
14
+ def url
15
+ @url ||= ToolbeltHelper.fetch_backup_public_url(id).chomp
16
+ end
17
+
18
+ def finished_at
19
+ @finished_at ||= begin
20
+ info = ToolbeltHelper.fetch_backup_info(id)
21
+ Time.parse(info.match(/Finished:\s*(.*)\n/)[1])
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def extract_id(backup_output)
28
+ matches = backup_output.match(/---backup---> (.*)\n/)
29
+ raise BackupFailedError.new(backup_output) unless matches
30
+ matches[1]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,55 @@
1
+ require "open-uri"
2
+ require "aws-sdk-v1"
3
+ require "base64"
4
+
5
+ module HerokuPgBackupsArchive
6
+ class BackupArchive
7
+ def self.perform(backup)
8
+ backup_archive = new(backup)
9
+ backup_archive.perform
10
+ end
11
+
12
+ def initialize(backup)
13
+ @backup = backup
14
+ end
15
+
16
+ def perform
17
+ bucket.objects[archive_path].write(backup_data, write_options)
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :backup
23
+
24
+ def bucket
25
+ @bucket ||= s3.buckets[HerokuPgBackupsArchive.config.bucket_name]
26
+ end
27
+
28
+ def s3
29
+ @s3 ||= AWS::S3.new(
30
+ access_key_id: HerokuPgBackupsArchive.config.aws_access_key_id,
31
+ secret_access_key: HerokuPgBackupsArchive.config.aws_secret_access_key
32
+ )
33
+ end
34
+
35
+ def archive_path
36
+ "#{backup.finished_at.strftime("%Y/%m/%d")}/#{backup.finished_at.iso8601}"
37
+ end
38
+
39
+ def backup_data
40
+ open(backup.url)
41
+ end
42
+
43
+ def write_options
44
+ unless HerokuPgBackupsArchive.config.sse_customer_key.nil?
45
+ {
46
+ sse_customer_algorithm: :AES256,
47
+ sse_customer_key: Base64.encode64(HerokuPgBackupsArchive.config.sse_customer_key),
48
+ sse_customer_key_md5: Base64.encode64(OpenSSL::Digest::MD5.digest(HerokuPgBackupsArchive.config.sse_customer_key))
49
+ }
50
+ else
51
+ {}
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,4 @@
1
+ module HerokuPgBackupsArchive
2
+ class BackupFailedError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,11 @@
1
+ module HerokuPgBackupsArchive
2
+ class Config
3
+ attr_accessor :heroku_toolbelt_path, :app_name, :sse_customer_key, :bucket_name, :aws_access_key_id, :aws_secret_access_key
4
+
5
+ def initialize
6
+ @heroku_toolbelt_path = "vendor/heroku-toolbelt/bin/heroku"
7
+ @aws_access_key_id = ENV["AWS_ACCESS_KEY_ID"]
8
+ @aws_secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ if defined?(Rails)
2
+ module HerokuPgBackupsArchive
3
+ class Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ load "tasks/heroku_pg_backups_archive.rake"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ module HerokuPgBackupsArchive
2
+ module ToolbeltHelper
3
+ class << self
4
+ def capture_backup
5
+ run("pg:backups capture -a #{HerokuPgBackupsArchive.config.app_name}")
6
+ end
7
+
8
+ def fetch_backup_public_url(backup_id)
9
+ run("pg:backups public-url #{backup_id} -q -a #{HerokuPgBackupsArchive.config.app_name}")
10
+ end
11
+
12
+ def fetch_backup_info(backup_id)
13
+ run("pg:backups info #{backup_id} -a #{HerokuPgBackupsArchive.config.app_name}")
14
+ end
15
+
16
+ private
17
+
18
+ def run(arguments)
19
+ command = "#{HerokuPgBackupsArchive.config.heroku_toolbelt_path} #{arguments}"
20
+ puts "Running: #{command}"
21
+ output = `#{command}`
22
+ puts "Output:\n#{output}"
23
+ output
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module HerokuPgBackupsArchive
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ desc "Capture a backup and archive it to S3"
2
+ task heroku_pg_backups_archive: :environment do
3
+ HerokuPgBackupsArchive.backup_and_archive
4
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heroku_pg_backups_archive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hightower
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-v1
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Backup your heroku PG database, and archive it to S3 optionally with
70
+ SSE-CA.
71
+ email:
72
+ - engineering@gethightower.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - heroku_pg_backups_archive.gemspec
88
+ - lib/heroku_pg_backups_archive.rb
89
+ - lib/heroku_pg_backups_archive/backup.rb
90
+ - lib/heroku_pg_backups_archive/backup_archive.rb
91
+ - lib/heroku_pg_backups_archive/backup_failed_error.rb
92
+ - lib/heroku_pg_backups_archive/config.rb
93
+ - lib/heroku_pg_backups_archive/railtie.rb
94
+ - lib/heroku_pg_backups_archive/toolbelt_helper.rb
95
+ - lib/heroku_pg_backups_archive/version.rb
96
+ - lib/tasks/heroku_pg_backups_archive.rake
97
+ homepage: https://www.github.com/hightower/heroku_pg_backups_archive
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Backup your heroku PG database, and archive it to S3.
121
+ test_files: []