Changeset 257


Ignore:
Timestamp:
10/13/06 00:21:13 (18 years ago)
Author:
nanardon
Message:
  • transfert are done for whole directory, not file by file
add -ttest options
  • add support of a max size for the repository, old file making this size exceeded are trashed
Location:
trunk/soft/scripts
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/soft/scripts/transfert

    r42 r257  
    2121use Pod::Usage; 
    2222use Digest::MD5; 
     23use File::Find; 
     24use File::Temp; 
    2325 
    2426# 0 .. 6 => debug, info, report, warn, error, die 
     
    3335    'logfile=s' => \my $logfile, 
    3436    'help|h' => sub { pod2usage(-exitval => 0) }, 
     37    't|test' => \my $test, 
    3538) or pod2usage(); 
    3639 
     
    227230    }; 
    228231 
    229     my @files = -d $source ? glob("$source/*") : glob($source); 
    230     my @datafiles; 
    231      
    232     foreach my $f (@files) { 
    233         if ($f =~ /.md5sum$/) { 
    234             loging(0, "%s is md5sum file, skipping", $f); 
    235             next; 
     232    # Searching files 
     233    my @files; 
     234    find( 
     235        { 
     236            wanted => sub { 
     237                -d $File::Find::name and return; 
     238                my @stats = stat _ or return; 
     239                push(@files, { 
     240                    f => $File::Find::name, 
     241                    s => $stats[7], 
     242                    t => $stats[9], 
     243                }); 
     244            }, 
     245        }, 
     246        $source, 
     247    ); 
     248 
     249    my @cmd = ( 
     250        split(/ /, $config->val($entry, 'cmd', 'rsync -avPH')), 
     251        $source, 
     252        $dest, 
     253    ); 
     254    loging(0, "running `%s'", join(' ', @cmd)); 
     255    my $error = 0; 
     256    while ($error < 3) { 
     257        my $exit = $test ? 0 : system(@cmd); 
     258        if ($exit == 0) { 
     259            # Wahou, success ! 
     260            $error = 0; 
     261            last; 
     262        } elsif ($exit == -1) { 
     263            loging(4, "tranfert failed unable to start `%s'", $cmd[0]); 
     264            $error++; 
     265        } elsif ($?) { 
     266            $error++; 
     267            $error < 3 and next; 
     268            loging(4, "tranfert failed, exit code: %d", $? >> 8); 
    236269        } 
    237          
    238         if (! -f $f) { 
    239             loging(0, "%s is not a regular file, skipping", $f); 
    240             next; 
     270    } 
     271    $error and return 0; 
     272 
     273    # No error running action: 
     274    if (my $postrun = $config->val($entry, 'postrun')) { 
     275        loging(0, "found postrun: %s for %s", $postrun || '(unset)', $entry); 
     276        my $fpostrun = sprintf($postrun); 
     277        loging(0, "Running `%s'", $fpostrun); 
     278        my $postrunres = system($fpostrun); 
     279        loging(0, "Postrun exit: %d", $postrunres); 
     280    } 
     281 
     282    if (my $maxsize = $config->val($entry, 'maxsize')) { 
     283        my $size = 0; 
     284        foreach my $f (sort { ($a->{t} || 0) <=> ($b->{t} || 0) } @files) { 
     285            if($size + $f->{s} > $maxsize) { 
     286                loging(0, "Trashing old %s because size exceed", $f->{f}); 
     287                unlink($f->{f}); 
     288            } 
     289            $size += $f->{s} || 0; 
    241290        } 
    242         my ($fsize, $ftime); 
    243         { 
    244             my @st = stat($f); 
    245             $fsize = $st[7]; 
    246             $ftime = $st[9]; 
    247         } 
    248          
    249         # Does the transfert need 
    250         if (-f "$f.md5sum") { 
    251             if ((stat("$f.md5sum"))[9] > $ftime) { 
    252                 loging(0, "%s exists and is newer, assume transfert is not need", "$f.md5sum"); 
    253                 next; 
    254             } 
    255         } 
    256          
    257         # Creating md5sum file 
    258         if(open(my $md5h, "> $f.md5sum")) { 
    259             if (open(my $fh, "< $f")) { 
    260                 loging(0, "Creating %s md5 file", "$f.md5sum"); 
    261                 binmode($fh); 
    262                 my $md5 = new Digest::MD5; 
    263                 $md5->addfile($fh); 
    264                 printf $md5h  
    265                         "# %s\n# %s (%d octets)\n# %s\n%s  %s\n", 
    266                         q$Id$, 
    267                         $f, $fsize, 
    268                         scalar(localtime), 
    269                         $md5->hexdigest, 
    270                         ($f =~ m!(?:.*/)?(.*)$!)[0]; 
    271                 close($fh); 
    272             } else { 
    273                 login(5, "Can't open %s for reading", $f); 
    274             } 
    275             close($md5h); 
    276         } else { 
    277             login(5, "Can't open %s for writting", "$f.md5sum"); 
    278         } 
    279  
    280         my $error = 0; 
    281         foreach my $filetotranfert (grep { -f $_ } ("$f.md5sum", $f)) { 
    282             my $cmd = sprintf( 
    283                 "%s %s %s", 
    284                 $config->val($entry, 'cmd', 'rsync -e ssh'), 
    285                 $filetotranfert, 
    286                 $dest, 
    287             ); 
    288             loging(0, "running `%s'", $cmd); 
    289             if (system($cmd)) { 
    290                 loging(4, "tranfert failed for %s, exit code: %d", $filetotranfert, $? >> 8); 
    291                 unlink("$f.md5sum"); # ensure transfert will be retried 
    292                 $error++; 
    293             } 
    294         } 
    295          
    296         if ($error) { 
    297             loging(5, "Failed to transfert %s", $f); 
    298             next; 
    299         } else { 
    300             loging(1, "%s (%d) transfer ok", $f, $fsize); 
    301             push(@datafiles, { file => $f, size => $fsize }); 
    302         } 
    303  
    304         # No error running action: 
    305         if (my $postrun = $config->val($entry, 'postrun')) { 
    306             loging(0, "found postrun: %s for %s", $postrun || '(unset)', $entry); 
    307             foreach my $filetotranfert (grep { -f $_ } ("$f.md5sum", $f)) { 
    308                 my $fpostrun = sprintf($postrun, $filetotranfert); 
    309                 loging(0, "Running `%s'", $fpostrun); 
    310                 my $postrunres = system($fpostrun); 
    311                 loging(0, "Postrun exit: %d", $postrunres); 
    312             } 
    313         } 
    314     } 
    315  
    316     my $totalsize = 0; 
    317     $totalsize += $_->{size} foreach (@datafiles); 
    318     loging(2, "%s: %d file transmit, %d octets", $entry, scalar(@datafiles), $totalsize); 
    319      
    320     return 0; 
     291    } 
     292 
     293    return 1; 
    321294} 
    322295 
  • trunk/soft/scripts/transfert-config

    r37 r257  
    55 
    66[test] 
    7 source=/home/users/olivier/lidarcvs/soft/scripts/* 
     7source=/home/users/olivier/myprojects/obsdata/ 
    88dest=/net/tmp/s/ 
    99mailto=thauvin@aerov.jussieu.fr, olivier.thauvin@aero.jussieu.fr 
    10 postrun= cp %s /tmp 
     10maxsize = 53791040 
     11#postrun= cp %s /tmp 
Note: See TracChangeset for help on using the changeset viewer.