Solved

Xcopy backup script

Posted on 2007-11-30
15
5,211 Views
Last Modified: 2012-05-05
I have a batch file that runs xcopy and copies files/folders from one server to another this batch file is scheduled with task scheduler.
I need someone to help elaborate this batch file so that it will be able to do the following tasks:
-it will create a text file that will serve as a log.
  the log will show the start time of the xcopy backup and the end time.
-it will compare the size of the source and destination and logs their sizem in the text file I previously  mentionned.
- it will send email to admin@domain.com with the content of the text file.

the batch file code is:
xcopy /D /s /H I:\source \\servername\destination

Thanks
0
Comment
Question by:jskfan
  • 8
  • 7
15 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 20383025
Here is a batch file which should do what you want.  It requires Diruse.exe from the Server 2003 Support Tools and Blat.exe, a free command-line email utility.  These executables should be placed in the same directory as the batch file.
http://www.microsoft.com/downloads/details.aspx?familyid=96A35011-FD83-419D-939B-9A772EA2DF90
http://www.blat.net

I left your xcopy command as you had it, but note that the /D switch should include a date.


@echo off

setlocal
 

REM File locations

set log=copylog.txt

set source=I:\source

set dest=\\servername\destination
 

REM Email options

set to=-to admin@domain.com

set from=-f admin@domain.com

set subject=-subject "Auto Message: copy log %date%%time%"

set smtp=-server yoursmtpserver.domain.com
 

REM Generates log and copies files

if exist "%log%" del "%log%"

echo Start time: %date%%time% >> "%log%"

for /F "tokens=1 usebackq" %%G in (`diruse "%source%" /M`) do set sourcesize=%%G

for /F "tokens=1 usebackq" %%G in (`diruse "%dest%" /M`) do set destsizepre=%%G

echo Source size before copy: %sourcesize% MB >> "%log%"

echo Destination size before copy: %destsizepre% MB >> "%log%"

xcopy /D /s /H "%source%" "%dest%" > NUL 2>&1

for /F "tokens=1 usebackq" %%G in (`diruse "%dest%" /M`) do set destsizepost=%%G

echo Destination size after copy: %destsizepost% MB >> "%log%"

echo Finish time: %date%%time% >> "%log%"
 

REM Emails log

blat "%log%" %to% %from% %subject% %smtp%
 

endlocal

Open in new window

0
 

Author Comment

by:jskfan
ID: 20384073
where does the xcopy fit in your script?
xcopy /D /s /H I:\source \\servername\destination


0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20384518
Line 22.  

I:\Source is set as the Source variable on Line 6.
\\servername\destination is set as the Dest variable on Line 7.
0
 

Author Comment

by:jskfan
ID: 20384678
so do I need to leave that batch file that has xcopy /D /s /H I:\source \\servername\destination
in a separate file?
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20384802
No.  This script incorporates that command on Line 22.
0
 

Author Comment

by:jskfan
ID: 20390228
by the way, since nobody will be logged on to the source(server1) and nobody will be logged on to the destination(server2), in this case would the script run as it is scheduled with scheduled task?
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20390237
You should change "I:\source" in the Source variable to a UNC path (e.g. \\server\share\source) if it will be running as a scheduled task.  Otherwise it should work fine.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:jskfan
ID: 20395335
it works fine. the only thing that is not working is the email. I haven't received an email after the backuo jobs done and the log file was logged.
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20396234
Did you customize the smtp variable under "REM Email options" with the name of your SMTP server?  Do you receive email if you run the Blat command manually?
0
 

Author Comment

by:jskfan
ID: 20396413
<<Do you receive email if you run the Blat command manually?>>>

how do you use it?
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20402540
You can view all the switches by typing Blat /?, but all you really need to test it is

blat somefile.txt -to admin@domain.com -f admin@domain.com subject "test message" -server yoursmtpserver.domain.com
0
 

Author Comment

by:jskfan
ID: 20402819
yoursmtpserver  you mean my exchange server. correct?
well i tried it and it hasn't sent me any message. Is the machine from where I am running the Blat command supposed to have SMTP service on it?
0
 

Author Comment

by:jskfan
ID: 20402845
I guess it worked now after removing the word subject and the word "text message"
0
 

Author Comment

by:jskfan
ID: 20402980
excellent!!!!!!
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20403351
Ah, I missed a dash.  It should be -subject.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

24 Experts available now in Live!

Get 1:1 Help Now