develooper Front page | perl.beginners | Postings from December 2002

FW: Help with my script please.

Thread Next
From:
Paul Kraus
Date:
December 4, 2002 11:04
Subject:
FW: Help with my script please.
Message ID:
01ae01c29bc7$db589cd0$64fea8c0@pkrausxp
I just finished the learning Perl book and thought I would try and
rewrite a bash script that I had written long ago. The purpose of the
app is to mount a samba folder on a winx machine. Copy its contents to a
local folder as a mirror. I just wanted some input on my app and some
things explained if you could.

First off I am using the system "cp" so that I can preserve dates and
permissions as well as dig into subdirectories. I was going to use
File::Copy but it just seem to cumbersome to accomplish the same text.
Am I incorrect is there a "better way to do this."

Secondly I am reading the parameters from a comma delimitated file but
this is going to be changed to a real config file that is setup like
[machine name] Password=; Username=; ...

So just ignore this top portion unless you have some insights or advice.

Thirdly for some reason 80% of the time when I call system "umount ...."
the umount process hangs forever. I can't even kill it with -9.
Sometimes it comes back sometimes it doesn't. Any thoughts?

Last but easiest: 
I know I can condense these into one assignment but I can't figure it
out for the life of me. $path=$5; $path=~s/ /\\ /g;

Thanks in advance,
Paul Kraus

Script
-=-=-=-
#!/usr/bin/perl
chomp ($dt=`date +%r`);
open CONFIG,"<backup.cfg" or die "Can't find configuration file!\n";
open LOG,">>backup.log"; &errors("New"); foreach (<CONFIG>){
    chomp;
    if (/^([^#].*),(.*),(.*),(.*),(.*),(.*)$/){
	    $machine=$1;
	    $username=$2;
	    $password=$3;
	    $share=$4;
	    $path=$5;
	    $path=~s/ /\\ /g;
	    $email=$6;
	    $backupdir = "/backup/$machine";
	    $machine="//$machine";
	    mkdir $backupdir unless (-e "/backup/$machine");
	    &mountdir;
	    next if ($error==1);
	    &backupdir($backupdir);
	    system "umount /mnt/backup";
       }
}

sub backupdir{
    $test=`cp --preserve --recursive --update /mnt/backup/$path/ $_[0]
2>&1`;
    &errors("cp",$1) if ($test=~/\/(.*)'+/);
}

    
sub mountdir{
    &errors("mount") if (system "mount","-t","smbfs","-o",
			"username=$username,password=$password",
			"$machine/$share","/mnt/backup");
}
sub errors{
    $workstation= $1 if ($machine=~/\/\/(.*)/);
    select LOG;
    print "($dt) Machine off : $workstation\n" if ($_[0] eq "mount");
    if ($_[0] eq "cp"){
	print "$_[1]";
	my @path=split /\//,$_[1];
	print "$_\n" foreach (@path);
	my $filename = pop(@path);
	print "($dt) File in use :$workstation :$filename\n" if ($_[0]
eq "cp");
    }
    if ($_[0] eq "New"){
	print "\nBackup log for ";
	print `date +%D`;
	print "------------------------\n"}
    select STDOUT;
}


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About