ubuntu文件传输,分布式系统搭建

来源:互联网 发布:互动百科排名优化 编辑:程序博客网 时间:2024/06/05 22:28

每一项功能探索都是以需求为先的:

项目需求: 多机连用,批量将pdf进行提取文本和文本转换

方案:1. 文件共享式:一台主机(Master)共享文件,使用nfs;其他slaver读取共享文件夹中的内容;
2. 分布式:文件分发传输到每一台主机,每台机器都在本地执行脚本

总体来说,方案1,2的逻辑都大同小异,具体实施的过程中,共享式的文件系统不稳定(原因暂不明确)

方案2主要需要解决问题:1>文件传输;2>远程脚本执行;3>系统调度和平衡负载

这一篇主要讲文件传输:
工具 SCP

scp是有Security的文件copy,基于ssh登录

scp allows files to be copied to, from, or between different hosts. It
uses ssh for data transfer and provides the same authentication and
same level of security as ssh.

  1. 文件

1.1远程到本地

 $ scp your_username@remotehost.edu:foobar.txt /some/local/directory 

1.2本地到远程<单文件,多文件>

 $ scp foobar.txt your_username@remotehost.edu:/some/remote/directory  $ scp foo.txt bar.txt your_username@remotehost.edu:~

1.3远程到远程

 $ scp your_username@rh1.edu:/some/remote/directory/foobar.txt \your_username@rh2.edu:/some/remote/directory/ 
  1. 文件夹

2.1本地到远程<其他参考文件操作>

 $ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar 

3.其他用法
3.1用其他端口

 $ scp -P 2264 foobar.txt your_username@remotehost.edu:/some/remote/directory 

3.2读取多个文件

  $ scp your_username@remotehost.edu:/some/remote/directory/\{a,b,c\} .   $ scp your_username@remotehost.edu:~/\{foo.txt,bar.txt\} . 

3.3换加密方式提速

  $ scp -c blowfish some_file your_username@remotehost.edu:~

3.4压缩<注:网速慢时用,因为压缩耗费CPU资源>

   $ scp -c blowfish -C local_file your_username@remotehost.edu:~ 

项目中遇到的问题:
使用语言:python
scp多个数据时(十万级pdf文件),最初传输挺快,后期(十万以后)巨慢,百度了半天没找到原因,自己追终了一下代码流程,原来是自己傻逼了。
楼主的代码,传输多个文件

$ scp your_username@remotehost.edu:/some/remote/file1 your_username@remotehost.edu:/some/remote/file2 ...your_username@remotehost.edu:/some/remote/filen ./

读者你能猜到出了什么问题吗?没错:
这样的代码,每传输一次都需要ssh登陆一次,后期ssh登陆的验证极其慢:
所以可以:
1>优化ssh的登陆时间;
2>使用前面提供的传输多文件的方式{pdf1,pdf2},这样就只有在最开始需要ssh一次

$ scp your_username@remotehost.edu:/some/remote/directory/\{file1,file2,...,filen\} ./
0 0
原创粉丝点击