Skip to content

Instantly share code, notes, and snippets.

@techpeace
Created December 29, 2009 21:43
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 techpeace/265626 to your computer and use it in GitHub Desktop.
Save techpeace/265626 to your computer and use it in GitHub Desktop.
require 'aws/s3'
require 'erb'
namespace :heroku do
def latest_bundle_name(app)
%x{ heroku bundles #{app_option(app)}| cut -f 1 -d ' ' }.chomp
end
def app_option(app)
'--app ' + app if app
end
def app_name(app)
return app if app
%x{heroku info}.split("\n")[0].split(' ')[1]
end
desc "Capture a new bundle and back it up to S3."
task :backup, :app do |t, args|
app = args[:app]
# Warn that we're about to blow out the latest bundle.
print 'WARNING: This will destroy the most recent bundle. Do you wish to proceed? (y/n) '
answer = STDIN.gets.chomp
exit unless answer == 'y'
puts
puts '===== Deleting most recent bundle from Heroku...'
%x{ heroku bundles:destroy #{latest_bundle_name(app)} #{app_option(app)} }
puts '===== Capturing a new bundle...'
%x{ heroku bundles:capture #{app_option(app)} }
while %x{ heroku bundles #{app_option(app)} | grep complete }.empty?
sleep 10
end
puts '===== Downloading new bundle...'
%x{ heroku bundles:download #{app_option(app)} }
puts '===== Pushing the bundle up to S3...'
# Establish a connection to S3.
aws_creds = YAML::load(ERB.new(File.read(File.join(Dir.getwd, 'config', 'amazon_s3.yml'))).result)["default"]
AWS::S3::Base.establish_connection!(
:access_key_id => aws_creds["access_key_id"],
:secret_access_key => aws_creds["secret_access_key"]
)
current_app_name = app_name(app)
bundle_file_name = current_app_name + '.tar.gz'
AWS::S3::S3Object.store(latest_bundle_name(app) + '.tar.gz', open(bundle_file_name), current_app_name + '-backups')
puts '===== Deleting the temporary bundle file...'
FileUtils.rm(bundle_file_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment