Changeset 39 for trunk


Ignore:
Timestamp:
11/21/05 19:04:46 (19 years ago)
Author:
thauvin
Message:
  • rework transfert, create md5sum file
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/soft/scripts/transfert

    • Property cvs2svn:cvs-rev changed from 1.5 to 1.6
    r38 r39  
    2020use POSIX qw(strftime); 
    2121use Pod::Usage; 
     22use Digest::MD5; 
    2223 
    2324# 0 .. 6 => debug, info, report, warn, error, die 
     
    184185sub transfert { 
    185186    my ($entry) = @_; 
    186     my $rsync = $config->val($entry, 'rsync', 'rsync'); 
    187     $rsync .= ' --log-format "TX %o %t %b %l %f"'; 
    188     $rsync .= ' -lptgoDH'; 
    189     $rsync .= ' ' . $config->val($entry, 'source'); 
    190     $rsync .= ' ' . $config->val($entry, 'dest'); 
    191     loging(0, "Rsync cmd: %s", $rsync); 
    192     open(my $hrsync, "$rsync |") or do { 
    193         loging(5, "Can exec rsync"); 
    194         return; 
     187    my $source = $config->val($entry, 'source') or do { 
     188        loging(5, "No source defined for %s", $entry); 
     189        return 0; 
    195190    }; 
    196     my @files; 
    197     while(my $line = <$hrsync>) { 
    198         my ($status, $time, $send, $size, $file) = 
    199             $line =~ m|^TX (\S*) (..../../.. ..:..:..) (\d+) (\d+) (.*)$|; 
    200         $status or next; 
    201         push(@files, { file => $file, size => $size }); 
    202         loging(1, "[%s] File '%s' transmit (size: %d)", $time, $file, $size); 
    203     } 
    204     my $res = close($hrsync); 
    205     if ($?) { 
    206         loging(4, "rsync exit with non 0 status: %d", $? >> 8); 
    207     } else { 
     191 
     192    my $dest = $config->val($entry, 'dest') or do { 
     193        loging(5, "No destination defined for %s", $entry); 
     194        return 0; 
     195    }; 
     196 
     197    my @files = -d $source ? glob("$source/*") : glob($source); 
     198    my @datafiles; 
     199     
     200    foreach my $f (@files) { 
     201        if ($f =~ /.md5sum$/) { 
     202            loging(0, "%s is md5sum file, skipping", $f); 
     203            next; 
     204        } 
     205         
     206        if (! -f $f) { 
     207            loging(0, "%s is not a regular file, skipping", $f); 
     208            next; 
     209        } 
     210        my ($fsize, $ftime); 
     211        { 
     212            my @st = stat($f); 
     213            $fsize = $st[7]; 
     214            $ftime = $st[9]; 
     215        } 
     216         
     217        # Does the transfert need 
     218        if (-f "$f.md5sum") { 
     219            if ((stat("$f.md5sum"))[9] > $ftime) { 
     220                loging(0, "%s exists and is newer, assume transfert is not need", "$f.md5sum"); 
     221                next; 
     222            } 
     223        } 
     224         
     225        # Creating md5sum file 
     226        if(open(my $md5h, "> $f.md5sum")) { 
     227            if (open(my $fh, "< $f")) { 
     228                loging(0, "Creating %s md5 file", "$f.md5sum"); 
     229                binmode($fh); 
     230                my $md5 = new Digest::MD5; 
     231                $md5->addfile($fh); 
     232                printf $md5h  
     233                        "# %s\n# %s (%d octets)\n# %s\n%s  %s\n", 
     234                        q$Id$, 
     235                        $f, $fsize, 
     236                        scalar(localtime), 
     237                        $md5->hexdigest, 
     238                        $f; 
     239                close($fh); 
     240            } else { 
     241                login(5, "Can't open %s for reading", $f); 
     242            } 
     243            close($md5h); 
     244        } else { 
     245            login(5, "Can't open %s for writting", "$f.md5sum"); 
     246        } 
     247 
     248        my $error = 0; 
     249        foreach my $filetotranfert (grep { -f $_ } ("$f.md5sum", $f)) { 
     250            my $cmd = sprintf( 
     251                "%s %s %s", 
     252                $config->val($entry, 'cmd', 'rsync -e ssh'), 
     253                $filetotranfert, 
     254                $dest, 
     255            ); 
     256            loging(0, "running `%s'", $cmd); 
     257            if (system($cmd)) { 
     258                loging(4, "tranfert failed for %s, exit code: %d", $filetotranfert, $? >> 8); 
     259                unlink("$f.md5sum"); # ensure transfert will be retried 
     260                $error++; 
     261            } 
     262        } 
     263         
     264        if ($error) { 
     265            loging(5, "Failed to transfert %s", $f); 
     266            next; 
     267        } else { 
     268            loging(1, "%s (%d) transfer ok", $f, $fsize); 
     269            push(@datafiles, { file => $f, size => $fsize }); 
     270        } 
     271 
    208272        # No error running action: 
    209273        if (my $postrun = $config->val($entry, 'postrun')) { 
    210274            loging(0, "found postrun: %s for %s", $postrun || '(unset)', $entry); 
    211             foreach my $f (@files) { 
    212                 my $filename = "/$f->{file}"; 
    213                 my $fpostrun = sprintf($postrun, $filename); 
     275            foreach my $filetotranfert (grep { -f $_ } ("$f.md5sum", $f)) { 
     276                my $fpostrun = sprintf($postrun, $filetotranfert); 
    214277                loging(0, "Running `%s'", $fpostrun); 
    215278                my $postrunres = system($fpostrun); 
    216                  
     279                loging(0, "Postrun exit: %d", $postrunres); 
    217280            } 
    218281        } 
    219282    } 
     283 
    220284    my $totalsize = 0; 
    221     $totalsize += $_->{size} foreach (@files); 
    222     loging(2, "%s: %d file transmit, %d octets", $entry, scalar(@files), $totalsize); 
     285    $totalsize += $_->{size} foreach (@datafiles); 
     286    loging(2, "%s: %d file transmit, %d octets", $entry, scalar(@datafiles), $totalsize); 
     287     
    223288    return 0; 
    224289} 
     
    257322 
    258323    $Log$ 
     324    Revision 1.6  2005/11/21 18:04:46  thauvin 
     325    - rework transfert, create md5sum file 
     326 
    259327    Revision 1.5  2005/11/18 18:40:27  thauvin 
    260328    - add postrun doc 
Note: See TracChangeset for help on using the changeset viewer.