Created
February 12, 2013 18:41
-
-
Save theo-bittencourt/4772154 to your computer and use it in GitHub Desktop.
Heroku DB Dump
This file contains hidden or 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
# thor heroku:db:dump | |
# options: --keep = Keep dump file | |
# --remote staging = To change the name of the remote | |
# --user bob = To change the name of the user | |
module Heroku | |
class Db < Thor | |
method_option :keep, :type => :boolean, :default => false | |
method_option :remote, :type => :string, :default => "heroku" | |
method_option :host, :type => :string, :default => "localhost" | |
method_option :user, :type => :string, :default => `whoami` | |
method_option :dbname, :type => :string | |
method_option :dump, :type => :string, :default => "latest.dump" | |
desc "dump", "clone a remote heroku database to the local environment" | |
def dump | |
puts "Cloning production database to local environment. This might take a few minutes\n" | |
puts "(1/4) capturing production database snapshot..." | |
puts `heroku pgbackups:capture --expire --remote #{options[:remote]}` | |
puts "(2/4) downloading snapshot..." | |
puts `curl -o #{options[:dump]} \`heroku pgbackups:url --remote #{options[:remote]}\`` | |
puts "(3/4) restoring snapshot..." | |
puts `pg_restore --verbose --clean --no-acl --no-owner -h #{options[:host]} -U #{options[:user]} -d #{options[:dbname] || dbname} #{options[:dump]}` | |
unless options[:keep] | |
puts "(4/4) cleaning up..." | |
puts `rm #{options[:dump]}` | |
else | |
puts "(4/4) skipping cleaning..." | |
end | |
end | |
no_tasks do | |
def dbname | |
YAML.load_file('config/database.yml')["development"]["database"] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment