Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created August 15, 2011 02:33
Show Gist options
  • Save garybernhardt/1145617 to your computer and use it in GitHub Desktop.
Save garybernhardt/1145617 to your computer and use it in GitHub Desktop.
task :cron => :environment do
DatabaseBackup.back_up_to_s3!
end
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
@scottwater
Copy link

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.

@garybernhardt
Copy link
Author

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