Skip to content

Instantly share code, notes, and snippets.

@zhiyao
Created October 30, 2012 16:10
Show Gist options
  • Save zhiyao/3981213 to your computer and use it in GitHub Desktop.
Save zhiyao/3981213 to your computer and use it in GitHub Desktop.
Heroku database backup, transfer between production and staging server, alot faster that the heroku db:pull and db:push. This will only work if your local app is setup with postgres database
#!/usr/bin/env ruby
#Place this in your root directory of your rails app
#Ensure that you have install thor gem
#To list the command, type 'thor -T'
module Heroku
class Db < Thor
desc "production_to_local", "clone a remote heroku database to the local environment"
method_option :remote_production, :type => :string, :default => 'production'
method_option :keep, :type => :boolean, :default => false
method_option :host, :type => :string, :default => "localhost"
method_option :user, :type => :string, :default => 'tenbugs'
method_option :dbname, :type => :string, :default => 'development_database_name' #development database name
method_option :dump, :type => :string, :default => "latest.dump"
def production_to_local
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_production]}`
puts "(2/4) downloading snapshot..."
puts `curl -o #{options[:dump]} \`heroku pgbackups:url --remote #{options[:remote_production]}\``
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
desc "production_to_staging", "transfer the production heroku database to the staging heroku database"
method_option :remote_staging, :type => :string, :default => 'staging'
method_option :remote_production, :type => :string, :default => 'production'
method_option :dbname, :type => :string, :default => 'staging_database_name'
def production_to_staging
puts "Cloning production database to staging database. This might take a few minutes\n"
puts "(1/2) capturing production database snapshot..."
puts `heroku pgbackups:capture --expire --remote #{options[:remote_production]}`
puts "(2/2) restoring snapshot from production database to staging database..."
puts `heroku pgbackups:restore DATABASE \`heroku pgbackups:url --remote #{options[:remote_production]}\` --app #{options[:dbname]} --confirm #{options[:dbname]}`
puts "Production database successfully transfer to staging database"
end
desc "staging_to_production", "transfer the staging heroku database to the production heroku database"
method_option :remote_staging, :type => :string, :default => 'staging'
method_option :remote_production, :type => :string, :default => 'production'
method_option :dbname, :type => :string, :default => 'development_database_name'
def staging_to_production
puts "Cloning staging database to production database. This might take a few minutes\n"
puts "(1/2) capturing staging database snapshot..."
puts `heroku pgbackups:capture --expire --remote #{options[:remote_staging]}`
puts "(2/2) restoring snapshot from staging database to production database..."
puts `heroku pgbackups:restore DATABASE \`heroku pgbackups:url --remote #{options[:remote_staging]}\` --remote #{options[:remote_production]} --confirm #{options[:dbname]}`
puts "Staging database successfully transfer to production database"
end
desc "rebuild_assets", "clean and precompile assets"
def rebuild_assets
puts "Cleaning all assets"
puts `bundle exec rake assets:clean`
puts "Precompiling all assets"
puts `bundle exec rake assets:precompile`
puts "Assets Precompiled"
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