We help IT Professionals succeed at work.

Windows backup script - Better way to determine current day?

Ubertam
Ubertam asked
on
I've got this backup script I use to back up using ntbackup to our NAS and have it automatically cull old files.  The backups are growing and I needed to eliminate the number of normal backups by one and increase retention to 2 weeks (normal backup every other week).  I changed the backup script to go for 2 full weeks, but out of the blue, it created empty folders for 11-Wednesday and 13-Friday (on the 11th and the 13th, in fact).  I think it's related to how the batch file is determining the day of the week.

Is there a better way to do this?

:HOME
net use G: /delete /y
net use \\YoyosRUsbackup\backup /delete /y
IF NOT EXIST G:\ net use G: \\YoyosRUsbackup\backup /user:admin YoyosRUsPassword
g:
cd\
IF NOT EXIST Weekly CALL d:\cycle.bat
cd Weekly
IF NOT EXIST 0 CALL d:\cycle.bat
cd 0

IF NOT EXIST 01-Sunday GOTO SUNDAY

:DATES
for /f %%a in ('date /t') do set DAY=%%a
IF %DAY%==Sun EXIT
IF %DAY%==Mon GOTO MONDAY
IF %DAY%==Tue GOTO TUESDAY
IF %DAY%==Wed GOTO WEDNESDAY
IF %DAY%==Thu GOTO THURSDAY
IF %DAY%==Fri GOTO FRIDAY
IF %DAY%==Sat GOTO SUNDAY

:SUNDAY
IF EXIST 01-Sunday GOTO SATURDAY
md 01-Sunday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\Full.bks" /n "Backup created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m normal /j "Full" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\01-Sunday\YoyosRUsfull.bkf"
exit

:MONDAY
IF EXIST 02-Monday GOTO MONDAY2
md 02-Monday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\02-Monday\YoyosRUsinc.bkf"
exit

:TUESDAY
IF EXIST 03-Tuesday GOTO TUESDAY2
md 03-Tuesday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\03-Tuesday\YoyosRUsinc.bkf"
exit

:WEDNESDAY
IF EXIST 04-Wednesday GOTO WEDNESDAY2
md 04-Wednesday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\04-Wednesday\YoyosRUsinc.bkf"
exit

:THURSDAY
IF EXIST 05-Thursday GOTO THURSDAY2
md 05-Thursday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\05-Thursday\YoyosRUsinc.bkf"
exit

:FRIDAY
IF EXIST 06-Friday GOTO FRIDAY2
md 06-Friday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\06-Friday\YoyosRUsinc.bkf"
exit

:SATURDAY
IF EXIST 07-Saturday GOTO SUNDAY2
md 07-Saturday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\02-Monday\YoyosRUsinc.bkf"
exit

:SUNDAY2
IF EXIST 08-Sunday GOTO SATBREAK
md 08-Sunday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\03-Tuesday\YoyosRUsinc.bkf"
exit

:MONDAY2
IF EXIST 09-Monday GOTO SATBREAK
md 09-Monday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\04-Wednesday\YoyosRUsinc.bkf"
exit

:TUESDAY2
IF EXIST 10-Tuesday GOTO SATBREAK
md 10-Tuesday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\05-Thursday\YoyosRUsinc.bkf"
exit

:WEDNESDAY2
IF EXIST 11-Wednesday GOTO SATBREAK
md 11-Wednesday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\06-Friday\YoyosRUsinc.bkf"
exit

:THURSDAY2
IF EXIST 12-Thursday GOTO SATBREAK
md 12-Thursday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\05-Thursday\YoyosRUsinc.bkf"
exit

:FRIDAY2
IF EXIST 13-Friday GOTO SATBREAK
md 13-Friday
C:\WINDOWS\system32\ntbackup.exe backup "@C:\Documents and Settings\ChiqutaBanana\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\YoyosRUsDiff.bks" /n "YoyosRUsinc.bkf created %DATE% at %TIME%" /d "Set created 7/20/2010" /v:no /r:yes /rs:no /hc:off /m incremental /j "YoyosRUsInc" /l:s /f "\\YoyosRUsbackup\backup\Weekly\0\06-Friday\YoyosRUsinc.bkf"
exit

:SATBREAK
CALL d:\cycle.bat
GOTO HOME

Open in new window

This script runs every day at 6:00PM.  It also runs Saturday at 3AM (so it can run it's full backup, which would go into the 01-Sunday folder).  The full backup usually takes around 60 hours to run (hence why I want it to start right after the differential backup should be finished).

Any hints on how to run this better?  Am I getting the days wrong?  Is there a VB version that works better?  Thanks.
Comment
Watch Question

Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Commented:
Your welcome to try out my backup script.  It has several options including a rotation scheme and is designed for use with NTBackup.

see http://www.lwcomputing.com/mysoftware.asp
Commented:
My ntbackup script is pasted below:

I back up to an external USB drive.

The resulting files are in this format:
Thu-04-19-2012.bkf

So this is my backup that ran early this morning after midnight (Thursday, April 19 2012).

script below:

for /f "Tokens=1-4 Delims=/ " %%i in ( 'date /t') do set dt=%%i-%%j-%%k-%%l
for /f "Tokens=1-2" %%i in ('time /t') do set tm=-%%i-%%j
rem
set tm=%tm::=-%
set dtt=%dt%%tm%
set dri=f:\
set exn=.bkf
set pth=%dri%%dt%%exn%
rem
ntbackup.exe backup "@c:\ntbackup\backup.bks" /v:yes /r:no /rs:no /hc:on /m normal /j "%dtt%" /l:s /f %pth%


Then at the end of the script I run a command to prune all .bkf files older than 1 week:
Forfiles -p F:\ -m *.bkf -d -7 -c "CMD /C del @file"

the F:\ is my usb drive where the backup files are kept. the "-d -7" means delete older than 7 days,
Commented:
Your script is not that pretty, but without rewriting everything, if I understood it right, 11-Wednesday and 13-Friday folders, which were created on the right days, but the problem was that they are empty, right?

If that is so, it looks like the problem is not on how your script establishes the day of the week (DOW), but only that the backup command failed on these days.

I would suggest including some error logging so you can troubleshoot operation failures. Try checking %ERRORLEVEL%, testing if backup file was created, and saving output from backup command to log file.

About establishing DOW, if it is working in your computer as it is, that's fine. But have in mind that date format can vary between computers, which would break your script.
Better ways to establish DOW:

(1) for /f "tokens=2 delims==" %%A in ('wmic path win32_localtime get dayofweek /format:list') do set DOW=%%A
(2) for /f %%A in ('powershell "&{[int](Get-Date).DayOfWeek}"') do set DOW=%%A
{not sure if it this one works in XP}

In both cases DOW will be identified by a number (0=Sunday 1=Monday etc.).
UbertamSenior Technician

Author

Commented:
Thanks for the good ideas all around. After it running for over a week, I realized the folder creation is actually working, but the new folders are empty.  I forgot to update the ntbackup script with the new path on each of the new days.  DUH.  Just updated it and that should fix it.  Thanks, guys.