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
@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