Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Created May 17, 2011 19:33
Show Gist options
  • Save carlhoerberg/977203 to your computer and use it in GitHub Desktop.
Save carlhoerberg/977203 to your computer and use it in GitHub Desktop.
How to do automatic backup with Heroku PGBackups and Heroku Cron. http://carlhoerberg.com/automatic-backup-of-heroku-database-to-s3
require 'aws/s3'
require 'heroku'
require 'heroku/command'
require 'heroku/command/auth'
require 'heroku/command/pgbackups'
task :cron do
class Heroku::Auth
def self.client
Heroku::Client.new ENV['heroku_login'], ENV['heroku_passwd']
end
end
Heroku::Command.run 'pgbackups:capture', ['--expire', '--app', ENV['heroku_appname']]
url = capture_stdout do
Heroku::Command.run 'pgbackups:url', ['--app', 'dmks']
end
AWS::S3::Base.establish_connection!(:access_key_id => ENV['s3_access_key'], :secret_access_key => ENV['s3_secret_key'])
AWS::S3::S3Object.store "pgdb-#{Time.now.strftime('%y%m%d_%H%M')}.dump", open(url), ENV['s3_backup_bucket']
end
module Kernel
def capture_stdout
out = StringIO.new
$stdout = out
yield
return out.string
ensure
$stdout = STDOUT
end
end
@carlhoerberg
Copy link
Author

carlhoerberg commented Jun 1, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment