Created
August 15, 2011 02:33
-
-
Save garybernhardt/1145617 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task :cron => :environment do | |
DatabaseBackup.back_up_to_s3! | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'heroku' | |
require 'heroku/command' | |
require 'heroku/command/auth' | |
require 'heroku/command/pgbackups' | |
class DatabaseBackup | |
def self.back_up_to_s3! | |
app_name = ENV.fetch('HEROKU_APP_NAME') | |
Heroku::Command.run 'pgbackups:capture', ['--expire', '--app', app_name] | |
url = capture_stdout do | |
Heroku::Command.run 'pgbackups:url', ['--app', app_name] | |
end | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => ENV.fetch('AMAZON_ACCESS_KEY_ID'), | |
:secret_access_key => ENV.fetch('AMAZON_SECRET_ACCESS_KEY')) | |
url.gsub! /^"|"$/, '' | |
AWS::S3::S3Object.store( | |
"pgdb-#{Time.now.strftime('%y%m%d_%H%M')}.dump", | |
open(url), | |
ENV.fetch('S3_BACKUP_BUCKET')) | |
end | |
def self.capture_stdout | |
out = StringIO.new | |
$stdout = out | |
yield | |
return out.string | |
ensure | |
$stdout = STDOUT | |
end | |
end | |
class Heroku::Auth | |
def self.client | |
Heroku::Client.new ENV.fetch('HEROKU_LOGIN'), ENV.fetch('HEROKU_PASSWORD') | |
end | |
end |
For me with the 2.1.4 version of the heroku gem, I had to require it aws-s3 gem.
Adding: gem 'aws-s3', '0.6.2', :require => 'aws/s3' to my Gemfile did the trick, although I am still on the fence about the require in the Gemfile instead of just in the actual database class.
Going forward on Heroku's cedar stack, I am sure you will be on your own for this gem either way.
The AWS gem is required by Bundler. I only explicitly require the Heroku gem because I had to for some reason, even though it was in my Gemfile. It seems that requiring 'heroku' doesn't also require those other bits.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is AWS required by Heroku?
-- EDIT
Just realized by question was horribly vague. I noticed you didn't require the aws gem. Does Heroku do it for you, or was it required by some other component of your system?