#!/usr/bin/perl -w
use strict;
use Pod::Usage;
use Sysadm::Install qw(:all);
use Log::Log4perl qw(:easy);
use Log::Log4perl::Appender::Screen;

Log::Log4perl->easy_init($DEBUG);

my $MDIR      = "/mnt/backup/data";
my %ptypes  = map { $_ => 1 }
         qw(83 7);
my @machnames = qw(desktop1 
                   desktop2 laptop1);
my %machnames = map { $_ => 1 } @machnames;

my $mname = pick "Box", \@machnames, 1;

my %drive_done;

my $bdir    = "$MDIR/$mname";
my $oldbdir = "$MDIR/$mname.old";

  # Move old backup aside
if(-d $oldbdir) {
  LOGDIE "$oldbdir already exists";
}
mv $bdir, $oldbdir if -d $bdir;
mkd $bdir unless -d $bdir;

    # Save the master boot record 
    # of the first IDE disk
tap qw(dd if=/dev/hda),
    "of=$bdir/hda.mbr",
    qw(count=1 bs=512);

my $sf = `sfdisk -d`;

while($sf =~ /^(.*Id=\s*(\w+).*)/mg) {
  my($line, $id) = ($1, $2);

  next unless exists $ptypes{$id};

  my($path) = split ' ', $line, 2;
  (my $dev   = $path) =~ s#.*/##;
  (my $drive = $dev)  =~ s/\d//g;

    # Save partition drive's table
  if(!$drive_done{$drive}++) {
    sysrun "sfdisk -d /dev/$drive " .
           ">$MDIR/$mname/$drive.pt";
  }

    # Save partition
  sysrun "partimage -b -d -z1 -o save" .
         " /dev/$dev $bdir/$dev.img.gz";
}

  # Remove old backup
rmf $oldbdir if -d $oldbdir;

=head1 NAME

pbb - Partition Based Backup

=head1 SYNOPSIS

    pbb

=head1 DESCRIPTION

Scans all IDE hard drives, and backs 
them up by partiion.

