Skip to content

Instantly share code, notes, and snippets.

@damian
Created November 23, 2011 11:55
Show Gist options
  • Save damian/1388506 to your computer and use it in GitHub Desktop.
Save damian/1388506 to your computer and use it in GitHub Desktop.
Cheap db backup rake task to be used with Rails
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