Changeset 108 for trunk

Show
Ignore:
Timestamp:
04/08/10 23:20:33 (4 years ago)
Author:
depesz
Message:

1. Add handling of .backup files
2. Add more debug info (for --verbose)
3. fix bug with --remove-unneeded - it took argument, so next word after it was basically ignored!
4. Make it actually work

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/omnipitr/lib/OmniPITR/Program/Restore.pm

    r106 r108  
    1313use File::Copy; 
    1414use Storable; 
     15use Data::Dumper; 
    1516use Getopt::Long; 
    1617use Cwd; 
     
    7879    my $self         = shift; 
    7980    my $segment_name = shift; 
    80     return unless $self->{ 'pre-removal-processing' }; 
     81    return 1 unless $self->{ 'pre-removal-processing' }; 
    8182 
    8283    $self->prepare_temp_directory(); 
     
    8586    make_path( $xlog_dir ); 
    8687 
     88    my $comment = 'Copying segment ' . $segment_name . ' to ' . $xlog_file; 
     89    $self->log->time_start( $comment ) if $self->{ 'verbose' }; 
    8790    my $response = $self->copy_segment_to( $segment_name, $xlog_file ); 
     91    $self->log->time_finish( $comment ) if $self->{ 'verbose' }; 
     92 
    8893    if ( $response ) { 
    8994        $self->log->error( 'Error while copying segment for pre removal processing for %s : %s', $segment_name, $response ); 
     
    9398    my $previous_dir = gwtcwd(); 
    9499    chdir $self->{ 'temp-dir' }; 
     100 
    95101    my $full_command = $self->{ 'pre-removal-processing' } . " pg_xlog/$segment_name"; 
     102 
     103    my $comment = 'Running pre-removal-processing command: ' . $full_command; 
     104 
     105    $self->log->time_start( $comment ) if $self->{ 'verbose' }; 
    96106    my $result = run_command( $self->{ 'tempdir' }, 'bash', '-c', $full_command ); 
     107    $self->log->time_finish( $comment ) if $self->{ 'verbose' }; 
     108 
    97109    chdir $previous_dir; 
    98110 
     
    100112    return 1 unless $result->{ 'error_code' }; 
    101113 
    102     $self->log->error( 'Error while calling pre removal processing [%s] : %s', $full_command, Dumper( $result ) ); 
     114    $self->log->error( 'Error while calling pre removal processing [%s] : %s', $full_command, $result ); 
    103115 
    104116    return; 
     
    112124    my $dir; 
    113125 
    114     unless ( opendir( $dir, $self->{ 'source' } ) ) { 
     126    unless ( opendir( $dir, $self->{ 'source' }->{ 'path' } ) ) { 
    115127        $self->log->error( 'Cannot open source directory (%s) for reading: %s', $self->{ 'source' }->{ 'path' }, $OS_ERROR ); 
    116128        return; 
     
    122134    for my $file ( @content ) { 
    123135        $file =~ s/\Q$extension\E\z//; 
    124         next unless $file =~ m{\A[a-fA-F0-9]{24}\z}; 
     136        next unless $file =~ m{\A[a-fA-F0-9]{24}(?:\.[a-fA-F0-9]{8}\.backup)?\z}; 
    125137        next unless $file lt $last_important; 
    126138        push @too_old, $file; 
     
    128140    return if 0 == scalar @too_old; 
    129141 
     142    $self->log->log( '%u segments too old, to be removed.', scalar @too_old ) if $self->{ 'verbose' }; 
     143 
    130144    my @sorted = sort @too_old; 
    131145    splice( @sorted, $self->{ 'remove-at-a-time' } ); 
     
    157171    my $response = run_command( $self->{ 'temp-dir' }, $self->{ 'pgcontroldata-path' }, $self->{ 'data-dir' } ); 
    158172    if ( $response->{ 'error_code' } ) { 
    159         $self->log->error( 'Error while getting pg_controldata for %s: %s', $self->{ 'data-dir' }, Dumper( $response ) ); 
     173        $self->log->error( 'Error while getting pg_controldata for %s: %s', $self->{ 'data-dir' }, $response ); 
    160174        return; 
    161175    } 
     
    200214            $self->exit_with_status( 1 ); 
    201215        } 
     216        return; 
    202217    } 
    203218 
     
    207222        my @file_info  = stat( $wanted_file ); 
    208223        my $file_mtime = $file_info[ 9 ]; 
    209         my $ok_since   = time - $self->{ 'recovery-delay' }; 
    210         if ( $ok_since > $file_mtime ) { 
    211             if ( $self->{ 'verbose' } ) { 
    212                 unless ( $self->{ 'logged_delay' } ) { 
    213                     $self->log->log( 'Segment %s found, but it is too fresh (mtime = %u, accepted since %u)', $self->{ 'segment' }, $file_mtime, $ok_since ); 
    214                     $self->{ 'logged_delay' } = 1
    215                 } 
     224        my $ok_since   = time() - $self->{ 'recovery-delay' }; 
     225        if ( $ok_since <= $file_mtime ) { 
     226            if (   ( $self->{ 'verbose' } ) 
     227                && ( !$self->{ 'logged_delay' } ) ) 
     228            { 
     229                $self->log->log( 'Segment %s found, but it is too fresh (mtime = %u, accepted since %u)', $self->{ 'segment' }, $file_mtime, $ok_since )
     230                $self->{ 'logged_delay' } = 1; 
    216231            } 
    217232            return; 
     
    221236    my $full_destination = File::Spec->catfile( $self->{ 'data-dir' }, $self->{ 'segment_destination' } ); 
    222237 
     238    my $comment = 'Copying segment ' . $self->{ 'segment' } . ' to ' . $full_destination; 
     239    $self->log->time_start( $comment ) if $self->{ 'verbose' }; 
    223240    my $response = $self->copy_segment_to( $self->{ 'segment' }, $full_destination ); 
     241    $self->log->time_finish( $comment ) if $self->{ 'verbose' }; 
    224242 
    225243    if ( $response ) { 
     
    334352    ); 
    335353 
    336     croak( 'Error while reading command line arguments. Please check documentation in doc/omnipitr-archive.pod' ) 
     354    croak( 'Error while reading command line arguments. Please check documentation in doc/omnipitr-restore.pod' ) 
    337355        unless GetOptions( 
    338356        \%args, 
     
    349367        'recovery-delay|w=i', 
    350368        'removal-pause-trigger|p=s', 
    351         'remove-unneeded|r=s', 
     369        'remove-unneeded|r', 
    352370        'source|s=s', 
    353371        'temp-dir|t=s', 
     
    380398    @{ $self }{ qw( segment segment_destination ) } = @ARGV; 
    381399 
     400    $self->{ 'finish' } = ''; 
     401 
    382402    $self->log->log( 'Called with parameters: %s', join( ' ', @argv_copy ) ) if $self->{ 'verbose' }; 
    383  
    384     $self->{ 'finish' } = ''; 
    385403 
    386404    return; 
     
    401419    $self->log->fatal( 'Given data-dir (%s) is not valid', $self->{ 'data-dir' } ) unless -d $self->{ 'data-dir' } && -f File::Spec->catfile( $self->{ 'data-dir' }, 'PG_VERSION' ); 
    402420 
    403     $self->log->fatal( 'Given segment name is not valid (%s)', $self->{ 'segment' } ) unless $self->{ 'segment' } =~ m{\A[a-fA-F0-9]{24}\z}; 
     421    $self->log->fatal( 'Given segment name is not valid (%s)', $self->{ 'segment' } ) unless $self->{ 'segment' } =~ m{\A[a-fA-F0-9]{24}(?:\.[a-fA-F0-9]{8}\.backup)?\z}; 
    404422 
    405423    $self->log->fatal( 'Given source (%s) is not a directory', $self->{ 'source' }->{ 'path' } ) unless -d $self->{ 'source' }->{ 'path' };