Skip to content

Instantly share code, notes, and snippets.

@girasquid
Created October 4, 2012 03:21
Show Gist options
  • Save girasquid/3831281 to your computer and use it in GitHub Desktop.
Save girasquid/3831281 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'aws/s3'
require 'uri'
AWS::S3::Base.establish_connection!(
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
)
filename = Time.now.strftime("%Y-%m-%d.sql")
parsed_url = URI.parse(ENV['DATABASE_URL'])
attrs_to_arguments = {
:U => 'user',
:p => 'port',
:h => 'host'
}
args = []
attrs_to_arguments.each do |argument,attr|
attr_value = parsed_url.send(attr)
if attr_value
args << "-#{argument} #{attr_value}"
end
end
file_path = "#{Dir.pwd}/#{filename}"
`pg_dump #{args.join(' ')} #{parsed_url.path.gsub('/','')} > #{file_path}`
# Restore with `pg_restore -h localhost -d ENV[DATABASE_NAME] [filename] `
AWS::S3::S3Object.store(
filename,
File.open(file_path),
ENV['AWS_BUCKET'],
:content_type => "application/xml"
)
File.delete(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment