Quite often in Rails projects, I need some way to backup the contents of database quickly. The script below is an easy way to backup the database of your current environment using the database.yml file already set up in your Rails project. Feel free to modify the below script to do your bidding.
/script/backup_db:
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'erb' require 'yaml' unless config = YAML::load(ERB.new(IO.read(RAILS_ROOT + "/config/database.yml")).result)[RAILS_ENV] abort "No database found for environment '#{RAILS_ENV}'" end db_out_path = File.join(RAILS_ROOT, 'tmp', "dbdump_#{RAILS_ENV}.sql") system "mysqldump -u #{config['username']} --password=#{config['password']} #{config['database']} >; #{db_out_path}"
To run your backup, just execute this from your rails root:
script/backup_db RAILS_ENV=production