Created
November 23, 2011 11:55
-
-
Save damian/1388506 to your computer and use it in GitHub Desktop.
Cheap db backup rake task to be used with Rails
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
namespace :db do | |
desc 'Backup database' | |
task :backup => :environment do | |
# Loads in database config and finds the correct environment | |
config = YAML::load(File.open('config/database.yml'))[Rails.env] | |
# Creates a filename which is unique to the backup | |
filename = "backup_#{Time.now.to_i}" | |
# Run a mysqldump using our credentials from database config | |
`mysqldump -u#{config['username']} -p#{config['password']} #{config['database']} > tmp/#{filename}.sql` | |
# Copy the backup from the remote box to our local machine | |
get "tmp/#{filename}", "~/Desktop/#{filename}" | |
# Delete the backup on the remote box | |
delete "tmp/#{filename}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment