BackupPC-Backups-Info

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

BackupPC-Backups-Info

Provides means for easily getting the current machines
backed up as well as information on the backups for those
machines by parsing the backups file for that machine.

bpc-info may also serve as a Nagios check as well.

INSTALLATION

To install this module, run the following commands:

	perl Makefile.PL
	make
	make test

bin/bpc-info  view on Meta::CPAN

use strict;
use warnings;
use Getopt::Std;
use BackupPC::Backups::Info;
use Time::Piece;
use Time::Seconds;

#print help
sub main::HELP_MESSAGE {
        print "\n".
			"-l   Show the last backup variables.\n".
			"-m <machine>   Specify a specific machine for the operation.\n".
			"-a <decimal days>  Search based on age.\n".
			"-e <equality>  The equality to use when searching based on age.\n".
			"-n   Nagios check mode.\n".
			"-c <crit level>  Number of machines not backed up to crit at. Default is 2.\n".
			"-i <file>  File with hosts to ignore.\n".
			"\n".
			"Equilities: either symbol or name can be used\n".
			"e, =\n".
			"lt, <\n".

bin/bpc-info  view on Meta::CPAN

			exit 2;
		}else{
			print "BACKUPS WARNING - ".$matched." ".$opts{e}." ".$opts{a}." days old;\n";
			exit 1;
		}
	}
}

=head1 NAME

bpc-info - A utility to get backup information from BackupPC in regards to backups.

=head1 SYNOPSIS

bpc-info B<-l> B<-m> <machine>
bpc-info B<-a> <days> B<-e> <equality>
bpc-info B<-a> <days> B<-e> <equality> B<-n> [B<-w> <warn level>] [B<-c> <critical level>] [B<-i> <ignore file>]

=head1 USAGE

Either B<-a> or B<-l> need to be given.

With B<-n>, B<-a> may be used as a nagios check and the output will be a simplified version
formated as such.

=head1 SWITCHES

=head2 B<-a> <days>

Deminal days after the last backup to used for searching. The comparison equality
is set via B<-e>.

A value of "-1" or the like may be given to display all machines and backup ages.

=head2 B<-c> <critical level>

The number of machines needing to match B<-a> when B<-n> is used for it to be
considered critical.

=head2 B<-e> <equality>

Either the symbol or letter equivalent may be used.

This is evaluated as below.

   $backup_age $equality $opts{a}

The understood ones are listed below.

    e, =
    lt, <
    le, <=
    gt, >
    ge, >=

=head2 B<-i> <ignore file>

bin/bpc-info  view on Meta::CPAN

    foo.bar
    # another comment 
        foo.bar

=head2 B<-l>

Displays the last information for the machine specificied via B<-m>.

=head2 B<-m> <machine>

The machine to operate on in terms of backups.

=head2 B<-n>

Operate in Nagios mode.

When combined with B<-a> can be used for alerting if to many backups
are older than a specific age.

=head2 B<-w> <warn level>

The number of machines needing to match B<-a> when B<-n> is used for it to
throw a warning.

=cut

lib/BackupPC/Backups/Info.pm  view on Meta::CPAN

package BackupPC::Backups::Info;

use 5.006;
use strict;
use warnings;
use base 'Error::Helper';

=head1 NAME

BackupPC::Backups::Info - Restrieves info on BackupPC backups.

=head1 VERSION

Version 0.1.1

=cut

our $VERSION = '0.1.1';


lib/BackupPC/Backups/Info.pm  view on Meta::CPAN

	
	if( ! $self->errorblank ){
        return undef;
    }

	return $self->{pcdir};
}

=head2 get_parsed

This parses the raw backups file and then returns a array of hashes.
For a explanation of the hashes, please see BACKUP HASH.

One archment is taken and that is the machine name.

   my @parsed=$bpcinfo->get_parsed($machine);
    if ( $bpcinfo->error ){
        warn('something happened: '.$self->errorstring);
    }

=cut

lib/BackupPC/Backups/Info.pm  view on Meta::CPAN

	}

	#break it at the lines
	my @lines=split(/\n/, $raw);

	#will store what we return
	my @parsed;

	my $int=0;
	while( defined( $lines[$int] ) ){
		my %backup;
		( $backup{num}, $backup{type}, $backup{startTime}, $backup{endTime},
		  $backup{nFiles}, $backup{size}, $backup{nFilesExist}, $backup{sizeExist},
		  $backup{nFilesNew}, $backup{sizeNew}, $backup{xferErrs}, $backup{xferBadFile},
		  $backup{xferBadShare}, $backup{tarErrs}, $backup{compress},
		  $backup{sizeExistComp}, $backup{sizeNewComp}, $backup{noFill},
		  $backup{fillFromNum}, $backup{mangle}, $backup{xferMethod}, $backup{level} )=split(/\t/, $lines[$int]);

		if ( $backup{compress} eq ''){
			$backup{compress}=0;
		}
		
		push( @parsed, \%backup );
		
		$int++;
	}

	#save info on the last
	my %last=%{$parsed[$#parsed]};
	$self->{last}{$machine}=\%last;

	#save the parsed
	$self->{parsed}{$machine}=\@parsed;
	
	return @parsed;
}

=head2 get_raw

This retrieves the law data from a backups file for a machine.

The section on backups file in
L<https://backuppc.github.io/backuppc/BackupPC.html#Storage-layout>
is suggested reading if you plan on actually using this.

    my $raw=$bpcinfo->get_raw('foo');
    if ($bpcinfo->error){
        warn('something errored');
    }

=cut

sub get_raw{

lib/BackupPC/Backups/Info.pm  view on Meta::CPAN

	my $pcdir=$self->get_pc_dir;
	my $machineDir=$pcdir.'/'.$machine;
	
	if (! -d $machineDir ){
		$self->{error}=6;
		$self->{eerorString}='"'.$machineDir.'" does not eixst';
		$self->warn;
		return undef;
	}

	my $backupsFile=$machineDir.'/backups';

	my $fh;
	if (! open( $fh, '<', $backupsFile ) ){
		$self->{error}=7;
		$self->{errorString}='failed to open "'.$backupsFile.'"';
		$self->warn;
	};

	my $data='';
	while ( my $line=$fh->getline ){
		$data=$data.$line;
	}
	
	return $data;
}

lib/BackupPC/Backups/Info.pm  view on Meta::CPAN

	
	if( ! $self->errorblank ){
        return undef;
    }

	return keys(%{$self->{parsed}});
}

=head2 read_in_all

This reads in the backups files for each machine.

Currently this just attempts to read in all via get_parsed
and ignores any errors, just proceeding to the next one.

As long as list_machines does not error, this will not error.

    $bpcinfo->read_in_all
    if ( $bpcinfo->error ){
        warn('something happened: '.$self->errorstring);
    }

lib/BackupPC/Backups/Info.pm  view on Meta::CPAN


	my @machines=$self->list_machines;
	if ( $self->error ){
		return undef;
	}

	my $pcdir=$self->get_pc_dir;
	
	my $int=0;
	while( defined( $machines[$int] ) ){
		if ( -f $pcdir.'/'.$machines[$int].'/backups' ){
			$self->get_parsed( $machines[$int] );
		}
				
		$int++;
	}

	return 1;
}

=head1 BACKUP HASH

Based on __TOPDIR__/pc/$host/backup from
L<https://backuppc.github.io/backuppc/BackupPC.html#Storage-layout>.

=head2 num

The backup number for the current hash.

=head2 type

Either 'incr' or 'full'.

=head2 startTime

The unix start time of the backup.

=head2 endTime

The unix end time of the backup.

=head2 nFiles

Number of files backed up.

=head2 size

Total file size backed up.

=head2 nFilesExist

lib/BackupPC/Backups/Info.pm  view on Meta::CPAN

=head2 nFilesNew

Number of new files not already in the pool.

=head2 sizeNew

Total size of files not in the pool.

=head2 xferErrs

Number of warnings/errors from the backup method.

=head2 xferBadFile

Number of errors from the backup method in regards to bad files.

=head2 xferBadShare

Number of errors from smbclient that were bad share errors.

=head2 tarErrs

Number of errors from BackupPC_tarExtract.

=head2 compress

The compression level used on this backup. Zero means no compression.

Please note that while BackupPC may leave this field blank if none is used, this module
will check for a blank value and set it to zero.

=head2 sizeExistComp

Total compressed size of files that already existed in the pool.

=head2 sizeNewComp

Total compressed size of new files in the pool.

=head2 noFill

et if this backup has not been filled in with the most recent previous filled or full backup.
See $Conf{IncrFill} in the BackupPC docs.

=head2 fillFromNum

If filled, this is the backup it was filled from.

=head2 mangle

Set if this backup has mangled file names and attributes. Always true for backups in v1.4.0
and above. False for all backups prior to v1.4.0.

=head2 xferMethod

The value of $Conf{XferMethod} when this dump was done.

=head2 level

=head1 ERROR FLAGS

=head2 1/backBackupPCdig

lib/BackupPC/Backups/Info.pm  view on Meta::CPAN

=head2 7/open

Open on a file failed. Please make sure the script is running as the same user as BackupPC.

=head1 AUTHOR

Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-backuppc-backups-info at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=BackupPC-Backups-Info>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.



( run in 0.522 second using v1.01-cache-2.11-cpan-49f99fa48dc )