未定义的方法`实例' for Capistrano :: Configuration:尝试使用capistrano任务时的类

时间:2015-01-18 10:20:25

标签: ruby-on-rails capistrano

以另一种方式解决问题

我决定使用https://github.com/sgruhier/capistrano-db-tasks来解决问题。

原始问题

要将生产数据库克隆到本地Rail的环境,我想使用this gist Capistrano's task

我复制了代码并将其粘贴到lib/capistrano/tasks/db.cap然后运行此命令:

 % bundle exec cap production db:clone_to_local
(Backtrace restricted to imported tasks)
cap aborted!
NoMethodError: undefined method `instance' for Capistrano::Configuration:Class

(See full trace by running task with --trace)

我把文件放错了地方吗?我该如何使用此任务?

1 个答案:

答案 0 :(得分:1)

这是一个capistrano2.x任务,你不能用capistrano3运行。

您应该使用cap2.x或重写它。

我试过,但我不确定这是否有效:

namespace :db do
  task :backup_name do
    on primary(:db) 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/#{fetch(:environment_database)}-snapshot-#{backup_time}.sql"
    end
  end

  desc "Backup your MySQL or PostgreSQL database to shared_path+/db_backups"
  task dump: :backup_name do
    on primary(:db) do
      run("cat #{shared_path}/config/database.yml") { |channel, stream, data| @environment_info = YAML.load(data)[fetch(:rails_env)] }
      if @environment_info['adapter'] == 'mysql'
        dbhost = @environment_info['host']
        dbhost = fetch(:environment_dbhost).sub('-master', '') + '-replica' if dbhost != 'localhost' # added for Solo offering, which uses localhost
        run "mysqldump --add-drop-table -u #{fetch(:dbuser)} -h #{dbhost} -p #{fetch(:environment_database)} | bzip2 -c > #{fetch(:backup_file)}.bz2" do |ch, stream, out |
          ch.send_data "#{fetch(:dbpass)}\n" if out=~ /^Enter password:/
        end
      else
        run "pg_dump -W -c -U #{fetch(:dbuser)} -h #{fetch(:environment_dbhost)} #{fetch(:environment_database)} | bzip2 -c > #{fetch(:backup_file)}.bz2" do |ch, stream, out |
          ch.send_data "#{fetch(:dbpass)}\n" if out=~ /^Password:/
        end
      end
    end
  end

  desc "Sync your production database to your local workstation"
  task clone_to_local: :dump do
    get "#{fetch(:backup_file)}.bz2", "/tmp/#{fetch(:application)}.sql.bz2"
    development_info = YAML.load_file("config/database.yml")['development']
    if development_info['adapter'] == 'mysql'
      run_str = "bzcat /tmp/#{fetch(:application)}.sql.bz2 | mysql -u #{development_info['username']} --password='#{development_info['password']}' -h #{development_info['host']} #{development_info['database']}"
    else
      run_str = "PGPASSWORD=#{development_info['password']} bzcat /tmp/#{fetch(:application)}.sql.bz2 | psql -U #{development_info['username']} -h #{development_info['host']} #{development_info['database']}"
    end
    %x!#{run_str}!
  end
end