Solved

Xcopy to RoboCopy

Posted on 2010-09-09
24
1,067 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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
 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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