Skip to content

Instantly share code, notes, and snippets.

@nicolai86
Last active December 3, 2018 07:17
Show Gist options
  • Save nicolai86/5524034 to your computer and use it in GitHub Desktop.
Save nicolai86/5524034 to your computer and use it in GitHub Desktop.
sync down the content of a mysql database used by a ruby on 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 "mysqldump $DATABASE --user=$USERNAME --password=$PASSWORD > #{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} mysql --verbose --user=$(ryaml config/database.yml development username) --password=$(ryaml config/database.yml development password) $(ryaml config/database.yml development database) < dump.sql]
%x[rm dump.sql]
end
end
@jbonney
Copy link

jbonney commented Aug 17, 2013

Thank you for this script. I found the RYAML part particularly useful.

@hulous
Copy link

hulous commented Oct 10, 2015

Thank you for the script ! I fork and update it for my own use (push local db to prod). This is not perfect but It works. ;-)

@crysfel
Copy link

crysfel commented Oct 19, 2017

thanks! I only had to remove the ; from the RYAML block in order to make it work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment