#!/usr/local/bin/perl -w
###########################################
# run-me -- Clonezilla backup script
# Mike Schilli, 2012 (m@perlmeister.com)
###########################################
use strict;
use File::Temp qw( tempfile );
use File::Basename;
use Log::Log4perl qw(:easy);
use Sysadm::Install qw( :all );

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

my $ip      = "192.168.0.111";
my $user    = "mschilli";
my $mnt_dir = "/home/partimag";

if( ! -d $mnt_dir ) {
  mkdir $mnt_dir;
}

network();
mount();
backup_all();
unmount();

1;

###########################################
sub network {
###########################################
  sysrun "sudo", "dhclient", "eth0";
}

###########################################
sub unmount {
###########################################
  sysrun "fusermount", "-u", $mnt_dir;
}

###########################################
sub mount {
###########################################
  DEBUG "Writing private key";
  my $privkey_file = (tempfile())[1];
  blurt privkey(), $privkey_file;

  DEBUG "Writing known hosts";
  my $kh_file = (tempfile())[1];
  blurt known_hosts(), $kh_file;

  my $rc = sysrun( "sudo", "sshfs",
    "$user\@$ip:/backup/clonezilla",
    $mnt_dir, "-o", 
    "ssh_command=ssh -i $privkey_file " .
    "-o BatchMode=yes " .
    "-o GlobalKnownHostsFile=$kh_file" );

  if( $rc ) {
    die "sshfs failed (backup host down?)";
  }
}

###########################################
sub backup_all {
###########################################
  my $last_backup_path =
    ( reverse sort <$mnt_dir/[0-9]*> )[0];
    
  my $continue_with;
    
  my @drives = < /dev/sda[0-9]* >;

  if( defined $last_backup_path and
      ! -f "$last_backup_path/DONE" ) {
    $continue_with = (
      sort { -M $a <=> -M $b } 
      < $last_backup_path/*gz* > )[0];
    $continue_with = 
      basename $continue_with;
    $continue_with =~ s/\..*//;
    DEBUG "Continue: $continue_with";
  }

  my $not_yet;

  if( defined $continue_with ) {
    $not_yet = 1;
  }
    
  my ( $sec, $min, $hour, $mday, $mon,
       $year ) = localtime( time );

  my $date = 
   sprintf "%04d-%02d-%02d-%02d-%02d-%02d", 
    $year + 1900, $mon+1, $mday, $hour, 
    $min, $sec;

  my @parts = ();

  for my $part ( sort @drives ) {

    my $base = basename $part;

    if( $base eq $continue_with ) {
      $not_yet = 0;
    }

    next if $not_yet;
    
    push @parts, basename $part;
  }

  backup( $date, @parts );
}

###########################################
sub backup {
###########################################
    my( $date, @parts ) = @_;

    DEBUG "Backing up @parts";
    sysrun qw( sudo /opt/drbl/sbin/ocs-sr 
      -b -q2 -c -j2 -z1 -i 2000 -sc -p true 
      savedisk ), "$date-img", @parts;

    blurt "", "$mnt_dir/$date-img/DONE";
}

###########################################
sub known_hosts {
###########################################
    return <<'EOT'
XXX
EOT
}

###########################################
sub privkey {
###########################################
    return <<'EOT'
-----BEGIN RSA PRIVATE KEY-----
YYY
-----END RSA PRIVATE KEY-----
EOT
}
