PDA

View Full Version : Perl rdiff-backup script



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

ghaefb
31st August 2004, 11:15 AM
ghenry you are really into perl now are you :)

Where did you learn perl skills?
And how come you like perl so much?

I'm thinking of learning perl too...

ghenry
31st August 2004, 11:43 AM
I just bought Learning Perl and bought the Perl CD Bookshelf from ebay.

Joined the beginners perl mailing list and went from there.

I love it because you can do so much with it and as it is quite hard it really makes you think about your filesystem etc. and things can easily be applied to php and others.

It is the swiss army knife, as there are so many modules.

My next project is going to be using http://axkit.org/, like www.gentoo.org as I love the structure of xml and like docbook xml too.

Also, at my day job I have some graduate engineer things to write up, so I am writing a script to pull things from google and write them to openoffice writer, so I don't have to keep copying and pasting :-)

Gavin.

ghenry
31st August 2004, 11:45 AM
Would you like a free copy of Perl CD bookshelf as a favour? You can have my official backup copy :-)

No copyright infringement then :-)

ghaefb
31st August 2004, 11:54 AM
Would you like a free copy of Perl CD bookshelf as a favour? You can have my official backup copy :-)

No copyright infringement then :-)

You would do that?
I'd really like to learn more about perl...

This is a CD right? How would I get it?

ghenry
31st August 2004, 12:06 PM
I have made a bzip2 tarball of the CD, it's basically, 7 O'reilly Perl books in HTML with a java search feature:

http://www.oreilly.com/catalog/perlcdbs4/
It's version 3 I have. I didn't even know there was version 4, damn!!!!

http://www.oreilly.com/catalog/perlcdbs3/ I what I have, which I actually think is better now I have had a look at it as it has perl & XML and LWP & perl = 7 books, whereas V4.0 has 6. I was going to buy mastering regluar expressions anyway, so I think I haven't lost out.


PM me or I'll give a link in the staff forum.

fedorabobblue
20th February 2006, 03:34 PM
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

================================================== ======
hey man just wondering about a few things in your backup script

i dont suppose you could explaine to me what a few things in your backup script is doing ?


like $rdiff = /usr/bin/rdiff-backup

whats this actually doing because isint $localdir = /your/localdir where u set the source for the backup ?

and remote dir where it backups to ? .

the other commands i do not understand are.

$sep and $options and $userhost (you@yourhost) is thi my hostname ?

if you could explaine this to me i would extremly greatful

ghenry
21st February 2006, 03:25 PM
================================================== ======
hey man just wondering about a few things in your backup script

i dont suppose you could explaine to me what a few things in your backup script is doing ?


like $rdiff = /usr/bin/rdiff-backup

whats this actually doing because isint $localdir = /your/localdir where u set the source for the backup ?

and remote dir where it backups to ? .

the other commands i do not understand are.

$sep and $options and $userhost (you@yourhost) is thi my hostname ?

if you could explaine this to me i would extremly greatful

$rdiff sets where the rdiff binary is located, in case you installed it somewhere else.

$sep is what goes before the print statement for print on the console.
lots of ------------------------

$options are the option for the rdiff command

$userhost is how you would log into where the backups are being saved to via SSH.

HTH.

fedorabobblue
21st February 2006, 03:36 PM
ok thanks alot man i thought u had wrote the entier backyup script urself.

I got a bit stuck and started writing my own

Do u think this is gona be easy for a brand new begginer of perl

i just wana make a simple backup script