Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- Author: beggars #perl
- Version: 0.01
- use strict;
- use warnings;
- use File::Find::Rule;
- use File::Basename qw/basename dirname/;
- use Data::Dumper;
- my @data_dir = qw { data /data1 /data2 /data3 }
- ; # list here the data dir if you want to loop on it.
- foreach my $dir (@data_dir) {
- #my $dir = 'data'; # else you need to set this one.
- # Gather server name with .mw or um on fqdn
- my %server_w_log =
- map { my $tempfile = basename $_; $tempfile => $_ }
- File::Find::Rule->directory->name(qr/.*\.(mw|um).*/)->in($dir);
- # Gather server with sql dirctory
- my %server_w_dir =
- map { my $tempfile = basename $_; $tempfile => $_ }
- File::Find::Rule->directory->name(qr/Microsoft\sSQL\sServer/i)->in($dir);
- my @backups
- ; # this will contain all the recent DB backups modified less or equal to one day
- # Gather servers with maintenance log
- for ( keys %server_w_log ) {
- push(
- @backups,
- [
- File::Find::Rule->file->name(qr/.*\.(bak|BAK)$/)
- ->modified(" =< 1")->in("$server_w_log{$_}/daily.0")
- ]
- );
- my $log = "$server_w_log{$_}/daily.0/C/maintenanceplan.log";
- # If maintenanceplan.log exist, compare the db backup agains the it, output the list on stdout
- if ( -f $log ) {
- $server_w_log{$_} = $log;
- compare_db_w_log( $log, $_, \@backups );
- }
- else { print "$log none existing\n"; }
- }
- # List server which has sql dir
- for ( keys %server_w_dir ) {
- my ($server) = ( split( '/', $server_w_dir{$_} ) )[2];
- print "\n\t$server had $server_w_dir{$_} directory \n";
- }
- # To compare the db backup agains the one on maintenanceplan.log
- sub compare_db_w_log {
- my ( $logfile, $server, $backuplist ) = @_;
- my %hash;
- my %compare;
- for (@$backuplist) {
- for (@$_) {
- my $key = basename $_;
- ($key) = ( split( '_', $key ) )[0];
- $compare{$key} = 1;
- }
- }
- open( LOG, '<', $logfile ) or warn "can't open $logfile $!\n";
- while (<LOG>) {
- my ($db) = ( split( ':', $_ ) )[0];
- push( @{ $hash{$server} }, $db );
- }
- for ( keys %hash ) {
- print "SERVER: $_ hash these DB's backup:\n";
- for ( @{ $hash{$_} } ) {
- if ( exists $compare{$_} ) { print $_, "\n"; }
- else {
- print
- "$_ don't have recent backup but exist on maintenance plan\n";
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment