Skip to content

Instantly share code, notes, and snippets.

@nthj
Created September 25, 2012 03:33
Show Gist options
  • Save nthj/3779827 to your computer and use it in GitHub Desktop.
Save nthj/3779827 to your computer and use it in GitHub Desktop.
Quickly dump your latest Heroku backup into Rackspace Cloud Files via rake db:backup
namespace :db do
desc 'Copy latest Heroku backup over to our Rackspace account'
task backup: :environment do
require 'heroku/client/pgbackups'
require 'open-uri'
credentials = YAML::load_file(Rails.root.join('config', 'rackspace.yml'))
api_key = credentials['production']['api_key']
container = credentials['production']['container']
username = credentials['production']['username']
url = Heroku::Client::Pgbackups.new(ENV['PGBACKUPS_URL']).get_latest_backup['public_url']
open('tmp/backup.dump', 'wb') do |file|
file << open(url).read
end
etag = `md5sum tmp/backup.dump`.to_s.split(' ').first
_, destination, token = ` curl -D - \
-H "X-Auth-Key: #{api_key}" \
-H "X-Auth-User: #{username}" \
https://auth.api.rackspacecloud.com/v1.0 | grep "X-"`.split("\n").map(&:strip).delete_if(&:blank?).map do |key|
key.split(' ').last
end
puts command = <<COMMAND
curl -X PUT -T tmp/backup.dump \
-H "ETag: #{etag}" \
-H "X-Auth-Token: #{token}" \
#{destination}/#{container}/backup-#{Time.now.to_i}.dump
COMMAND
`#{command}`
end
end
gem 'heroku'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment