Solved

Xcopy backup script

Posted on 2007-11-30
15
5,205 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

21 Experts available now in Live!

Get 1:1 Help Now