Skip to content

Instantly share code, notes, and snippets.

@SergeyKozlov
Forked from Epictetus/Rakefile.rb
Created July 6, 2018 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergeyKozlov/eb2f940eaef33a39dc94b37a81b59e5a to your computer and use it in GitHub Desktop.
Save SergeyKozlov/eb2f940eaef33a39dc94b37a81b59e5a 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment