Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Created May 17, 2011 19:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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
@vandersonmota
Copy link

This just saved me! Thanks!

@mcansky
Copy link

mcansky commented Jun 1, 2011

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

@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