如何将文件从一台服务器复制到另一台服务器?

时间:2022-11-15 19:52:50

I have one server which has nothing but xls log files. Each file is 5-15Mb and it is dynamic in the sense that files get added at any point of time. Now I need a way to do the following process using Ruby.

我有一台服务器,只有xls日志文件。每个文件都是5-15Mb,并且在任何时候都可以添加文件的意义上它是动态的。现在我需要一种方法来使用Ruby来执行以下过程。

  1. Copy a file by sending the filename from one server which has nothing but log files to another server.
  2. 通过从一台服务器发送文件名来复制文件,该服务器只有日志文件到另一台服务器。

  3. I need to pass the server password as an argument.
  4. 我需要传递服务器密码作为参数。

  5. Everything happens in the background, which is triggered from a Ruby script.
  6. 一切都在后台发生,这是从Ruby脚本触发的。

4 个解决方案

#1


10  

Check out the Net::SCP and Net::SSH gems. The first lets you retrieve a file using a secure copy, and the second will let you easily find the names of the files available for retrieval. In Net::SSH, ssh.exec! will be your friend.

查看Net :: SCP和Net :: SSH宝石。第一个允许您使用安全副本检索文件,第二个允许您轻松找到可供检索的文件的名称。在Net :: SSH中,ssh.exec!将是你的朋友。

From the Net::SCP docs:

来自Net :: SCP文档:

Net::SCP implements the SCP (Secure CoPy) client protocol, allowing Ruby programs to securely and programmatically transfer individual files or entire directory trees to and from remote servers. It provides support for multiple simultaneous SCP copies working in parallel over the same connection, as well as for synchronous, serial copies.

Net :: SCP实现了SCP(Secure CoPy)客户端协议,允许Ruby程序安全地以编程方式将单个文件或整个目录树传输到远程服务器或从远程服务器传输。它支持在同一连接上并行工作的多个同时SCP副本,以及同步,串行副本。

Net::SCP also provides an open-uri tie-in, so you can use the Kernel#open method to open and read a remote file:

Net :: SCP还提供了open-uri绑定,因此您可以使用Kernel#open方法打开并读取远程文件:

  # if you want to read from a URL voa SCP:
  require 'uri/open-scp'
  puts open("scp://user@remote.host/path/to/file").read

From the Net::SSH docs:

来自Net :: SSH文档:

require 'net/ssh'

Net::SSH.start('host', 'user', :password => "password") do |ssh|
  # capture all stderr and stdout output from a remote process
  output = ssh.exec!("hostname")

Add an end to the above code to close the block. Inside the block, output will contain the results of the command you sent.

在上面的代码中添加一个结尾以关闭该块。在块内,输出将包含您发送的命令的结果。

An alternate to retrieving the files via Ruby from the machine containing the files, would be to have Ruby initiate the transfer directly from the machine hosting the files and push them via scp to the other machine.

从包含文件的机器通过Ruby检索文件的替代方法是让Ruby直接从托管文件的机器启动传输,并通过scp将它们推送到另一台机器。

Instead of using Net::SCP and Net::SSH, you could use Net::SFTP, to manage it all in one gem. It rides on a secure connection too, but SFTP might not be available to you. The Net::SFTP::Operations::Dir and Net::SFTP::Operations::Download classes and docs will be your friend.

您可以使用Net :: SFTP在一个gem中管理它,而不是使用Net :: SCP和Net :: SSH。它也依赖于安全连接,但您可能无法使用SFTP。 Net :: SFTP :: Operations :: Dir和Net :: SFTP :: Operations ::下载类和文档将成为您的朋友。

Other options include using the standard rsync in a simple shell as @tadman mentioned. There are a multitude of ways of accomplishing this, and it is a common need in hosting environments.

其他选项包括在@tadman提到的简单shell中使用标准rsync。有许多方法可以实现这一点,这是托管环境中的常见需求。


any other better approach?

任何其他更好的方法?

rsync, at the command-line. It's very smart and can move folders and deltas of files if needed. Also, "How to transfer files using ssh and Ruby" and its link to "Ruby file upload ssh intro".

rsync,在命令行。它非常智能,可以根据需要移动文件夹和文件的增量。另外,“如何使用ssh和Ruby传输文件”及其链接到“Ruby file upload ssh intro”。

Melding @tadman's rsync recommendation with Ruby, there's "Cheapest rsync replacement (with Ruby)".

Melding @ tadman对Ruby的rsync建议,有“最便宜的rsync替换(使用Ruby)”。

#2


4  

This is how it worked

这就是它的工作原理

I used the net-ssh & net-scp gem as suggested by @theTinMan and i was able to copy my files.

我使用了@theTinMan建议的net-ssh和net-scp gem,我能够复制我的文件。

require 'rubygems'
require 'net/ssh'
require 'net/scp'

Net::SSH.start("ip_address", "username",:password => "*********") do |session|
  session.scp.download! "/home/logfiles/2-1-2012/login.xls", "/home/anil/Downloads"
end

and to a copy a entire folder

并复制整个文件夹

require 'rubygems'
require 'net/ssh'
require 'net/scp'

Net::SSH.start("ip_address", "username",:password => "*********") do |session|
  session.scp.download!("/home/logfiles/2-1-2012", "/home/anil/Downloads",  :recursive => true)
end

#3


1  

You should probably just use rsync instead of rolling your own thing. Use ssh with public/private key access and you will avoid a password. Using a password at all is probably a bad idea.

您应该只使用rsync而不是滚动自己的东西。使用带公钥/私钥访问的ssh,您将避免使用密码。完全使用密码可能是一个坏主意。

#4


0  

Seems it is possible without scp

似乎没有scp是可能的

@session = Net::SSH.start(@ftpIp, @ftpUser, :password => @ftpPass)
@conn = Net::SFTP::Session.new(@session).connect!

def download_remote_folder(remote_path, local_path, use_ssh = @use_ssh)
    @conn ||= connect
    if (use_ssh)
      @conn.download!(remote_path,local_path,:recursive => true)
    else
      @conn.get(remote_path, local_path)
    end
  end

  def download_remote_file(remote_path, local_path, use_ssh = @use_ssh)
    @conn ||= connect
    if (use_ssh)
      @conn.download!(remote_path,local_path)
    else
      @conn.get_file(remote_path, local_path)
    end
  end

#1


10  

Check out the Net::SCP and Net::SSH gems. The first lets you retrieve a file using a secure copy, and the second will let you easily find the names of the files available for retrieval. In Net::SSH, ssh.exec! will be your friend.

查看Net :: SCP和Net :: SSH宝石。第一个允许您使用安全副本检索文件,第二个允许您轻松找到可供检索的文件的名称。在Net :: SSH中,ssh.exec!将是你的朋友。

From the Net::SCP docs:

来自Net :: SCP文档:

Net::SCP implements the SCP (Secure CoPy) client protocol, allowing Ruby programs to securely and programmatically transfer individual files or entire directory trees to and from remote servers. It provides support for multiple simultaneous SCP copies working in parallel over the same connection, as well as for synchronous, serial copies.

Net :: SCP实现了SCP(Secure CoPy)客户端协议,允许Ruby程序安全地以编程方式将单个文件或整个目录树传输到远程服务器或从远程服务器传输。它支持在同一连接上并行工作的多个同时SCP副本,以及同步,串行副本。

Net::SCP also provides an open-uri tie-in, so you can use the Kernel#open method to open and read a remote file:

Net :: SCP还提供了open-uri绑定,因此您可以使用Kernel#open方法打开并读取远程文件:

  # if you want to read from a URL voa SCP:
  require 'uri/open-scp'
  puts open("scp://user@remote.host/path/to/file").read

From the Net::SSH docs:

来自Net :: SSH文档:

require 'net/ssh'

Net::SSH.start('host', 'user', :password => "password") do |ssh|
  # capture all stderr and stdout output from a remote process
  output = ssh.exec!("hostname")

Add an end to the above code to close the block. Inside the block, output will contain the results of the command you sent.

在上面的代码中添加一个结尾以关闭该块。在块内,输出将包含您发送的命令的结果。

An alternate to retrieving the files via Ruby from the machine containing the files, would be to have Ruby initiate the transfer directly from the machine hosting the files and push them via scp to the other machine.

从包含文件的机器通过Ruby检索文件的替代方法是让Ruby直接从托管文件的机器启动传输,并通过scp将它们推送到另一台机器。

Instead of using Net::SCP and Net::SSH, you could use Net::SFTP, to manage it all in one gem. It rides on a secure connection too, but SFTP might not be available to you. The Net::SFTP::Operations::Dir and Net::SFTP::Operations::Download classes and docs will be your friend.

您可以使用Net :: SFTP在一个gem中管理它,而不是使用Net :: SCP和Net :: SSH。它也依赖于安全连接,但您可能无法使用SFTP。 Net :: SFTP :: Operations :: Dir和Net :: SFTP :: Operations ::下载类和文档将成为您的朋友。

Other options include using the standard rsync in a simple shell as @tadman mentioned. There are a multitude of ways of accomplishing this, and it is a common need in hosting environments.

其他选项包括在@tadman提到的简单shell中使用标准rsync。有许多方法可以实现这一点,这是托管环境中的常见需求。


any other better approach?

任何其他更好的方法?

rsync, at the command-line. It's very smart and can move folders and deltas of files if needed. Also, "How to transfer files using ssh and Ruby" and its link to "Ruby file upload ssh intro".

rsync,在命令行。它非常智能,可以根据需要移动文件夹和文件的增量。另外,“如何使用ssh和Ruby传输文件”及其链接到“Ruby file upload ssh intro”。

Melding @tadman's rsync recommendation with Ruby, there's "Cheapest rsync replacement (with Ruby)".

Melding @ tadman对Ruby的rsync建议,有“最便宜的rsync替换(使用Ruby)”。

#2


4  

This is how it worked

这就是它的工作原理

I used the net-ssh & net-scp gem as suggested by @theTinMan and i was able to copy my files.

我使用了@theTinMan建议的net-ssh和net-scp gem,我能够复制我的文件。

require 'rubygems'
require 'net/ssh'
require 'net/scp'

Net::SSH.start("ip_address", "username",:password => "*********") do |session|
  session.scp.download! "/home/logfiles/2-1-2012/login.xls", "/home/anil/Downloads"
end

and to a copy a entire folder

并复制整个文件夹

require 'rubygems'
require 'net/ssh'
require 'net/scp'

Net::SSH.start("ip_address", "username",:password => "*********") do |session|
  session.scp.download!("/home/logfiles/2-1-2012", "/home/anil/Downloads",  :recursive => true)
end

#3


1  

You should probably just use rsync instead of rolling your own thing. Use ssh with public/private key access and you will avoid a password. Using a password at all is probably a bad idea.

您应该只使用rsync而不是滚动自己的东西。使用带公钥/私钥访问的ssh,您将避免使用密码。完全使用密码可能是一个坏主意。

#4


0  

Seems it is possible without scp

似乎没有scp是可能的

@session = Net::SSH.start(@ftpIp, @ftpUser, :password => @ftpPass)
@conn = Net::SFTP::Session.new(@session).connect!

def download_remote_folder(remote_path, local_path, use_ssh = @use_ssh)
    @conn ||= connect
    if (use_ssh)
      @conn.download!(remote_path,local_path,:recursive => true)
    else
      @conn.get(remote_path, local_path)
    end
  end

  def download_remote_file(remote_path, local_path, use_ssh = @use_ssh)
    @conn ||= connect
    if (use_ssh)
      @conn.download!(remote_path,local_path)
    else
      @conn.get_file(remote_path, local_path)
    end
  end