#!/usr/bin/perl -w
#
# Author: Hemant Shah
#         October 3, 2008
#
# This script will remove all the backup files from the disk and their records
# from the database (Full and Incremental) that belong to disk backups more
# than 2 month old. The records for the tape backups are not deleted, they will
# be automatically purge by PruneBaculaVolumes scripts after 2 years.
#
#
# Example:
#  Month 1: Full, Incremental
#  Month 2: Full, Incremental
#  Month 3: Full, Incremental
#  Before Month 4 Full backups start this script will remove all files and 
#  records for disk backups for Month 1.
#
#  This script must be executed before the first Full backup of the month,
#  either as a Admin job or RunBefore script.
# 
#  The script assumes that the volume name (label) for the tape backups is
#  FullBackup-YYYY-MM-DD. In the database query it excludes the volume names
#  that start file "FullBackup-".
#

use Date::Calc qw(Nth_Weekday_of_Month_Year Parse_Date);
use strict;
use warnings;
use Time::Local;

my($ExeName) = "";
($ExeName = $0) =~ s:.*/::;

my $Logfile     = "/backups/log/maintenance.log";
my $DateTime    = localtime(time);
my @VolumeNameArray = ();
my @WarningArr = ();
my $VolumeName = "";
my $Volume = "";
my $Line = "";
my $VolumeCount = 0;
my ($Year,$Month) = Parse_Date(`/bin/date --date="-2 month"`);
my ($LastYear,$LastMon,$LastDay) = Nth_Weekday_of_Month_Year($Year,$Month,5, 1);
my $PrugeDate=sprintf("%4d-%02d-%02d", $LastYear,$LastMon,$LastDay);

open( LOGFILE, ">> $Logfile" ) or die "Error: $! on opening $Logfile.\n";
print LOGFILE "\n$ExeName begin: $DateTime\n\n";
print LOGFILE "Purging all records for backup to disk before $PrugeDate\n";

open(PSQLPIPE, "psql -t -A  -d bacula -c \"select volumename from media where volumename not like 'FullBackup-%' and firstwritten < \'$PrugeDate\' order by firstwritten\" |") or die $!;
while(<PSQLPIPE>)
{
  chomp;
  ($VolumeName) = split(/\|/, $_);
  push(@VolumeNameArray, $VolumeName);
}
close(PSQLPIPE);

$VolumeCount = $#VolumeNameArray + 1;

####################################################################
# Delete files for the purged volume files from /backups/backupfiles
# directory.
####################################################################
foreach $Volume (@VolumeNameArray)
{
   my ($File)=</backups/backupfiles/*/$Volume>;
   if (!defined $File)
   {
      print LOGFILE "WARNING: Cannot find file for $Volume. File not deleted.\n";
      push(@WarningArr, "WARNING: Cannot find file for $Volume. File not deleted.");
   }
   else
   {
      print LOGFILE "Deleting $File file.\n";
      if (!unlink($File))
      {
        push(@WarningArr, "WARNING: Cannot delete $File.\n\t$@");
      }
   }
}

####################################################################
#Remove volume records from Bacula catalog.
####################################################################

open(SYSTEM, "|/usr/local/bin/bconsole -c /usr/local/etc/bconsole.conf > /dev/null") or die $!;

foreach $Volume (@VolumeNameArray)
{
   print SYSTEM "delete volume=$Volume\nyes\n";
}

print SYSTEM "quit\n";

close(SYSTEM);


####################################################################
# Clean up.
####################################################################
if ($VolumeCount > 0)
{
   print LOGFILE "\nFollowing volumes were purged from database:\n";

   open(PIPE,"|/usr/sbin/sendmail root");
   printf PIPE "From: (Bacula) <root>\n";
   printf PIPE "Subject: $ExeName - $VolumeCount volumes purged from database\n";
   printf PIPE "Cc:\n";
   printf PIPE "\n";
   printf PIPE "Following volumes purged from database:\n";

   foreach $Volume (@VolumeNameArray)
   {
      print LOGFILE "\t$Volume\n";
      print PIPE "\t$Volume\n";
   }

   if ($#WarningArr != -1)
   {
      print PIPE "\n$#WarningArr WARNINGS:\n";
      foreach $Line (@WarningArr)
      {
         print PIPE "   $Line\n";
      }
   }

   close(PIPE);
}

$DateTime = localtime(time);

print LOGFILE "\n$ExeName end: $DateTime\n\n";

close(LOGFILE);

