Skip to content

Instantly share code, notes, and snippets.

@robhurring
Last active August 29, 2015 13:58
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 robhurring/9995758 to your computer and use it in GitHub Desktop.
Save robhurring/9995758 to your computer and use it in GitHub Desktop.
Rake task to sync heroku database locally
namespace :db do
desc 'sync with prod database'
task :sync, :backup_id do |t, args|
dump_file = Rails.root.join('tmp', 'latest.dump')
db_configs = YAML.load_file(Rails.root.join('config', 'database.yml'))
db_config = db_configs[Rails.env]
commands = [
"heroku pgbackups:capture --expire",
"curl -o #{dump_file} \`heroku pgbackups:url #{args[:backup_id]}\`",
"pg_restore --verbose --clean --no-acl --no-owner -U #{db_config['username']} -d #{db_config['database']} #{dump_file}",
"rm -f #{dump_file}"
]
puts "\033[36mDropping current db\033[0m"
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
puts "\033[36mImporting latest DB from heroku\033[0m"
commands.each do |command|
STDERR.puts command
`#{command}`
end
puts "\033[36mMigrating...\033[0m"
Rake::Task["db:migrate"].invoke
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment