This is my first real Perl program outside of the learning to Perl
exercises. Any thoughts, opinions, or suggestions are greatly
appreciated.

I used system copy rather file::copy for simplicity. It seems like doing
the same kind of recursive copy with file::copy would have made this a
very large program not to mention some complicated loops. I would have
had to set time stamps as well as permissions for each file copied as
well. I think I have reduced the processing time by letting cp do the
copy work. Also with it being samba specific it seemed unnecessary to
even attempt to make this portable to winXX.

Backup Script (well more like a mirror script)
Copies samba shared directories to a local directory.
-----------
#!/usr/bin/perl


#Constants

use constant DEST    => "/backup/";
use constant SOURCE  => "/mnt/backup/";
use constant INIFILE => "/backup/scripts/pdkbackup/backup.ini";

use Config::IniFiles;
my $ini;

open LOG, ">>backup.log";
tie %ini, 'Config::IniFiles', (-file => INIFILE);

&logheader;
foreach $section (keys %ini){
    print "Backing up $ini{$section}{machine}\n";
    unless (&mount){
        &etrap("mnt");
        next;
    }
    mkdir "//backup/$ini{$section}{machine}" unless (-e
"/backup/$ini{$section}{machine}");
    $ini{$section}{path}  =~s/ /\\ /g;  #Convert path with spaces to
unix standard

    $ini{$section}{email} =~s/ /\\ /g;  #Convert path with spaces to
unix standard

    $command = "cp --preserve --recursive --update
".SOURCE."$ini{$section}{path} ".DEST."$ini{$section}{machine} 2>&1";
    &copydir($command);
    if (exists $ini{$section}{email}){
        $command="cp --preserve --recursive --update
".SOURCE."$ini{$section}{email} ".DEST."$ini{$section}{machine} 2>&1";
        &copydir($command);
    }
    system "sync";
    system "umount /mnt/backup";
}
print LOG "\n\n";

sub mount{
    !system
"mount","-t","smbfs","-o","username=$ini{$section}{username},password=$i
ni{$section}{password}",
    "//$ini{$section}{machine}/$ini{$section}{share}","/mnt/backup";
}
sub copydir{
    my $copydir = `$_[0]`;
    if ($copydir =~ m![^/]*'!){

       $copydir = $1 if ($copydir =~ m!([^/]*)'!);
       &etrap("cp",$copydir);
    }
}
sub etrap{
    chomp(my $date=`date +%r`);
    my $copyerror  = "Can not be locked!";
    my $mounterror = "Computer offline!";
    SWITCH: {
       printf LOG "%-12s %10s : %20s %30s\n",  $ini{$section}{machine},
$date, $_[1], $copyerror   if ($_[0] eq "cp");
       printf LOG "%-12s %10s : %20s %30s\n",  $ini{$section}{machine},
$date, "na" , $mounterror  if ($_[0] eq "mnt");
    }
}
sub logheader{
    print LOG "Backup log for ". `date +%D`;
    printf LOG "%-12s  %10s   %20s %30s\n", "Machine", "Time", "File",
"Error";
    print LOG "-"x (78) ."\n";
}


Paul Kraus
Network Administrator
PEL Supply Company
216.267.5775 Voice
216-267-6176 Fax
www.pelsupply.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to