Xcopy backup script

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
jskfanAsked:
Who is Participating?
 
Shift-3Connect With a Mentor Commented:
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
 
jskfanAuthor Commented:
where does the xcopy fit in your script?
xcopy /D /s /H I:\source \\servername\destination


0
 
Shift-3Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
jskfanAuthor Commented:
so do I need to leave that batch file that has xcopy /D /s /H I:\source \\servername\destination
in a separate file?
0
 
Shift-3Commented:
No.  This script incorporates that command on Line 22.
0
 
jskfanAuthor Commented:
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
 
Shift-3Commented:
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
 
jskfanAuthor Commented:
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
 
Shift-3Commented:
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
 
jskfanAuthor Commented:
<<Do you receive email if you run the Blat command manually?>>>

how do you use it?
0
 
Shift-3Commented:
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
 
jskfanAuthor Commented:
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
 
jskfanAuthor Commented:
I guess it worked now after removing the word subject and the word "text message"
0
 
jskfanAuthor Commented:
excellent!!!!!!
0
 
Shift-3Commented:
Ah, I missed a dash.  It should be -subject.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.