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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.