ghenry
31st August 2004, 11:09 AM
Just finished this and thought someone could use it:
#!/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 $options = '-v5 --print-statistics';
my $localdir = '/your/localdir';
my $userhost = 'you@yourhost';
my $remotedir = '/your/remotedir';
my @args = ( $localdir, "$userhost\:\:$remotedir" );
my $to = 'Your Name <your.name@you.com>';
my $from = 'Your Support Service <support@yourservice.com>';
my $sep = '-' x 76 . "\n";
my $time = localtime;
my $datestamp = strftime '%d.%m.%y.%T', localtime;
# Suretec messages
print "\n", $sep, "Brought to you by Your Company Ltd.\n", $sep;
print "\n", $sep, "Initialising remote backup synchronsation on $time.\n", $sep;
# Using system command call to give us a return code, with die in the 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", $sep, "Remote backup complete on $time. E-mail sent with details.\n", $sep;
# Create a success logfile
open LOG, ">>$datestamp-rdiff-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", $sep, "Remote backup failed on $time. E-mail sent with details.\n", $sep;
# Create a failure logfile
open LOG, ">>$datestamp-rdiff-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";
die "Backup exited funny: $?" unless $backup == 0;
}
# Program complete
I will be adding to this regularly, so you can find the latest version at:
http://www.perl.me.uk/downloads
#!/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 $options = '-v5 --print-statistics';
my $localdir = '/your/localdir';
my $userhost = 'you@yourhost';
my $remotedir = '/your/remotedir';
my @args = ( $localdir, "$userhost\:\:$remotedir" );
my $to = 'Your Name <your.name@you.com>';
my $from = 'Your Support Service <support@yourservice.com>';
my $sep = '-' x 76 . "\n";
my $time = localtime;
my $datestamp = strftime '%d.%m.%y.%T', localtime;
# Suretec messages
print "\n", $sep, "Brought to you by Your Company Ltd.\n", $sep;
print "\n", $sep, "Initialising remote backup synchronsation on $time.\n", $sep;
# Using system command call to give us a return code, with die in the 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", $sep, "Remote backup complete on $time. E-mail sent with details.\n", $sep;
# Create a success logfile
open LOG, ">>$datestamp-rdiff-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", $sep, "Remote backup failed on $time. E-mail sent with details.\n", $sep;
# Create a failure logfile
open LOG, ">>$datestamp-rdiff-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";
die "Backup exited funny: $?" unless $backup == 0;
}
# Program complete
I will be adding to this regularly, so you can find the latest version at:
http://www.perl.me.uk/downloads