August 3, 2007

Using rsync for incremental backups

If you have a large amount to data to backup, it doesn’t make sense to copy the data which does not change. Especially if you are doing network backups, the job might not even complete within one day and you suck up all the bandwidth. Rsync is excellent for daily backups. Using a perl script and a cron job, you can automate backups from all your servers to a remote location.

#!/usr/bin/perl

require “/home/kching/lib/email.pl”;

=item rysnc job

this script reads in a file of rsync source and destinations and perform the
jobs in sequential order

=cut

$PATH=’/home/kching’;
$SENDMAIL = ‘/usr/sbin/sendmail’;

$rsyncuser=’keching’;
$filein = “$PATH/rsync.joblist”;
$logfile = “$PATH/rsync.log”;

$checkdisk =”$PATH/checkDiskUsage.pl”;

@email_list=(
‘keching@ucsd.edu’
);

$FROM=’rsync’;

$rsyncoptions = $ARGV[0];

if($rsyncoptions eq ‘delete’){
$rsyncoptions = ” –delete “;
}else{
$rsyncoptions = “”;
}

$timeout = ‘ –timeout=1800 ‘;

@excludelist=qw(
mnt
tmp
sys
proc
export1
boot
dev
lost+found
);

foreach my $dir(@excludelist){
$exclude.= ” –exclude \”/$dir/\” “;
}

$rsyncoptions.=$exclude.$timeout;

$RSYNC=’rsync -avh –itemize-changes ‘.$rsyncoptions.’ –password-file=’.$PATH.’/rsync.password ‘.$rsyncuser.’@';

open(IN,$filein) || die “cannot open $filein\n”;

eval{

$log = ”;
while(<IN>){
if($_=~/^#/){
next;
}
$disk = `perl $checkdisk`;
if($disk){
print “$disk\n”;
email(\@email_list, $FROM, $disk, “Disks nearing capacity, rsync aborted.”);
}

chomp($_);
($source, $destination) = split(/\t/,$_);
$current_date=`/bin/date`;
#$log.= “<BR>Timestamp:”.$current_date.”<BR>”;
$cmd = $RSYNC.$source.” $destination 2> $logfile”;
$log.=”$_<BR>$cmd<BR>”;
print “$log\n”;
#email(\@email_list, $FROM, $log, “Backup job $source started”);
$log .= “<BR>Start Timestamp:”.$current_date.”<BR>”;
$log .= `$cmd`;
$current_date=`/bin/date`;
$log = “<BR>End Timestamp:”.$current_date.”<BR>”.$log;
$errors = `cat $logfile`;
if(!$errors){
$errors=’none’;
}else{
email(\@email_list, $FROM, $errors.$log, “Errors job $source”);
}
$log = “Error log:\n$errors\n”.$log;
#$log =~s/\n/<BR>/g;
}
close(IN);
email(\@email_list, $FROM, $log, “Backup jobs ended”);

};
$disk = `perl $checkdisk`;

if($@){
$ERROR=”rsync server Exception:”;
$ERROR .= $@;
print $ERROR;
email(\@email_list,$FROM,$ERROR, ‘Backup job error’);
}

rsyncjobpl.txt

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.