Skip to content

Instantly share code, notes, and snippets.

@mattsgarrison
Created December 17, 2013 15:32
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 mattsgarrison/8006808 to your computer and use it in GitHub Desktop.
Save mattsgarrison/8006808 to your computer and use it in GitHub Desktop.
A couple rake tasks to back up and restore Postgres databases in Rails using your Rails Environment variable.
namespace :db do
desc "Runs pg_dump against the environment's database."
task pg_dump: :environment do
db_config = Rails.application.config.database_configuration[Rails.env]
system "pg_dump -U #{db_config['username']} #{db_config['database']} -f dump_#{Rails.env}.sql"
end
desc "Loads a pg_dump file into the environment's database."
task pg_restore: :environment do
db_config = Rails.application.config.database_configuration[Rails.env]
input = ''
STDOUT.puts "Please provide the full path to the .sql backup file you want to restore:"
input = STDIN.gets.chomp
raise "File not found." unless File.exist?(input)
system "psql -U #{db_config['username']} -d #{db_config['database']} -f #{input}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment