【问题标题】:ruby filesync gem, uninitialized constant Find ( NameError)ruby filesync gem,未初始化的常量查找(NameError)
【发布时间】:2014-05-21 14:55:05
【问题描述】:

一切正常,直到第 32 行我试图找到哪些文件和目录需要上传到备份服务器。你能帮我把文件从 local_path 同步到 remote_path 吗?谢谢您的帮助!

当我运行它时,我得到:

box.ru:32:in `block (2 levels) in <main>': uninitialized constant Find (NameError)
from /Library/Ruby/Gems/2.0.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:769:in `call'
from /Library/Ruby/Gems/2.0.0/gems/net-sftp-2.1.2/lib/net/sftp/session.rb:769:in `connect'
from newbox.ru:28:in `block in <main>'
from /Library/Ruby/Gems/2.0.0/gems/net-ssh-2.9.1/lib/net/ssh.rb:210:in `start'
from newbox.ru:25:in `<main>'
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
require 'net/sftp'
require 'highline/import'


host = 'server'
local_path = '/Users/awesome1/Development/box'
remote_path = '/home/awesome2/box'

def sanitize_string(string_name)
string_name.gsub(/[^\w\.\-]/,"_")
end


puts "box username:"
user0 = gets.chomp
user = sanitize_string(user0)

pass0 = ask("Enter password for #{user}: ") { |q| q.echo = false }
pass = sanitize_string(pass0)

puts 'connecting to box...'
Net::SSH.start( host, user, :password => pass ) do|ssh|
result = ssh.exec!("cd #{remote_path} && ls")
 puts result
   ssh.sftp.connect do |sftp| 
 puts 'Checking for files which need updating'
 Find.find(local_path) do |file|
     next if File.stat(file).directory?
     local_file = "#{dir}/#{file}"
     remote_file = remote_path + local_file.sub(local_path, '')

     begin
     remote_dir = File.dirname(remote_file)
     sftp.stat(remote_dir)
 rescue Net::SFTP::Operations::StatusException => e
   raise unless e.code == 2
   sftp.mkdir(remote_dir, :mode => dir_perm)
 end 
   begin
   rstat = sftp.stat(remote_file)
 rescue Net::SFTP::Operations::StatusException => e
   raise unless e.code == 2
   sftp.put_file(local_file, remote_file)
   sftp.setstat(remote_file, :permissions => file_perm)
   next
 end

 if File.stat(local_file).mtime > Time.at(rstat.mtime)
   puts "Copying #{local_file} to #{remote_file}"
   ssh.sftp.upload(local_file, remote_file)
  end
 end
end
end 

【问题讨论】:

    标签: ruby ssh initialization sftp


    【解决方案1】:

    您的box.ru 缺少require 'find' 行:

    require 'find'
    
    puts 'connecting to box...'
    Net::SSH.start( host, user, :password => pass ) do|ssh|
    result = ssh.exec!("cd #{remote_path} && ls")
     puts result
       ssh.sftp.connect do |sftp| 
     puts 'Checking for files which need updating'
     Find.find(local_path) do |file|
         next if File.stat(file).directory?
         local_file = "#{dir}/#{file}"
         remote_file = remote_path + local_file.sub(local_path, '')
     # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多