Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #!/usr/bin/perl
  2. Author: beggars #perl
  3. Version: 0.01
  4.  
  5.  
  6. use strict;
  7. use warnings;
  8. use File::Find::Rule;
  9. use File::Basename qw/basename dirname/;
  10. use Data::Dumper;
  11.  
  12. my @data_dir = qw { data /data1 /data2 /data3 }
  13. ; # list here the data dir if you want to loop on it.
  14. foreach my $dir (@data_dir) {
  15.  
  16. #my $dir = 'data'; # else you need to set this one.
  17.  
  18. # Gather server name with .mw or um on fqdn
  19.  
  20. my %server_w_log =
  21. map { my $tempfile = basename $_; $tempfile => $_ }
  22. File::Find::Rule->directory->name(qr/.*\.(mw|um).*/)->in($dir);
  23.  
  24. # Gather server with sql dirctory
  25.  
  26. my %server_w_dir =
  27. map { my $tempfile = basename $_; $tempfile => $_ }
  28. File::Find::Rule->directory->name(qr/Microsoft\sSQL\sServer/i)->in($dir);
  29.  
  30. my @backups
  31. ; # this will contain all the recent DB backups modified less or equal to one day
  32.  
  33. # Gather servers with maintenance log
  34. for ( keys %server_w_log ) {
  35. push(
  36. @backups,
  37. [
  38. File::Find::Rule->file->name(qr/.*\.(bak|BAK)$/)
  39. ->modified(" =< 1")->in("$server_w_log{$_}/daily.0")
  40. ]
  41. );
  42. my $log = "$server_w_log{$_}/daily.0/C/maintenanceplan.log";
  43.  
  44. # If maintenanceplan.log exist, compare the db backup agains the it, output the list on stdout
  45.  
  46. if ( -f $log ) {
  47. $server_w_log{$_} = $log;
  48. compare_db_w_log( $log, $_, \@backups );
  49. }
  50. else { print "$log none existing\n"; }
  51. }
  52.  
  53. # List server which has sql dir
  54.  
  55. for ( keys %server_w_dir ) {
  56. my ($server) = ( split( '/', $server_w_dir{$_} ) )[2];
  57. print "\n\t$server had $server_w_dir{$_} directory \n";
  58. }
  59.  
  60. # To compare the db backup agains the one on maintenanceplan.log
  61.  
  62. sub compare_db_w_log {
  63. my ( $logfile, $server, $backuplist ) = @_;
  64. my %hash;
  65. my %compare;
  66. for (@$backuplist) {
  67. for (@$_) {
  68. my $key = basename $_;
  69. ($key) = ( split( '_', $key ) )[0];
  70. $compare{$key} = 1;
  71. }
  72. }
  73.  
  74. open( LOG, '<', $logfile ) or warn "can't open $logfile $!\n";
  75. while (<LOG>) {
  76. my ($db) = ( split( ':', $_ ) )[0];
  77. push( @{ $hash{$server} }, $db );
  78. }
  79. for ( keys %hash ) {
  80. print "SERVER: $_ hash these DB's backup:\n";
  81. for ( @{ $hash{$_} } ) {
  82. if ( exists $compare{$_} ) { print $_, "\n"; }
  83. else {
  84. print
  85. "$_ don't have recent backup but exist on maintenance plan\n";
  86. }
  87. }
  88. }
  89. }
  90. }
Add Comment
Please, Sign In to add comment