Created
August 17, 2010 23:15
-
-
Save bouchard/532561 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
## Modified from Gem Session's Dumple, available at: | |
## http://gem-session.com/2010/07/dumping-database-from-within-rails-project | |
fail_gently = ARGV.include?("--fail-gently") | |
gzip = ARGV.include?("--compress") | |
if ARGV.include?("-i") | |
puts "*******************************************************" | |
puts | |
system("du -sh ~/dumps") | |
puts | |
puts "*******************************************************" | |
exit | |
end | |
require "yaml" | |
config_path = 'config/database.yml' | |
unless File.exist?(config_path) | |
if fail_gently | |
puts "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" | |
puts "* *" | |
puts "* *" | |
puts "* Script is not called from inside a Rails project, *" | |
puts "* *" | |
puts "* THE DATABASE WILL NOT BE DUMPED. *" | |
puts "* *" | |
puts "* *" | |
puts "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" | |
sleep 5 | |
exit | |
else | |
raise "Call me from inside a Rails project." | |
end | |
end | |
config = YAML::load(File.open(config_path)) | |
environment = ARGV.reject{ |arg| arg[0].chr == '-' }.first || 'production' | |
config = config[environment] or raise "No production environment found. Please do `dumple [env_name]`" | |
dump_dir = "#{ENV['HOME']}/dumps" | |
unless File.directory?(dump_dir) | |
Dir.mkdir(dump_dir) | |
system("chmod 700 #{dump_dir}") | |
end | |
dump_path = "#{dump_dir}/#{config['database']}_#{Time.now.strftime("%Y%m%d_%H%M%S")}.dump" | |
puts | |
puts "Dumping database for environment \"#{environment}\"..." | |
system "mysqldump -u\"#{config['username']}\" -p\"#{config['password']}\" #{config['database']} -r #{dump_path}" | |
system "chmod 600 #{dump_path}" | |
dump_size_kb = (File.size(dump_path) / 1024).round | |
if gzip | |
system "gzip #{dump_path}" | |
dump_gzip_kb = (File.size(dump_path + '.gz') / 1024).round | |
puts "Dumped to #{dump_path}.gz (#{dump_size_kb} KB, #{dump_gzip_kb} KB gzipped)" | |
else | |
puts "Dumped to #{dump_path} (#{dump_size_kb} KB)" | |
end | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment