Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Created May 17, 2011 19:33
Show Gist options
  • Select an option

  • Save carlhoerberg/977203 to your computer and use it in GitHub Desktop.

Select an option

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

Copy link
Copy Markdown

This just saved me! Thanks!

@mcansky

mcansky commented Jun 1, 2011

Copy link
Copy Markdown

having trouble with this :
require 'heroku/command/auth'
require 'heroku/command/pgbackups'
don't work for me

@carlhoerberg

carlhoerberg commented Jun 1, 2011 via email

Copy link
Copy Markdown
Author

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