Skip to content

Instantly share code, notes, and snippets.

@cdmwebs
Created December 20, 2010 10:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cdmwebs/748222 to your computer and use it in GitHub Desktop.
Save cdmwebs/748222 to your computer and use it in GitHub Desktop.
Pull a copy of the latest backup via ssh and restore to dev
require 'net/scp'
namespace :db do
desc 'pull the latest backup & restore locally'
task :restore do
Rake::Task['db:download'].invoke
Rake::Task['db:replace'].invoke
end
desc 'pull down the lastest backup from production'
task :download do
Net::SCP.download!("example.org", "user", "path/to/latest.tar.gz", "latest.tar.gz", :verbose => true) do |ch, name, sent, total|
puts "#{name}: #{sent}/#{total}"
end
puts 'gunzipping...'
system *%W[gunzip -f latest.tar.gz]
end
desc 'restore a production backup locally'
task :replace => :environment do
puts 'dropping and re-creating db...'
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
puts 'importing...'
db_name = Rails.configuration.database_configuration[Rails.env]['database']
system *%W[pg_restore --verbose --no-owner --dbname #{db_name} latest.tar]
puts 'all done!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment