Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Making a rake tasks to capture Heroku pgbackup and push to S3
namespace :backup do
desc "backup db from heroku and send to S3"
task :push_to_s3 => :environment do
Rake::Task[:environment].invoke
require 'aws/s3'
require 'heroku'
APP_NAME = 'luna-sandals' # put your app name here
BACKUP_BUCKET = "#{APP_NAME}-db-backups" # put your backup bucket name here
file_name = "backup-#{Time.now.strftime('%s')}.dump"
puts "Back up started @ #{Time.now}"
puts "Pulling DB..."
system "/usr/local/bin/heroku pgbackups:capture --expire --app #{APP_NAME}"
system "curl -o #{file_name} `/usr/local/bin/heroku pgbackups:url --app #{APP_NAME}`"
puts "Uploading to S3..."
s3_creds = YAML::load_file("#{Rails.root}/config/s3.yml")
AWS::S3::Base.establish_connection!(
:access_key_id => s3_creds[Rails.env]['access_key_id'],
:secret_access_key => s3_creds[Rails.env]['secret_access_key']
)
begin
bucket = AWS::S3::Bucket.find BACKUP_BUCKET
rescue
AWS::S3::Bucket.create BACKUP_BUCKET
bucket = AWS::S3::Bucket.find BACKUP_BUCKET
end
aws = AWS::S3::S3Object.store file_name, File.read(file_name), bucket.name#, :content_type => 'application/x-gzip'
puts "Done @ #{Time.now}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.