• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3060
  • Last Modified:

Writing Log file from .bat file running a backup....

I am running a xcopy to backup some folders, what I'd like to do now is keep a log of everything that happens, like when the file was copied, when the backup was completed/started, etc...

so far what I have gotten it to do is write a line to the file "DB Backup Completed on date"

so to recap, what I would like to do is:

*******************************************
DB Backup started 2/6/2008 23:00
File: xxxxx.mdb - 2/6/2008 23:05
File: xxxxx.mdb - 2/6/2008 23:10
DB Backup Complete 2/6/2008 23:30
*******************************************

so ...  how can I write the filename, and a date timestamp to the log file?

below is what I have now...
scm -Action 6 -Server gm-base -Service MSSQLServer -Silent 1
 
xcopy d:\db\*.* e:\temp\db /S /H /R /E /K /Y
echo DB Backup Completed on date >> backuplog.txt
 
scm -Action 1 -Server gm-base -Service MSSQLServer -Silent 1
 
REM This backups SQl databases....

Open in new window

0
kevp75
Asked:
kevp75
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
debuggerauCommented:
robocopy from the resource kit should help..
0
 
TextReportCommented:
in your ECHO you can include %DATE% and %TIME%
Cheers, Andrew
0
 
TextReportCommented:
Sorry forgot you can add a redirect on the XCOPY command
xcopy d:\db\*.* e:\temp\db /S /H /R /E /K /Y >>backuplog.txt

Cheers, Andrew
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
kevp75Author Commented:
ok, cool...   so how can I get the files listed like I posted above?

right now it simply writes the filelist, and the DB Backup stuff....

@debuggerau.... not sure what robocopy is, nor do I have the resource kit
0
 
kevp75Author Commented:
here's what it is writing right now.  I'll post the "code" below it:

***********************************
Backup Started on Wed 02/06/2008 19:19:56.37
***********************************
C:\Inetpub\ClubSites\default.aspx
C:\Inetpub\ClubSites\footer.ascx
C:\Inetpub\ClubSites\header.ascx
C:\Inetpub\ClubSites\_notes\default.aspx.mno
C:\Inetpub\ClubSites\_notes\header.ascx.mno
5 File(s) copied
***********************************
Backup Completed on Wed 02/06/2008 19:19:56.46
***********************************

ideally I'd like it to be:
***********************************
Backup Started on Wed 02/06/2008 19:19:56.37
***********************************
File: C:\Inetpub\ClubSites\default.aspx  - Wed 02/06/2008 19:19:56.46
File: C:\Inetpub\ClubSites\footer.ascx - Wed 02/06/2008 19:19:56.46
File: C:\Inetpub\ClubSites\header.ascx - Wed 02/06/2008 19:19:56.46
File: C:\Inetpub\ClubSites\_notes\default.aspx.mno - Wed 02/06/2008 19:19:56.46
File: C:\Inetpub\ClubSites\_notes\header.ascx.mno - Wed 02/06/2008 19:19:56.46
5 File(s) copied
***********************************
Backup Completed on Wed 02/06/2008 19:19:56.46
***********************************

echo *********************************** >> backuplog.txt
echo Backup Started on %DATE% %TIME% >> backuplog.txt
echo *********************************** >> backuplog.txt
 
xcopy C:\Inetpub\ClubSites\*.* C:\backup /S /H /R /E /K /Y >> backuplog.txt
 
echo *********************************** >> backuplog.txt
echo Backup Completed on %DATE% %TIME% >> backuplog.txt
echo *********************************** >> backuplog.txt

Open in new window

0
 
kevp75Author Commented:
update:
i got it to write the filename based on the date/timestamp....however, when I try to echo date/time for the files, it bombs out:


set startDate=%date%
set startTime=%time%
 
set sdy=%startDate:~10%
set /a sdm=1%startDate:~4,2% - 100
set /a sdd=1%startDate:~7,2% - 100
set /a sth=%startTime:~0,2%
set /a stm=1%startTime:~3,2% - 100
set /a sts=1%startTime:~6,2% - 100
 
echo ******************************************************* >> %sdm%_%sdd%_%sdy%-%sth%%stm%_backup_log.txt
echo Backup Started on %DATE% %TIME% >> %sdm%_%sdd%_%sdy%-%sth%%stm%_backup_log.txt
echo ******************************************************* >> %sdm%_%sdd%_%sdy%-%sth%%stm%_backup_log.txt
 
xcopy C:\Inetpub\ClubSites\*.* C:\backup /S /H /R /E /K /Y echo %DATE% %TIME% >> %sdm%_%sdd%_%sdy%-%sth%%stm%_backup_log.txt
 
echo ******************************************************* >> %sdm%_%sdd%_%sdy%-%sth%%stm%_backup_log.txt
echo Backup Completed on %DATE% %TIME% >> %sdm%_%sdd%_%sdy%-%sth%%stm%_backup_log.txt
echo ******************************************************* >> %sdm%_%sdd%_%sdy%-%sth%%stm%_backup_log.txt

Open in new window

0
 
TextReportCommented:
ECHO %DATE% %TIME% are not valid parameters for xcopy and looking at the help on copy and xcopy there isn't a way to get the time displayed for each copy.

I have taken a look at using FOR inside the batch file but I have been unable to get it to work, the theory being you could use a FOR to loop through every file and copy each file individually and also ECHO %DATE% %TIME%  to the log.

Cheers, Andrew
0
 
kevp75Author Commented:
sorry debuggerau, robocopy does not do what I am intending to do
0
 
Steve KnightIT ConsultancyCommented:
Try a for loop then if you wish.  This runs down all files under the source directory (c:\tst in this case) and copies them to c:\backup.

It creates a logfile.txt which gets appended to each time in this format.  If there is an error it will write "Error copying "file1.txt"

09/02/2008 13:03:01.70 Backup Started
09/02/2008 13:03:01.76 Copied "file1.txt"
09/02/2008 13:03:01.80 Copied "file2.txt"
09/02/2008 13:03:01.84 Copied "file3.txt"
09/02/2008 13:03:01.87 Copied "file4.txt"
09/02/2008 13:03:01.91 Copied "file5.txt"
09/02/2008 13:03:01.97 Copied "test2.txt"
09/02/2008 13:03:01.97 Backup Ended


Steve

@echo off
set source=c:\test
set dest=c:\backup
set logfile=c:\backup\logfile.txt
echo %date% %time% Backup Started>>%logfile%
pushd "%source%"
for /f "tokens=* delims=" %%a in ('dir /b /s /a-d') do call :copyfile "%%a" "%%~nxa"
POPD
echo %date% %time% Backup Ended>>%logfile%
goto :eof

:copyfile
echo Copying file %1
echo f|xcopy %1 "%dest%" /h/r/y/k
if errorlevel 1 echo %date% %time% Error %errorlevel% copying %2 >>%logfile% & goto :eof
echo %date% %time% Copied %2 >>%logfile%

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now