rake rails db backup

Ruby code posted by dan ryan
created at 02 Jun 02:31

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

namespace :db do
  # Declare some variables
  timestamp = Time.now.strftime("%Y-%m-%d")
  backup_dir = "#{RAILS_ROOT}/db"

  namespace :backup do
    desc 'Backup all the local databases defined in config/database.yml'
    task :all => :environment do
      ActiveRecord::Base.configurations.each_value do |config|

      system "mysqldump -u #{config['username']} -p#{config['password']} \
      #{config['database']} >   \
      #{backup_dir}/#{config['database']}_#{timestamp}.sql"
      end
    end
  end



  desc 'Backup the database for the current RAILS_ENV'
  task :backup => :environment do
    config = ActiveRecord::Base.configurations[RAILS_ENV]
    system "mysqldump -u #{config['username']} -p#{config['password']} \
    #{config['database']} > \
    #{backup_dir}/#{config['database']}_#{timestamp}.sql"

  end
end
860 Bytes in 3 ms with coderay