Solved

Xcopy to RoboCopy

Posted on 2010-09-09
24
1,050 Views
Last Modified: 2012-05-10
So I have a script that I run to copy files. It normally uses xcopy, I've changed it over to robocopy with some suggestions from others here...however it isn't currently doing what I want it to do.

I have attached the code.

The main points to note are:
1.) The need to write a log file....which appears to be working correctly. I don't want this information appending everyday...a fresh log is necessary. If someone can think of a way to increment and add a date to each log file that would be AMAZING. Then i could keep a history of individual log files.

2.) robocopy d:\data u: /e /zb /r:3 /log:%LogFile1Data% 2>&1

This line really isn't getting the job done. When I run this file I get the following:

U: was deleted successfully.

w: was deleted successfully.

The command completed successfully.

The command completed successfully.


 Log File : G:\FILESERVER_Backup_Logs\NAS01Databackuplog.log


Neither of the emails send....so at this point I am assuming that I have completly botched the 'Robocopy' translation from 'Xcopy'.




@echo off



REM Clears previously mapped drives

net use U: /d /y

net use w: /d /y





REM Assigning the network drives to the NAS devices

net use U: \\NAS01\DATA

net use w: \\NAS02\DATA





set LogFile1Data="G:\FILESERVER_Backup_Logs\NAS01Databackuplog.log"



set LogFile2Data="G:\FILESERVER_Backup_Logs\NAS02Databackuplog.log"





REM Copies the DATA folder to Network Attached Storage device NAS01 and writes the log file on the G: drive

robocopy d:\data u: /e /zb /r:3 /log:%LogFile1Data% 2>&1

blat.exe - -body "FILESERVER DATA Backup to NAS01 completed with %Errorlevel% errors. The log can be viewed at \\FILESERVER\FILESERVER_Backup_Logs" -server EXCHANGE -f Backup6@domain.com -t domain-InformationServices@domain.com -s "FILESERVER DATA Backup to NAS01 finished with %Errorlevel% errors" 





REM Copies the DATA folder to Network Attached Storage device NAS02 and writes the log file on the G: drive

robocopy d:\data w: /e /zb /r:3 /log:%LogFile2Data% 2>&1

blat.exe - -body "FILESERVER DATA Backup to NAS02 completed with %Errorlevel% errors. The log can be viewed at \\FILESERVER\FILESERVER_Backup_Logs" -server EXCHANGE -f Backup7@domain.com -t domain-InformationServices@domain.com -s "FILESERVER DATA Backup to NAS02 finished with %Errorlevel% errors" 





REM Backup Complete!

Open in new window

0
Comment
Question by:Sleestack90
  • 11
  • 10
  • 2
  • +1
24 Comments
 
LVL 7

Assisted Solution

by:compaqus
compaqus earned 150 total points
ID: 33636963
this will give you the date to use in a command
you can do a rename of the file after it is created for example...


@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
set year=%Year:~2%
@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%


@echo %Year%%Month%%Day%databackup.log
0
 
LVL 3

Expert Comment

by:DeltaR7
ID: 33637005
I think it would be handy to use VBS script instead of a batch file
with this you can specify your date & time stamps for the log files    (functions date() or now() are handy)
 to call robocpy from a vbs script you can use following code.
Set objShell = CreateObject("Wscript.Shell")

objSource = InputBox("Enter Source")
objDestination = InputBox("Enter Destination")

objCommand = "RoboCopy.Exe " & Chr(34) & objSource & Chr(34) & " " & Chr(34) & objDestination & Chr(34) & " /e /r:1 /w:2"
objShell.Run(objCommand)

MsgBox "Done"
 
 
For the emails, i can see you are using BLAT.exe, however if you use VBS, this can be included in your script.
Let me check if i can find some vbs email code
0
 
LVL 7

Expert Comment

by:compaqus
ID: 33637013
For sending emails I use this one, you can also attach using mpack:

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm
0
 
LVL 7

Expert Comment

by:compaqus
ID: 33637291
ok, so add this at the end of the batch to rename the files with the date:

As for the email, i don't see %Errorlevel% defined before using it in the email.  

I use bmail, you can get it here http://www.beyondlogic.org/solutions/cmdlinemail/bmailv107.zip

and you could replace the blat lines with something like this:

bmail -s YOURemailserver -t to@domain.com -f from@domain.com -h -a "YOUR SUBJECT" -b "your email body"


@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
set year=%Year:~2%
@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%


ren g:\FILESERVER_Backup_Logs\NAS01Databackuplog.log NAS01Databackuplog%Year%%Month%%Day%.log
ren g:\FILESERVER_Backup_Logs\NAS02Databackuplog.log NAS02Databackuplog%Year%%Month%%Day%.log

Open in new window

0
 
LVL 7

Expert Comment

by:compaqus
ID: 33637464
you have 2 "-" after blat
you only need one.

blat.exe - -body "FILESERVER DATA Backup.........................

change to:
blat.exe  -body "FILESERVER DATA Backup.........................

0
 

Author Comment

by:Sleestack90
ID: 33637586
OK...wow alot fo response. I'd like to start with, thank you for all of the ideas. Lets narrow some of this down just a bit.

@DeltaR7: Not interested in using VBScript, it always ends up being more involved, and if i was starting from scratch that might be fine, but the batch files do what i need them to do...so i can't see the benefit of switching (unless i am missing something)

@compaqus: blat works fine for the email and it always has when i was using xcopy....for some reason it just isn't working now...it HAS worked with variations of the code i listed above....the error level works correctly as well.

The date code looks pretty useful....Question: will this code change the date on the log file and then allow for the creation of a new log file next time the batch file has run?

0
 
LVL 7

Expert Comment

by:compaqus
ID: 33637797
The code there will rename the file and add the date. The new file will get written next day, then it will be renamed also, and so on...

Have you noticed the 2 "-" at the begining?  (not sure about this, will get blat and see)
0
 

Author Comment

by:Sleestack90
ID: 33637909
@compaqus: I will try the date thing....as for blat....thats just how it works....no idea why, but it's correct :)
0
 

Author Comment

by:Sleestack90
ID: 33638012
The main problem here being that the files aren't copying correctly most of the time. Does it look like one of the switches I am using isn't correct?

Here is the 'xcopy' version for what I am trying to accomplish:

xcopy d:\data w:\ /e/c/h/d/y >>%LogFile1Users% 2>&1

0
 
LVL 7

Expert Comment

by:compaqus
ID: 33638028
you were right it works!

as for not sending emails you could add "pause" after the blat line to see what error you get there. on my computer it worked flawlessly.
0
 
LVL 7

Expert Comment

by:compaqus
ID: 33638399
looks to me you may be missing the copy newer or changed files option.

add the /mir switch (mirror)

what isn't getting copied?
0
 

Author Comment

by:Sleestack90
ID: 33639111
@compaqus: I think you might be right...let me try that.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Sleestack90
ID: 33639255
I added the error messages in the code section below.

I can see that one of my devices is not attaching correctly which i can fix.

However the part that reads:

Subject: FILESERVER DATA Backup to NAS01 finished with 16 errors
Login name is Backup6@domain.com


This kind of concerns me....I don't understand where the "Login name is Backup6@domain.com" is coming from really? Does it think that there should be login information, and that's why the rest of the script isn't running?
The network connection could not be found.



More help is available by typing NET HELPMSG 2250.



w: was deleted successfully.



System error 1351 has occurred.



Configuration information could not be read from the domain controller, either b

ecause the machine is unavailable, or access has been denied.



The command completed successfully.





 Log File : G:\FILESERVER_Backup_Logs\NAS01Databackuplog.log

Press any key to continue . . .

Blat v2.6.2 w/GSS encryption (build : Feb 25 2007 12:06:19)



Sending stdin.txt to domain-InformationServices@domain.com



Subject: FILESERVER DATA Backup to NAS01 finished with 16 errors

Login name is Backup6@domain.com



 Log File : G:\FILESERVER_Backup_Logs\NAS02Databackuplog.log

Open in new window

0
 
LVL 7

Expert Comment

by:compaqus
ID: 33639442
the login part comes from the blat line. After -f add a valid email address, blat is trying to sent that email address to the server.
also, is your email server named "EXCHANGE" ?

right now blat is trying to send an email from "Backup6@domain.com" (-f switch)  to "domain-InformationServices@domain.com" (-t switch) using a server named "EXCHANGE"
0
 
LVL 7

Expert Comment

by:compaqus
ID: 33639473
I would also put pause after each line to see where the heck are those 16 errors.
0
 

Author Comment

by:Sleestack90
ID: 33640216
If we correct the problem, which is the line i've posted, we won't need to troubleshoot the 16 errors....it's really one error that is causing 16 problems.
0
 

Author Comment

by:Sleestack90
ID: 33640310
if i run the following commands in a cmd prompt, it works fine............it's the logging bit
robocopy d:\data u: /e /zb /mir /r:3

I have attached the new code below for review
@echo off



REM Clears previously mapped drives

net use U: /d /y

net use w: /d /y





REM Assigning the network drives to the NAS devices

net use U: \\NAS01\DATA

net use w: \\NAS02\DATA





set LogFile1Data="G:\FILESERVER_Backup_Logs\NAS01Databackuplog.log"



set LogFile2Data="G:\FILESERVER_Backup_Logs\NAS02Databackuplog.log"





REM Copies the DATA folder to Network Attached Storage device NAS01 and writes the log file on the G: drive

robocopy d:\data u: /e /zb /mir /r:3 /log:%LogFile1Data% 2>&1



Pause



blat.exe - -body "FILESERVER DATA Backup to NAS01 completed with %Errorlevel% errors. The log can be viewed at \\FILESERVER\FILESERVER_Backup_Logs" -server EXCHANGE -f Backup6@DOMAIN.com -t DOMAIN-InformationServices@DOMAIN.com -s "FILESERVER DATA Backup to NAS01 finished with %Errorlevel% errors" 





REM Copies the DATA folder to Network Attached Storage device NAS02 and writes the log file on the G: drive

robocopy d:\data w: /e /zb /mir /r:3 /log:%LogFile2Data% 2>&1



pause 



blat.exe - -body "FILESERVER DATA Backup to NAS02 completed with %Errorlevel% errors. The log can be viewed at \\FILESERVER\FILESERVER_Backup_Logs" -server EXCHANGE -f Backup7@DOMAIN.com -t DOMAIN-InformationServices@DOMAIN.com -s "FILESERVER DATA Backup to NAS02 finished with %Errorlevel% errors" 





REM Renames the log files using the date

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(

    Set Month=%%A

    Set Day=%%B

    Set Year=%%C

)

set year=%Year:~2%

@echo DAY = %Day%

@echo Month = %Month%

@echo Year = %Year%





ren g:\FILESERVER_Backup_Logs\NAS01Databackuplog.log NAS01Databackuplog%Year%%Month%%Day%.log

ren g:\FILESERVER_Backup_Logs\NAS02Databackuplog.log NAS02Databackuplog%Year%%Month%%Day%.log





REM Backup Complete!

Open in new window

0
 
LVL 7

Expert Comment

by:compaqus
ID: 33640799
I just tested the script, it works perfectly on my computer. I have only changed the paths to match my server, I get 0 errors and the email every time.
0
 

Author Comment

by:Sleestack90
ID: 33640859
Roger that, I'll work all of this backwards...adding one piece at a time until I figure out what the problem is...and then i'll post.
0
 
LVL 7

Expert Comment

by:compaqus
ID: 33640943
me, I'm using this action script editor
http://www.sephiroth.it/python/sepy.php
it's easier to keep track of what's there, it has highlighting.
0
 
LVL 6

Expert Comment

by:bkoehler-mpr
ID: 33652132
A few things:
Make sure both Robocopy and Blat are in your system %path%
Don't map drives; use UNC paths
When you say there are 16 errors do you mean the errorlevel is 16?
I simplified some of your language and the script, e.g. with Robocopy an errorlevel of 1 means files were copied, not that there was an error.
@echo off

REM Get the date for log files

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(

    Set Month=%%A

    Set Day=%%B

    Set Year=%%C

)

set year=%Year:~2%

@echo DAY = %Day%

@echo Month = %Month%

@echo Year = %Year%





set LogFile="G:\FILESERVER_Backup_Logs\%Year%%Month%%Day%_NAS01.log"

REM Copy data to NAS01 logging to the G:\

robocopy d:\data \\NAS01\DATA /zb /mir /NP /r:3 /log:%LogFile%

echo %Errorlevel% >> %LogFile%

blat -t DOMAIN-InformationServices@DOMAIN.com -f Backup6@DOMAIN.com  -s "FILESERVER DATA Backup to NAS01 finished with an error Level of %Errorlevel%" -server EXCHANGE -body "FILESERVER DATA Backup to NAS01 completed with an error Level of %Errorlevel%. Logs are available at \\FILESERVER\FILESERVER_Backup_Logs"





set LogFile="G:\FILESERVER_Backup_Logs\%Year%%Month%%Day%_NAS02.log"

REM Copy data to NAS02 logging to the G:\

robocopy d:\data \\NAS02\DATA /zb /mir /NP /r:3 /log:%LogFile%

echo %Errorlevel% >> %LogFile%

blat -t DOMAIN-InformationServices@DOMAIN.com -f Backup7@DOMAIN.com  -s "FILESERVER DATA Backup to NAS02 finished with an error Level of %Errorlevel%" -server EXCHANGE -body "FILESERVER DATA Backup to NAS02 completed with an error Level of %Errorlevel%. Logs are available at \\FILESERVER\FILESERVER_Backup_Logs" 



REM Backup Complete!

Open in new window

0
 

Author Comment

by:Sleestack90
ID: 33661453
@bkoehler-mpr::

1.) Robocopy and blat are installed correctly.
2.) The UNC thing is a good idea....mapping those drives was 'iffy' at best....the codes correct, but for whatever reason the mapping doesn't work sometime.
3.) When I say 16 errors, I mean the 'errorlevel' is 16, which I am now assuming is not the same thing at 16 errors.

With the new information I have about #3 above....i am wondering how to echo and include something other than the 'errorlevel' number in my email?

For instance:

if errorlevel 16 echo ***FATAL ERROR*** & goto end
if errorlevel 15 echo FAIL MISM XTRA COPY & goto end
if errorlevel 14 echo FAIL MISM XTRA & goto end
if errorlevel 13 echo FAIL MISM COPY & goto end
if errorlevel 12 echo FAIL MISM & goto end
if errorlevel 11 echo FAIL XTRA COPY & goto end
if errorlevel 10 echo FAIL XTRA & goto end
if errorlevel 9 echo FAIL COPY & goto end
if errorlevel 8 echo FAIL & goto end
if errorlevel 7 echo MISM XTRA COPY & goto end
if errorlevel 6 echo MISM XTRA & goto end
if errorlevel 5 echo MISM COPY & goto end
if errorlevel 4 echo MISM & goto end
if errorlevel 3 echo XTRA COPY & goto end
if errorlevel 2 echo XTRA & goto end
if errorlevel 1 echo COPY & goto end
if errorlevel 0 echo –no change– & goto end
:end

Basically, when I get the email, I want to know what action I need to take instead of referencing another source.
0
 
LVL 6

Accepted Solution

by:
bkoehler-mpr earned 350 total points
ID: 33665191
I slightly modified your errorlevel example from the Robocopy doc.
@echo off
REM Get the date for log files
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
set year=%Year:~2%
@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%

set LogFile="G:\FILESERVER_Backup_Logs\%Year%%Month%%Day%_NAS01.log"
REM Copy data to NAS01 logging to the G:\
robocopy d:\data \\NAS01\DATA /zb /mir /NP /r:3 /log:%LogFile%


if errorlevel 16 set result="***FATAL ERROR***" & goto nas01
if errorlevel 15 set result="FAIL MISM XTRA COPY" & goto nas01
if errorlevel 14 set result="FAIL MISM XTRA" & goto nas01
if errorlevel 13 set result="FAIL MISM COPY" & goto nas01
if errorlevel 12 set result="FAIL MISM" & goto nas01
if errorlevel 11 set result="FAIL XTRA COPY" & goto nas01
if errorlevel 10 set result="FAIL XTRA" & goto nas01
if errorlevel 9 set result="FAIL COPY" & goto nas01
if errorlevel 8 set result="FAIL" & goto nas01
if errorlevel 7 set result="MISM XTRA COPY" & goto nas01
if errorlevel 6 set result="MISM XTRA" & goto nas01
if errorlevel 5 set result="MISM COPY" & goto nas01
if errorlevel 4 set result="MISM" & goto nas01
if errorlevel 3 set result="XTRA COPY" & goto nas01
if errorlevel 2 set result="XTRA" & goto nas01
if errorlevel 1 set result="COPY" & goto nas01
if errorlevel 0 set result="--no change--" & goto nas01
:nas01
blat -t DOMAIN-InformationServices@DOMAIN.com -f Backup6@DOMAIN.com  -s "FILESERVER DATA Backup to NAS01 finished with a result of %result%" -server EXCHANGE -body "FILESERVER DATA Backup to NAS01 completed with a result of %result%. Logs are available at \\FILESERVER\FILESERVER_Backup_Logs"



set LogFile="G:\FILESERVER_Backup_Logs\%Year%%Month%%Day%_NAS02.log"
REM Copy data to NAS02 logging to the G:\
robocopy d:\data \\NAS02\DATA /zb /mir /NP /r:3 /log:%LogFile%

if errorlevel 16 set result="***FATAL ERROR***" & goto nas02
if errorlevel 15 set result="FAIL MISM XTRA COPY" & goto nas02
if errorlevel 14 set result="FAIL MISM XTRA" & goto nas02
if errorlevel 13 set result="FAIL MISM COPY" & goto nas02
if errorlevel 12 set result="FAIL MISM" & goto nas02
if errorlevel 11 set result="FAIL XTRA COPY" & goto nas02
if errorlevel 10 set result="FAIL XTRA" & goto nas02
if errorlevel 9 set result="FAIL COPY" & goto nas02
if errorlevel 8 set result="FAIL" & goto nas02
if errorlevel 7 set result="MISM XTRA COPY" & goto nas02
if errorlevel 6 set result="MISM XTRA" & goto nas02
if errorlevel 5 set result="MISM COPY" & goto nas02
if errorlevel 4 set result="MISM" & goto nas02
if errorlevel 3 set result="XTRA COPY" & goto nas02
if errorlevel 2 set result="XTRA" & goto nas02
if errorlevel 1 set result="COPY" & goto nas02
if errorlevel 0 set result="--no change--" & goto nas02
:nas02
blat -t DOMAIN-InformationServices@DOMAIN.com -f Backup7@DOMAIN.com  -s "FILESERVER DATA Backup to NAS02 finished with a result of %result%" -server EXCHANGE -body "FILESERVER DATA Backup to NAS02 completed with a result of %result%. Logs are available at \\FILESERVER\FILESERVER_Backup_Logs"


REM Backup Complete!

Open in new window

0
 

Author Closing Comment

by:Sleestack90
ID: 33665427
Perfect.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Having issues meeting security compliance criteria because of those pesky USB drives? Then I can help you! This article will explain how to disable USB Mass Storage devices in Windows Server 2008 R2.
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now