Rsync Backup - Code


#!/usr/bin/perl -w
# ------------------------------------------------------------
# script:  ldaprsync.pl
# Author:  Brad Marshall (bmarshal@pisoftware.com)
# Date:    20000801
#
# Purpose: Rsync certain modules from hosts in ldap
#
# Copyright (c) 2000 Plugged In Software Pty Ltd.  All rights reserved.

use strict;
use Net::LDAP;

my %config = ( "destdir" => "/opt/hosts",
               "rsyncoptions" => "--compress --archive --one-file-system");

my($ldap) = Net::LDAP->new('ldap.staff.plugged.com.au') or die "Can't bind to ldap: $!\n";

$ldap->bind;

my($mesg) = $ldap->search( base => "dc=pisoftware,dc=com",
                           filter => '(objectclass=machine)');

$mesg->code && die $mesg->error;

my($entry,$attr);

my(%results);
foreach $entry ($mesg->entries) {
        #print "DN = ",$entry->dn,"\n";
        #my($tmpcn) = $entry->get('uid');
        my(@rsync) = $entry->get('rsync');
        my(@hostname) = $entry->get('hostname');
        my($host) = join(" ", @hostname);
        foreach my $src (@rsync) {
                print "Getting $host $src\n";

                my $direct;
                if ($src =~ /^(.*)\//g) {
                        $direct = $1;
                } else {
                        $direct = $src;
                }
                my $dir = "$config{destdir}/$host/$direct";
                if ( ! -d $dir ) {
                        system("mkdir -p $dir");
                }
                my @ary = split /\s+/, "rsync $config{rsyncoptions} $host\::$src $dir";
                system(@ary) == 0 or warn "system @ary failed: $?\n";
                #print "@ary\n" or warn "system @ary failed: $?\n";
        }
#       foreach $attr ($entry->attributes) {
#               print $attr,": ", join(" ", $entry->get($attr)), "\n";
#       }
#       print "\n";
}

$ldap->unbind;