#!/usr/bin/perl use strict; use Getopt::Long; use DBI; my $allowed = 3; my $fullinterval = 90; my $scaninterval = "3 DAY"; my $spool = "no"; my $priority = 7; my $debug = 0; my $exit = 0; my %jobs; my $batch = 0; my @validjobs; my @jobstorun; my @jobstocheck; my $Row; $fullinterval--; GetOptions("debug+"=>\$debug, "allowed:i"=>\$allowed, "priority:i"=>\$priority, "spool:s"=>\$spool, "fullinterval:s"=>\$fullinterval, "batch+"=>\$batch ); print "Debug: $debug\n" if $debug; print "Allowed: $allowed\n" if $debug; print "Interval: $fullinterval\n" if $debug; print "Batch: $batch\n" if $debug; my $username = 'bacula';my $password = 'backupm@n';my $database = 'bacula';my $hostname = 'localhost'; my $port = 3306; my $dbh = DBI->connect("dbi:mysql:database=$database;" . "host=$hostname;port=$port", $username, $password); die "Unable to connect to Database" unless $dbh; #Get list of recent successful Incremental jobs my $SQL="select Distinct(Name) as Name from Job WHERE Level IN ('I', 'D') AND JobStatus = 'T' AND StartTime > DATE_SUB(CURDATE(), INTERVAL $scaninterval)"; my $Select = $dbh->prepare($SQL); die "Unable to run query" unless $Select; $Select->execute(); my $rv = $Select->rows; if ($rv > 0) { print "The following incrementals were found:\n" if $debug>1; while($Row=$Select->fetchrow_hashref) { $jobs{ $Row->{'Name'} }{'Active'} = 1; print " $Row->{'Name'}\n" if $debug>1; } $Select->finish; } else { $Select->finish; die "Somethings wrong, no successful Incremental jobs returned!\n"; } #Get list of last successful full backup for each Job $SQL="select Max(JobId) as JobId, Name, DATEDIFF(CURDATE(), Max(StartTime)) as Age from Job WHERE Level = 'F' AND JobStatus = 'T' GROUP BY Name"; $Select = $dbh->prepare($SQL); die "Unable to run query" unless $Select; $Select->execute(); $rv = $Select->rows; if ($rv > 0) { #Attach the last successful full backup to each job while($Row=$Select->fetchrow_hashref) { if ($jobs{ $Row->{'Name'} }{'Active'} eq 1) { $jobs{ $Row->{'Name'} }{'Age'} = $Row->{'Age'}; $jobs{ $Row->{'Name'} }{'JobId'} = $Row->{'JobId'}; push(@validjobs,$Row->{'Name'}); } } $Select->finish; } else { $Select->finish; die "Somethings wrong, no successful Full jobs returned!\n"; } my @joblist = (sort { $jobs{$b}{'Age'} <=> $jobs{$a}{'Age'} } @validjobs); if (scalar(@joblist) > 0) { print "The following Fulls were found that match:\n" if $debug>1; print " Age ID Name\n" if $debug>1; foreach my $jobtocheck (@joblist) { print " $jobs{$jobtocheck}{'Age'} $jobs{$jobtocheck}{'JobId'} $jobtocheck\n" if $debug>1; if ($jobs{$jobtocheck}{'Age'} > $fullinterval && scalar(@jobstorun) < $allowed) { push(@jobstorun,$jobtocheck); } } } if (scalar(@jobstorun) > 0) { print "Running the following virtual full jobs today:\n"; foreach my $jobtocheck (@jobstorun) { print " $jobs{$jobtocheck}{'Age'} $jobtocheck\n"; my $test = `/sbin/bconsole -c /etc/bacula/bconsole.conf <disconnect(); exit $exit;