Skip to content

Instantly share code, notes, and snippets.

@nicolai86
Last active May 12, 2017 07:27
Show Gist options
  • Save nicolai86/5524030 to your computer and use it in GitHub Desktop.
Save nicolai86/5524030 to your computer and use it in GitHub Desktop.
sync down the content of a postgresql database used by a rails application using mina.
RYAML = <<-BASH
function ryaml {
ruby -ryaml -e 'puts ARGV[1..-1].inject(YAML.load(File.read(ARGV[0]))) {|acc, key| acc[key] }' "$@"
};
BASH
namespace :sync do
task :db do
isolate do
invoke :environment
queue RYAML
queue "USERNAME=$(ryaml #{deploy_to}/shared/config/database.yml #{rails_env} username)"
queue "PASSWORD=$(ryaml #{deploy_to}/shared/config/database.yml #{rails_env} password)"
queue "DATABASE=$(ryaml #{deploy_to}/shared/config/database.yml #{rails_env} database)"
queue "PGPASSWORD=$PASSWORD pg_dump -U $USERNAME $DATABASE -c -f #{deploy_to}/dump.sql"
queue "gzip -f #{deploy_to}/dump.sql"
mina_cleanup!
end
%x[scp #{user}@#{domain}:#{deploy_to}/dump.sql.gz .]
%x[gunzip -f dump.sql.gz]
%x[#{RYAML} psql -d $(ryaml config/database.yml development database) -f dump.sql]
%x[rm dump.sql]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment