【问题标题】:Non sequential ftp script非顺序 ftp 脚本
【发布时间】:2014-01-11 22:39:27
【问题描述】:

场景: 我必须将大约 3000 个文件,每个 30 到 35 MB 从一台服务器传输到另一台服务器(两台服务器都是 IBM-AIX 服务器)。 这些文件为 .gz 格式。它们在目的地使用 gunzip 命令解压到 b 使用。

我现在的做法: 我制作了 .sh 文件,每个文件包含 500 个文件的 ftp 脚本。这些 .sh 文件在运行时,将文件传输到目的地。在目的地,我继续检查有多少文件到达,一旦有 100 个文件到达,我对这 100 个文件运行 gunzip,然后对接下来的 100 个文件再次运行相同的文件,依此类推。我为一批 100 个运行 gunzip 只是为了节省时间。

我在想什么: 我正在寻找可以将我的文件 ftp 传输到目的地的命令或任何其他方式,并且一旦传输了 100 个文件,它们就会开始解压缩 ,此解压缩不应暂停传输剩余文件。

我尝试过的脚本:

ftp -n 192.168.0.22 << EOF
quote user username
quote pass password
cd /gzip_files/files
lcd /unzip_files/files
prompt n
bin
mget file_00028910*gz
! gunzip file_00028910*gz
mget file_00028911*gz
! gunzip file_00028911*gz
mget file_00028912*gz
! gunzip file_00028912*gz
mget file_00028913*gz
! gunzip file_00028913*gz
mget file_00028914*gz
! gunzip file_00028914*gz
bye

上述代码的缺点是当

! gunzip file_00028910*gz

lines 正在执行,下一批的 ftp 即 (file_00028911*gz) 的 ftp 被暂停,因此浪费了大量时间和带宽利用率损失。 这 !标记用于在 ftp 提示符下运行操作系统命令。

希望我已经正确解释了我的情况。如果我得到解决方案,将更新帖子,如果有人已经有解决方案,请回复。

问候 亚什。

【问题讨论】:

  • 你可以使用rsync吗?此外,scp 可以使用动态压缩进行复制(即选项“-C”)。

标签: ftp aix nonsequential


【解决方案1】:

由于您似乎是在 UNIX 系统上执行此操作,因此您可能已经安装了 Perl。你可以试试下面的 Perl 代码:

use strict;
use warnings;
use Net::FTP;

my @files = @ARGV; # get files from command line

my $server = '192.168.0.22';
my $user   = 'username';
my $pass   = 'password';

my $gunzip_after = 100; # collect up to 100 files

my $ftp = Net::FTP->new($server) or die "failed connect to the server: $!";
$ftp->login($user,$pass) or die "login failed";

my $pid_gunzip;
while (1) {
    my @collect4gunzip;

    GET_FILES:
    while (my $file = shift @files) {
        my $local_file = $ftp->get($file);
        if ( ! $local_file ) {
            warn "failed to get $file: ".$ftp->message;
            next;
        }
        push @collect4gunzip,$local_file;
        last if @collect4gunzip == $gunzip_after;
    }

    @collect4gunzip or last; # no more files ?

    while ( $pid_gunzip && kill(0,$pid_gunzip)) {
        # gunzip is still running, wait because we don't want to run multiple
        # gunzip instances at the same time
        warn "wait for last gunzip to return...\n";
        wait();

        # instead of waiting for gunzip to return we could go back to retrieve
        # more files and add them to @collect4gunzip
        # goto GET_FILES;
    }

    # last gunzip is done, start to gunzip collected files
    defined( $pid_gunzip = fork()) or die "fork failed: $!";
    if ( ! $pid_gunzip ) {
        # child process should run gunzip
        # maybe one needs so split it into multipl gunzip calls to make
        # sure, that the command line does not get too long!!
        system( ['gunzip', @collect4gunzip ]);
        # child will exit once done
        exit(0);
    }

    # parent continues with getting more files
}

它没有经过测试,但至少它通过了语法检查。

【讨论】:

    【解决方案2】:

    两种解决方案之一。不要直接调用gunzip。调用“blah”和“blah”是一个脚本:

    #!/bin/sh
    gunzip "$@" &
    

    所以 gunzip 被放到后台,脚本立即返回,然后你继续 FTP。另一个想法是在 sh 命令中添加 & ——我敢打赌这也可以。即在 ftp 脚本中,执行:

    ! gunzip file_00028914*gz &
    

    但是……我相信你有点误入歧途。 rsync 和其他解决方案是可行的方法,原因有很多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-26
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多