the Following is a back up script to take back up from remote servers using rsync
The script can do the following :
1) reads config file , check the config file if the remote file exists
2) does an rsync of remote dir to backupServer
3) does a tar of the back up in archive dir of the backup server
4) remove all files that are 60 days older ( currently disabled )
5) sends a mail in case any errors
-------------------------------------------------------
this is your /script/backup_scripl.pl
-------------------------------------------------------
#!/usr/bin/perl
use strict ;
use warnings;
use Data::Dumper ;
use Net::SMTP;
use File::Find;
use Archive::Tar;
######################################
our %Config;
require('/<PATHTOCONFIFFILE>/config_backup.pl');
my $F1 ='/var/www/projects/';
my $F2 ='/backup/apps/DocRoot/';
my $ARCH ='/backup/apps/DocRoot/archive/';
my $today = `date +%F`; chomp($today);
my $now =`date +%d%b-%H:%M`; chomp($now);
my $log ="/backup/docroot-backup1.log";
my $KEY ="/opt/.securekey/id_rsa";
my $RCPT ='systemadmin@yourdomain.com';
my $commonip = '10.0.1.' ;
my $hostname =`hostname -i`; chomp($hostname);
unless ( -e $F2 ) {
sendemail("ALERT - DocRoot Backup - Storage Path ABSENT ","Storage location $F2 was not found in the system at `date`. \nTerminating DocRoot backup process at stage1. \nServer is $hostname");
logit(" Backup Alert - $F2 STOR ABSENT");
exit ;
}
unless ( -e $F2 ) {
sendemail("ALERT - DocRoot backup Alert - Write Error on storage","Apparently I CANNOT write to $F2/testtouch on `date`. \nTerminating backup process at stage1.\nServer is $hostname ");
logit(" Backup Alert - storage is probably readonly. Cannot write to $F2 ");
exit;
}
logit("---------------------------- START TIME $now ---------------------------------------- ");
foreach my $web ( keys %Config ) {
logit(" Start server $web");
foreach my $server( keys %{$Config{$web}} ) {
print "/usr/bin/rsync -e \"ssh -i $KEY \" --delete -au root\@$commonip$server:$Config{$web}{$server}[1]$Config{$web}{$server}[0] $F2$Config{$web}{$server}[0].$server.proj\n" ;
system("/usr/bin/rsync -e \"ssh -i $KEY \" --delete -au root\@$commonip$server:$Config{$web}{$server}[1]$Config{$web}{$server}[0] $F2$Config{$web}{$server}[0].$server.proj") ;
logit("$F2$Config{$web}{$server}[0].$server.proj Rsync completed ... ");
}
logit(" End server ") ;
}
foreach my $web ( keys %Config ) {
foreach my $server ( keys %{$Config{$web}} ) {
if ( -e "$F2$Config{$web}{$server}[0].$server.proj" ) {
if ( chdir("$F2$Config{$web}{$server}[0].$server.proj") ) {
print " tar --use=lbzip2 -c -f $ARCH$Config{$web}{$server}[0].$server.proj-$today.tbz2 . 2>/dev/null \n";
system("tar --use=lbzip2 -c -f $ARCH$Config{$web}{$server}[0].$server.proj-$today.tbz2 . 2>/dev/null");
if ( $? == 0 ) {
logit("Archive of $F2$Config{$web}{$server}[0].$server.proj to $ARCH$Config{$web}{$server}[0].$server.proj-$today.tbz2 success");
}else{
logit("Archive of $F2$Config{$web}{$server}.$server.proj failed") ;
sendemail("ALERT - DocRoot backup Alert - Archive Fail", "$now :[STAGE - 2, Server = $hostname ] -- Archive of $F2$Config{$web}{$server}[0]\.$server\.proj failed.\nPlease check manually.\nPossible failure due to bad ecit status. Continuing with remaining process." );
}
} else {
logit("NO Dir $F2$Config{$web}{$server}[0].$server.proj ") ;
}
}else {
sendemail("ALERT - DocRoot backup Alert - Archiving Failed","Archiving of $F2$Config{$web}{$server}[0]\.$server\.proj failed $now \nTerminating backup process at stage2-Archiving.\nServer is $hostname ");
logit("-- Compression Failed....Not Found $F2/$Config{$web}{$server}[0]\.$server\.proj ");
}
}
}
logit("---------------------------- END TIME $now ---------------------------------------- ");
# purge backups older than AGE in days
my @file_list;
my @find_dirs = ($ARCH); # directories to search
my $now_time = time(); # get current time
my $days = 60; # how many days old
my $seconds_per_day = 60*60*24; # seconds in a day
my $AGE = $days*$seconds_per_day; # age in seconds
find ( sub { my $file = $File::Find::name;
if ( -f $file ) {
push (@file_list, $file);
}
}, @find_dirs);
for my $file (@file_list) {
my @stats = stat($file);
if ($now_time-$stats[9] > $AGE) {
#unlink $file;
print "unlink $file\n";
}
}
sub sendemail {
#usage sendemail("subject","mssg");
my $from = 'backupserver@yourdomain.com' ;
my $sub = $_[0] ;
my $content = $_[1] ;
my $to = $RCPT ;
my $relayhost = '10.0.0.22' || 'localhost';
my $smtp = Net::SMTP->new("$relayhost",
Debug => 1,
);
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend("Subject: $sub");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("$content");
$smtp->dataend();
$smtp->quit();
}
sub logit {
my $s = shift;
my $logtimestamp =`date +%d%b-%H:%M:%S` ;
chomp($logtimestamp);
print "$s\n";
my $fh;
open($fh, '>>', "$log") or die "$log: $!";
print $fh "$logtimestamp : $s\n";
close($fh);
}
------------------------------------------------------------------------------------------------------------------
this is the config file -- /script/config_backup.pl
------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl
%Config = (
'site1.domain.com' => {'23 => ['site1-domain','/var/www/html/'] },
'ontravel1.domain.com' => {'25' => ['ontravel1','/var/www/html/'] },
) ;
**** use on your own risk
No comments:
Post a Comment