Wiggins d'Anconia ha scritto:

Goffredo Saffioti wrote:

Good Evening all .

I'm experiencing to write a perl script for to make backup with afio under perl .
But a have some problem when try to execute the system command with the " @_ " this seem to be not the right way for exec this system command "i'm not sure" .
but in past time i'm used this kind of syntax for another script that make the quota for all users in a directory on the same way.
the backup scrpt is as follow:



#!/usr/bin/perl -w



use strict; # always



#use IO::Socket; use IPC::Open2; use Term::ANSIColor; use POSIX ":sys_wait_h";


These three modules are great, but are you sure you need them?



my $status;


Where does $status get used?

#$pid = fork;
#$begin = 1 if ( !$begin );
#$end = $i if ( !$end);


I am assuming these won't be coming back, if they are where do they come from?



#$FULLSYSTEM_SOURCE_DIR = "/home/webhosting/utenti/"; $MOUNT_DIR = "/mnt/test"; $BACKUP_FILE = "Testbackup.cpio"; $FULL_LOG_FILE = "Testlog.log";


These have not been scoped.



$filename = "/home/webhosting/utenti/stats.txt";



No scoping.



@_=`ls -l`; #Read the direcory content


perldoc -f opendir
perldoc -f readdir

Shelling out is a very inefficient, insecure, error prone way of doing this operation, it is much better handled with a dir handle, or with the File::Find module if you want recursion. While Perl likes to do lots of fancy stuff with @_ and $_, lets dispense with the cryptic stuff and use real variable names until we understand when and why we should use cryptic names...

while ($_ = shift @_) {


I assume here you are attempting to loop over the list of files? See above documentation.

@strarr = split ' ';


Why are you splitting on space?
perldoc -f split

open(LOG, ">> $filename") || die "cannot open file";


Include $! in your error messages so you know why it died, not just that it did. This is a very inefficient loop, if you are going to write to the same file every pass over the array then open the file once outside of the while loop, then just keep printing to it rather than re-opening it.


print LOG "$strarr[8]\n"; #and write the filenames on a file


What does the *9th* element have in it? How do you know it is the 9th element? If this is supposed to be the filename check out

perldoc File::Basename

close (LOG);


Again if you open your filehandle outside of the while loop you don't have to close it every iteration, this should go outside the loop.

}



At this point I am very confused because it looks like you are writing to a file that you then read and loop over to call a command on each of the elements, why the write just to read in the same thing? Why not just do your work all in one loop?




open (LOG1, "< /home/webhosting/utenti/stats.txt") || die "cannot open file with LOG1";


Again include $!...

@log1=(<LOG1>);


Ok read the whole thing in, no scoping.

for ($i=1;$i<=((@log1)/4);$i++){ #my dir is too big so i devide the content in parts more little


If you are concerned about the size there is no need to store the whole file to an array and then loop over the array. You do realize you are skipping the first elmenet of the array right? Array indices start at zero. This also isn't dividing your array into parts, it is looping from 1 to 4 so you will only get the 2nd through 5th elements of the array, and even if it was your initial read of the file stores the whole thing to memory so it is a moot point if this for loop did only read a quarter at a time....


open (LOG2, ">> /home/webhosting/utenti/statswork.txt") || die "cannot open file with LOG1";


Include $!... I assume this is a file to let you know how far you have gone in your work? Again open the file outside of the loop once, then print to it inside the loop.

print LOG2 "$log1[$i]"; #and write this part on a new file


open (LOG3, "< /home/webhosting/utenti/statswork.txt") || die "cannot open file with LOG1"; #so... now the trouble .


Why are we reopening the file we just wrote inside the loop?


#this is the system command that i experience to exec but when i exec the script i receive this : sh: -print: command not found
and the script terminate .
So how can i accomplish this trouble?


@_= `/usr/bin/find $log1[$i] -print -depth -mount -xdev | /usr/bin/afio -oulvAZ/ -T3k -s 2000m -L$MOUNT_DIR/$FULL_LOG_FILE $MOUNT_DIR/$BACKUP_FILE`; /


Again use a descriptive variable name for your output capturing. As to why you are getting that particular error message I really can't say, what is in $log1[$i]? You should try cleaning up the program first, then print the commands that should be run rather than just calling them, then copy and paste one of them, see if it does what you want then add it to the looping constructs. What's the trailing backslash doing there? You should also be checking the return status of the command by examining $?, etc. perldoc -f system for more


close (LOG3);
close (LOG2);
close (LOG1);


Assuming you still need to open these at all they should be closed outside the loop....

}

Pleeese help I need backup .
Thanks in advance and sorry for my ugly english .


I am more worried about the Perl ;-). Depending on what your needs are there have to be thousands of backup programs available for free online. If all you want is a dynamic list of directories to pass to find why not just issue a sub command at the shell?


I still also think this might be better handled by File::Find...

http://danconia.org


-- UNTESTED -- #!/usr/bin/perl use strict; use warnings;


my $MOUNT_DIR = "/mnt/test"; my $BACKUP_FILE = "Testbackup.cpio"; my $FULL_LOG_FILE = "Testlog.log";


my $backup_dir = '/home/user'; my $running_output = '/tmp/im_running.txt';


my $OUTPUT; open($OUTPUT, ">>$running_output") or die "Can't open output file: $!";


my $DIRHANDLE; opendir($DIRHANDLE, $backup_dir) or die "can’t opendir $backup_dir: $!"; while (my $file = readdir($DIRHANDLE)) { next if (($file eq '.') || ($file eq '..'));


unless (-d $file) { print $OUTPUT "Skipping non-directory: $file\n"; next; } unless (-r $file) { print $OUTPUT "Skipping unreadable directory: $file\n"; next; }


print $OUTPUT "Backing up starting at $file...\n";



my @find_output = `/usr/bin/find $file -print -depth -mount -xdev | /usr/bin/afio -oulvAZ/ -T3k -s 2000m -L$MOUNT_DIR/$FULL_LOG_FILE $MOUNT_DIR/$BACKUP_FILE`;
# do some checking of $? here...



print $OUTPUT "Back up of $file complete.\n"; } closedir $DIRHANDLE;


print $OUTPUT "I am outta here....\n";



close($OUTPUT);




Ok your script work fine, but my problem still persist, because the work directory is too big so i need to do backup on more little portion of dir , i attempt to process i wish to process $backup_dir/2 or /4 how can do it?
Thnx in advance .



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



Reply via email to