nate (owner)

Revisions

gist: 130815 Download_button fork
public
Public Clone URL: git://gist.github.com/130815.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace :db do
  task :backup_name, :roles => :db, :only => { :primary => true } do
    now = Time.now
    run "mkdir -p #{shared_path}/db_backups"
    backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-')
    set :backup_file, "#{shared_path}/db_backups/#{rails_env}-snapshot-#{backup_time}.sql"
  end
 
  desc "Backup your MySQL database to shared_path/db_backups"
  task :dump, :roles => :db, :only => { :primary => true } do
    backup_name
    run("cat #{shared_path}/config/database.yml") {|channel, stream, data| @environment_info = YAML.load(data)[rails_env] }
    run "mysqldump --add-drop-table -u #{@environment_info['username']} -p #{@environment_info['database']} | bzip2 -c > #{backup_file}.bz2" do |ch, stream, out |
       ch.send_data "#{@environment_info['password']}\n" if out=~ /^Enter password:/
    end
  end
 
  desc "Sync your production database to your local workstation"
  task :copy_to_local, :roles => :db, :only => { :primary => true } do
    backup_name
    dump
    get "#{backup_file}.bz2", "tmp/#{application}.sql.bz2"
  end
end