On Fri, Jan 7, 2011 at 16:20, Blake Dunlap <iki...@gmail.com> wrote:

> On Fri, Jan 7, 2011 at 13:15, Phil Stracchino <ala...@metrocast.net>wrote:
>
>> Blake,
>> By all means post it, and you might want to consider submitting it as a
>> contributed support script.
>>
>
>  Script to automatically start VirtualFulls based on Full Backup Age
> attached, currently set up to have some hardcoding in the script, but easily
> changeable. Designed to be run from crontab piped to mail program if any
> issues.
>
> Assumes a fairly simple backup structure, but easily customizable.
> Originally designed for our use, which consisted of age based backup
> schedule, each night set to incremental, with max diff age of a week, and
> max full age set about 2 weeks longer than the age set in the vmon script.
>
> Script looks at the last x days of incrementals (so as to not have to read
> configs etc), and the most recent full backup for each job, and orders them
> from oldest to newest. The top x jobs with fulls older than x days have
> vfull backups spawned via console.
>
> Reason for this design was so that we would have staggered backups, so as
> not to overload backup server, as well as to maintain under backup window.
>
> -Blake
>

Attached is a cleaner version.
#!/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 = '';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 <<EOF
run job=$jobtocheck level=VirtualFull SpoolData=$spool priority=$priority yes
EOF` unless $debug;
        }
        $exit = 1 if $batch;
}

$dbh->disconnect();

exit $exit;
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl 
_______________________________________________
Bacula-users mailing list
Bacula-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bacula-users

Reply via email to