一切正常,直到第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
答案 0 :(得分: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, '')
# ...