#!/usr/bin/perl

use strict;
use warnings;
use Mail::Sendmail;
use POSIX qw(strftime);

#################################################
#  program:	suretec-backup                  #
#  license:	GPL 				#
#  author:	Gavin Henry            		#
#  company:	Suretec Systems Ltd.            #
#  url:		http://www.suretecsystems.com	#
#  version:	v1.0				#
#                                               #
#  first draft : 30-08-04                       #
#  last update : 31-08-04			#
#################################################

# Globals
my $rdiff = '/usr/bin/rdiff-backup';
my $localdir = '/home/ghenry/perl';
my $userhost = "slim_jim\@ssh.magicfx.co.uk";
my $remotedir = '/home/slim_jim/perl';
my $args = "$localdir $userhost\:\:$remotedir";
my $to = "ghenry\@perl.me.uk";
my $from = "ghenry\@perl.me.uk";
my $time = localtime;  
my $datestamp =  strftime "%d.%m.%y.%T", localtime;


# Suretec Message
print "\n----------------------------------------------------------------------------\n";
print "Brought to you by Suretec Systems Ltd.\n";
print "----------------------------------------------------------------------------\n";

# Start message
print "\n----------------------------------------------------------------------------\n";
print "Initialising remote backup synchronsation on $time.\n";
print "----------------------------------------------------------------------------\n";

# Using system command call to give us a return code, with die after if{}else{} block.
my $backup = system($rdiff, $args);

# Send e-mail with a few details for success and failures
# Success
if ($backup == 0) {
my %mails = ( To => "$to",
From => "$from",
Subject => "Remote backup complete from $ENV{HOSTNAME} on $time",
Message => "The remote backup has been completed on $ENV{HOSTNAME} on $time with the command:\n\n $rdiff $args\n" );
sendmail(%mails);
# Success finish message
print "\n----------------------------------------------------------------------------\n";
print "Remote backup complete on $time. E-mail sent with details.\n";
print "----------------------------------------------------------------------------\n";

# Create a success logfile
open LOG, ">>$datestamp-suretec-backup-success.log"
  or die "Cannot create logfile: $!";
print LOG "Remote backup completed on $time, with the command:\n\n$rdiff $args\n\nAn e-mail has been sent.\n";
close LOG;
print "Logfile created on $time.\n\n";

# Failure
} else {
my %mailf = ( To => "$to",
From => "$from",
Subject => "Remote backup failed from $ENV{HOSTNAME} on $time",
Message => "The remote backup has failed on $ENV{HOSTNAME} on $time with the command:\n\n$rdiff $args\n" );
sendmail(%mailf);
# Failure finish message
print "\n----------------------------------------------------------------------------\n";
print "Remote backup failed on $time. E-mail sent with details.\n";
print "----------------------------------------------------------------------------\n";

# Create a failure logfile
open LOG, ">>$datestamp-suretec-backup-failed.log"
  or die "Cannot create logfile: $!";
print LOG "Remote backup failed on $time, with the command:\n\n$rdiff $args\n\nAn e-mail has been sent.\n";
close LOG;
print "Logfile created on $time.\n\n";
}

# Did it work?
die "Backup exited funny: $?" unless $backup == 0;

# Program complete.






