#!/usr/bin/perl 

$RSYNC = "/usr/bin/rsync";
$SOURCE = "/home/andy";
$DESTINATION = "sf:~/rsync/bleys";
$FLAGS = "-acrvze ssh --delete";                     #RSYNC flags
$SENDMAIL="/usr/sbin/sendmail";
$EMAIL = "andy\@ccpu.com";
$BOX= `/bin/uname -n`;
$DATESTART =`date`;
$success =0;

@temp_log = `$RSYNC $FLAGS $SOURCE $DESTINATION 2>&1`;

if ($?>>8) { $success=1; }

push @log,"-----------------------------------------\n";
push @log,"RSYNC log on $BOX";
push @log,$DATESTART;
push @log,"-----------------------------------------\n";

foreach (@temp_log) {
	push @log,$_;
}

$DATEEND = `date`;
push @log,"-----------------------------------------\n";
push @log,"End of RSYNC log on $DATEEND";
push @log,"-----------------------------------------\n";
print @log;

if ($success == 1) {
        open(MAIL,"|$SENDMAIL $EMAIL");
        print MAIL "Subject: Failure rsync on $BOX\n\n";
        print MAIL "@log";
        close(MAIL);
}
else {
	open(MAIL,"|$SENDMAIL $EMAIL");
        print MAIL "Subject: Success rsync on $BOX\n\n";
        print MAIL "@log";
        close(MAIL);
}
