Link to home
Start Free TrialLog in
Avatar of yc_yc
yc_yc

asked on

How to ensure 'xcopy' completed and successfully in Perl script?

I use 'xcopy' to copy directory trees and files to another archiving server.
Before I use 'rd' to remove directories and all contents, I have to ensure the
'xcopy' completed and successfully, which means it didn't have any missing
or corrupted archive files during 'xcopy'.  How to verify the 'xcopy' successfully
to avoid data loss before doing 'rd' in Perl script?

system("xcopy $originalDataDir $archiveFolder /e /y /i /k /q");
system("rd /s /q $originalDataDir");
ASKER CERTIFIED SOLUTION
Avatar of matrixnz
matrixnz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since you're using a Perl script, I'd use Perl methods instead of xcopy or robocopy.

File::Copy::Recursive will copy the dir tree and if there is a failure on any portion of the copy process, it will stop and return false.  You can do it in a 2 step process like you're currently doing by using the dircopy() and pathrm() methods.  Or, you can do it in a single step with the dirmove() method.

http://search.cpan.org/~dmuey/File-Copy-Recursive-0.33/Recursive.pm
You can also look at using the File::Xcopy module, which would be my next choice after File::Copy::Recursive.

http://search.cpan.org/~geotiger/File-Xcopy-0.12/Xcopy.pm
To answer you question more directly, you need to check the return value of the system call.

See perldoc -f system

Quoted from the doc:
               The return value is the exit status of the program as returned by the "wait"
               call.  To get the actual exit value shift right by eight (see below).  See also
               "exec".  This is not what you want to use to capture the output from a command,
               for that you should use merely backticks or "qx//", as described in "âSTRINGâ"
               in perlop.  Return value of -1 indicates a failure to start the program
               (inspect $! for the reason).
Avatar of yc_yc
yc_yc

ASKER

Matrixnz,

Thanks for your recommend. I started to learn how to use it. Since the download 'windows resource kits 2003 Server' in Microsoft website cannot be installed on windows 2000, I found the 'robocopy.exe' from the following link: http://www.soloenterprises.org/tips-and-tricks/robocopy.php
Do you think the application robocopy I got from above link shall be fine?

Also, do you think the following command with selected options is good enough to handle directories and files copied from source to destination server?

$robocopy $sourceServer $archiveServer /COPY:DATA /ZB /E /NFL /NDL /NP /LOG:$roboLog

Thanks
Hi yc_yc

The install shouldn't be OS dependant, I have Windows 2003 Resource Kit installed on my XP machine, maybe it was a windows installer problem.  If you have a Windows XP machine then recommend expanding it and copying the relavent files or install RoboCopy GUI http://www.microsoft.com/technet/technetmag/issues/2006/11/UtilitySpotlight/ which basically has Robocopy included.  Not saying that the copy you have is incorrect, but it's difficult to verify if it's the latest version or not.

Anyway I digress, from what you suggest it does look ok, however I'd use /COPYALL to ensure all the information is pulled across including Security, Owner and Audting Info.  If you only wish to collect the data than you'd just need the switch /COPY:D rather than /COPY:DATA unless you meant /COPY:DAT which you shouldn't need as this is the default.

Cheers
Avatar of yc_yc

ASKER

matrixnz,

The windows 2003 Resource Kit need have XP or later windows version to be able to install.
The 'robocopy' application version is 5.1.1.1010, which was downloaded from my last email's link.

The script will be run by the window system tools 'Scheduled Tasks' on a local PC.
Do you think if it is all right to run a script scheduled on a local PC that uses command 'robocopy'
to handle data archiving from source server to destination server through network?

Thanks
Hi yc_yc

Yes that's fine, we have a number of users that do it this way, there are other ways of scripting it as well but adding to 'Scheduled Tasks' is fairly common practice.

Cheers