On Tuesday 31 Aug 2004 00:43, Chris Devers wrote:
> On Tue, 31 Aug 2004, Gavin Henry wrote:
> > This is my first real go at a perl script. Thoughts?
>
> Don't send attachments?
>
> There's no telling what an attachment contains these days; if it's just
> a plain text Perl script then please paste it inline with your message.
>
> Thanks :-)

#!/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 = "[EMAIL PROTECTED]";
my $remotedir = '/home/slim_jim/perl';
my $args = "$localdir $userhost\:\:$remotedir";
my $to = "[EMAIL PROTECTED]";
my $from = "[EMAIL PROTECTED]";
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.

-- 
Just getting into the best language ever...
Fancy a [EMAIL PROTECTED] Just ask!!!

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to