heroku_cloud_backup 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ .rvmrc
3
+ *.gem
4
+ .bundle
5
+ Gemfile.lock
6
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in heroku_cloud_backup.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jack Chu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = heroku_cloud_backup
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Jack Chu. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "heroku_cloud_backup/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "heroku_cloud_backup"
7
+ s.version = HerokuCloudBackup::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jack Chu"]
10
+ s.email = ["jack@jackchu.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Backup pg dumps to the cloud}
13
+ s.description = %q{PG backups into the cloud with fog}
14
+
15
+ s.rubyforge_project = "heroku_cloud_backup"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.add_runtime_dependency('fog', '>= 0.7.2')
22
+ s.add_runtime_dependency('heroku', '>= 2.1.4')
23
+ end
@@ -0,0 +1,10 @@
1
+ module HerokuCloudBackup
2
+ module Errors
3
+ class Error < StandardError; end
4
+ class NotFound < HerokuCloudBackup::Errors::Error; end
5
+ class InvalidProvider < HerokuCloudBackup::Errors::Error; end
6
+ class ConnectionError < HerokuCloudBackup::Errors::Error; end
7
+ class UploadError < HerokuCloudBackup::Errors::Error; end
8
+ class NoBackups < HerokuCloudBackup::Errors::Error; end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ require 'heroku_cloud_backup'
2
+
3
+ module HerokuCloudBackup
4
+ if defined? Rails::Railtie
5
+ require 'rails'
6
+ class Railtie < Rails::Railtie
7
+ rake_tasks do
8
+ require "heroku_cloud_backup/tasks"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ require 'heroku_cloud_backup'
2
+
3
+ namespace :heroku do
4
+ desc "Example showing PostgreSQL database backups from Heroku to Amazon S3"
5
+ task :backup => :environment do
6
+ HerokuCloudBackup.execute
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module HerokuCloudBackup
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,108 @@
1
+ require 'fog'
2
+ require 'open-uri'
3
+ require "heroku"
4
+ require "pgbackups/client"
5
+ require 'heroku_cloud_backup/errors'
6
+ require 'heroku_cloud_backup/railtie'
7
+ require 'heroku_cloud_backup/version'
8
+
9
+ module HerokuCloudBackup
10
+ class << self
11
+
12
+ def log(message)
13
+ puts "[#{Time.now}] #{message}"
14
+ end
15
+
16
+ def backups_url
17
+ ENV["PGBACKUPS_URL"]
18
+ end
19
+
20
+ def client
21
+ @client ||= PGBackups::Client.new(ENV["PGBACKUPS_URL"])
22
+ end
23
+
24
+ def databases
25
+ if db = ENV["HEROKU_BACKUP_DATABASES"]
26
+ db.split(",").map(&:strip)
27
+ else
28
+ ["DATABASE_URL"]
29
+ end
30
+ end
31
+
32
+ def backup_name(to_url)
33
+ # translate s3://bucket/email/foo/bar.dump => foo/bar
34
+ parts = to_url.split('/')
35
+ parts.slice(4..-1).join('/').gsub(/\.dump$/, '')
36
+ end
37
+
38
+ def execute
39
+ log "heroku:backup started"
40
+
41
+ @bucket_name = ENV['HCB_BUCKET'] || "#{ENV['APP_NAME']}-heroku-backups"
42
+ @backup_path = ENV['HCB_PREFIX'] || "db"
43
+ @providers = ENV['HCB_PROVIDERS'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_PROVIDERS' config variable."))
44
+ b = client.get_latest_backup
45
+ raise HerokuCloudBackup::Errors::NoBackups.new("You don't have any pgbackups. Please run heroku pgbackups:capture first.") if b.empty?
46
+
47
+ @providers.split(',').each do |provider|
48
+ case provider
49
+ when 'aws'
50
+ @hcb_aws_access_key_id = ENV['HCB_AWS_ACCESS_KEY_ID'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_AWS_ACCESS_KEY_ID' config variable."))
51
+ @hcb_aws_secret_access_key = ENV['HCB_AWS_SECRET_ACCESS_KEY'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_AWS_SECRET_ACCESS_KEY' config variable."))
52
+ begin
53
+ @connection = Fog::Storage.new(
54
+ :provider => 'AWS',
55
+ :aws_access_key_id => @hcb_aws_access_key_id,
56
+ :aws_secret_access_key => @hcb_aws_secret_access_key
57
+ )
58
+ rescue
59
+ raise HerokuCloudBackup::Errors::ConnectionError.new("There was an error connecting to your provider.")
60
+ end
61
+ else
62
+ raise HerokuCloudBackup::Errors::InvalidProvider.new("One or more of your providers were invalid. Valid values are 'aws', 'rackspace', or 'google'")
63
+ end
64
+
65
+ directory = @connection.directories.get(@bucket_name)
66
+
67
+ if !directory
68
+ directory = @connection.directories.create(:key => @bucket_name)
69
+ end
70
+
71
+ public_url = b["public_url"]
72
+ created_at = DateTime.parse b["created_at"]
73
+ db_name = b["from_name"]
74
+ name = "#{ENV['APP_NAME']}-#{created_at.strftime('%Y-%m-%d-%H%M%S')}.dump"
75
+ begin
76
+ directory.files.create(:key => "#{@backup_path}/#{b["from_name"]}/#{name}", :body => open(public_url))
77
+ rescue Exception => e
78
+ raise HerokuCloudBackup::Errors::UploadError.new(e.message)
79
+ end
80
+ end
81
+
82
+ prune
83
+
84
+ log "heroku:backup complete"
85
+ end
86
+
87
+ private
88
+
89
+ def prune
90
+ number_of_files = ENV['HCB_MAX_BACKUPS']
91
+ if number_of_files && number_of_files.to_i > 0
92
+ directory = @connection.directories.get(@bucket_name)
93
+ files = directory.files.all(:prefix => @backup_path)
94
+ file_count = 0
95
+ files.reverse.each do |file|
96
+ if file.key =~ Regexp.new("/#{@backup_path}\/#{ENV['APP_NAME']}-\d{4}-\d{2}-\d{2}-\d{6}\.sql\.gz$/i")
97
+ file_count += 1
98
+ else
99
+ next
100
+ end
101
+ if file_count > number_of_files
102
+ file.destroy
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heroku_cloud_backup
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Jack Chu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-23 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fog
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.7.2
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: heroku
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.1.4
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: PG backups into the cloud with fog
38
+ email:
39
+ - jack@jackchu.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - heroku_cloud_backup.gemspec
53
+ - lib/heroku_cloud_backup.rb
54
+ - lib/heroku_cloud_backup/errors.rb
55
+ - lib/heroku_cloud_backup/railtie.rb
56
+ - lib/heroku_cloud_backup/tasks.rb
57
+ - lib/heroku_cloud_backup/version.rb
58
+ homepage: ""
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: heroku_cloud_backup
81
+ rubygems_version: 1.8.3
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Backup pg dumps to the cloud
85
+ test_files: []
86
+