Jump to content

backup script: find day of week in cmd


midiboy

Recommended Posts

Hi everyone.

I am trying to develop a backup script using the command line. I am thinking of something like this:

Script should check the day of the week, then create (if not already exist) a folder with that variable and then start ntbackup with the bkf file set in that folder. That way I only have one script but it runs Mo-Sa and drops bkf files into separate folders. (which overwrites the one inside).

Only problem is ... how do I get the day of the week into a variable ? The %date% var. does not seem to help here.

any ideas ?

Ah ... and as a bonus it would be nice if the script could email the backup logs afterwards. Any small comandline smtp program available somewhere ?

Thanks for your help !

Alex

Link to comment
Share on other sites


This is a batch file which will give you the day of the week, however it needs to use a vbscript to do it.

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS
SET "TM_=%TEMP%\_TMP$.VBS"
>%TM_% ECHO/WSCRIPT.ECHO WEEKDAYNAME(WEEKDAY(DATE),TRUE)
FOR /F %%? IN ('CSCRIPT //NOLOGO %TM_%') DO (SET "WD_=%%?" &&(DEL %TM_%))
ECHO/Weekday is %WD_%
ENDLOCAL &GOTO :EOF

I would also suggest that your entire project is a vbscript one too.

Link to comment
Share on other sites

Hi again,

well, I have already based a small backupscript upon your idea. Maybe it helps someone else too. I also found a small freeware smtp send app here which I am using inside the script to email the logfile-

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS

REM define variables:

REM variable path eg: D: or D:\Backup (without "\" at the end!)

set DL=D:\Backup

REM DELETE OLD BACKUP LOGS ( Only works on a german system. On an english system change "Anwendungsdaten" into "program data" (I think)

if exist "%TMP%\..\Anwendungsdaten\Microsoft\Windows NT\NTBackup\data\*.log" del "%TMP%\..\Anwendungsdaten\Microsoft\Windows NT\NTBackup\data\*.log"

REM FIND weekday

SET "TM_=%TEMP%\_TMP$.VBS"
>%TM_% ECHO/WSCRIPT.ECHO WEEKDAYNAME(WEEKDAY(DATE),TRUE)
FOR /F %%? IN ('CSCRIPT //NOLOGO %TM_%') DO (SET "WD_=%%?" &&(DEL %TM_%))

REM CREATE FOLDERS

if not exist "%DL%\%WD_%" md "%DL%\%WD_%"

REM START NTBACKUP (BKS SELECTION FILE HAS TO BE CREATED BEFORE !)

"%WINDIR%\System32\NTBACKUP.EXE" backup "@C:\Unbenannt.bks" /d "Description" /v:yes /r:no /rs:no /hc:off /m normal /l:s /f "%DL%\%WD_%\Sicherung_Test_%WD_%.bkf"

REM MOVE LOGFILE TO BACKUPLOCATION ( Only works on a german system. On an english system change "Anwendungsdaten" into "program data" (I think)

move /Y "%TMP%\..\Anwendungsdaten\Microsoft\Windows NT\NTBackup\data\*.log" "%DL%\%WD_%\Sicherung_Test_%WD_%.bkf.log"

REM MAIL THE LOGFILE USING BMAIL

bmail.exe -s mailserveradress -t toemailadress -f fromemailadress -a "Test Backup Results" -m "%DL%\%WD_%\Sicherung_Test_%WD_%.bkf.log" -c



REM REPEAT THE ABOVE STEPS FOR A SECOND BACKUPJOB


"%WINDIR%\System32\NTBACKUP.EXE" backup systemstate /d "Systemstate" /v:yes /r:no /rs:no /hc:off /m normal /l:s /f "%DL%\%WD_%\Systemstate_%WD_%.bkf"

move /Y "%TMP%\..\Anwendungsdaten\Microsoft\Windows NT\NTBackup\data\*.log" "%DL%\%WD_%\Systemstate_%WD_%.bkf.log"

bmail.exe -s mailserveradress -t toemailadress -f fromemailadress -a "Test Backup Results" -m "%DL%\%WD_%\Systemstate_%WD_%.bkf.log" -c



ENDLOCAL &GOTO :EOF

It works here. But maybe it could be refined. For instance it would be nicer if all the backupjobs including the moving of the backuplog and the mailing could run in some loop with variables for the bks, bkf, logfilenames and backup description. However, I am not very good at loops (yet) :-)

I am not good at scripting at all :whistle:

Bye,

Alex

Edited by midiboy
Link to comment
Share on other sites

If you didn't want to use vbscript you can get the day with

FOR /F "TOKENS=1 DELIMS=/ " %%A IN ('DATE /T') DO (SET day=%%A)

edit: just noticed this is the xp forum, seem to remember date output changed since w2k, so this may not work

Edited by uid0
Link to comment
Share on other sites

Here is a VBS scripts to get the day name

Dim Act	 : Set Act = CreateObject("Wscript.Shell")
Dim LT, Msg : LT = "day to run the script"
'------------------------------------------------------------------------------------------------------------>
'/-> vbSunday = 1, vbMonday = 2, vbTuesday = 3, vbWednesday = 4, vbThursday = 5, vbFriday = 6, vbSaturday = 7
If Weekday(Date) = vbMonday Then '/-> Change To Suit Your Needs, You can Use The Number Values To Check
Msg = "This is the correct " & LT & Space(5) &_
vbcrlf & Space(3) & "Number Code " & vbTab & Weekday(Date) &_
vbCrLf & Space(3) & "Short Name " & vbTab & WeekdayName(Weekday(Date),True) &_
vbCrLf & Space(3) & "Long Name " & vbTab & WeekdayName(Weekday(Date))
Act.Popup Space(9) & "Confirm Correct Day" & vbCrLf & Msg , 5, "Return Values", 0 + 48 + 4096
Else
Msg = "Today, " & WeekdayName(Weekday(Date)) & " is not the " & LT
Act.Popup Msg, 5, "Wrong Day", 0 + 48 + 4096
End If

Edited by gunsmokingman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...