Created
February 20, 2011 02:56
-
-
Save bookis/835645 to your computer and use it in GitHub Desktop.
Making a rake tasks to capture Heroku pgbackup and push to S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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