Skip to content

Instantly share code, notes, and snippets.

@svenyurgensson
Forked from nicolai86/sync-postgres-mina.rb
Created June 10, 2016 12:45
Show Gist options
  • Save svenyurgensson/fc634a21ba9108278eeafbbcd43067cb to your computer and use it in GitHub Desktop.
Save svenyurgensson/fc634a21ba9108278eeafbbcd43067cb 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