Created
December 17, 2013 15:32
-
-
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.
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
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