2014-01-11 43 views
0

場景: 我必須將大約3000個文件(每個服務器30到35 MB)從一臺服務器轉移到另一臺服務器(兩臺服務器都是IBM-AIX服務器)。 這些文件格式爲.gz格式。使用gunzip命令將它們解壓縮到目的地。非順序ftp腳本

我現在這樣做的方式: 我製作了包含每個500個文件的ftp腳本的.sh文件。這些.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 

線正在執行時,用於下一個批次即FTP爲(file_00028911 ftp的* gz)已暫停,因此會浪費大量時間並損失帶寬利用率。 The!標記用於在ftp提示符下運行操作系統命令。

希望我已經正確地解釋了我的方案。如果我得到一個解決方案,如果任何一個解決方案已經有答案,將更新帖子。

Regards Yash。

+0

你可以使用rsync?另外,scp可以複製動態壓縮(這是選項「-C」)。 –

回答

0

由於您似乎在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 
} 

它沒有測試,但至少它通過了語法檢查。

0

兩種解決方案之一。不要直接調用gunzip。呼叫「嗒嗒」和「嗒嗒」是一個腳本:

#!/bin/sh 
gunzip "[email protected]" & 

所以gunzip解投入的背景下,該腳本將立即返回,和你繼續與FTP。另一個想法是將&添加到sh命令中 - 我敢打賭,這樣做也會起作用。即在ftp腳本中,請執行以下操作:

! gunzip file_00028914*gz & 

但是......我相信你有點讓自己誤入歧途。由於很多原因,rsync和其他解決方案都是有用的。