Friday, October 16, 2009

Daily Server Backup Script

'+++++daily backup script++++++

Dim sh, nw, fso
Set sh = WScript.CreateObject("WScript.Shell")
Set nw = WScript.CreateObject("WScript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")

CRLF = Chr(13) & Chr(10)

On Error Resume Next

'-------------------------------------------
'determine day of the week
'-------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")

For Each objItem in colItems
    varday = objItem.DayOfWeek
Next

Select case varday
    case 7
            dayname = "sunday"
    case 1
            dayname = "monday"
    case 2
            dayname = "tuesday"
    case 3
            dayname = "wednesday"
    case 4
            dayname = "thursday"
    case 5
            dayname = "friday"
    case 6
            dayname = "saturday"

End Select
'-------------------------------------------
'set variables
'-------------------------------------------
backupjob = "C:\scripts\backup-full-ss.bks" 'location of backup selections
'sample bks file
'C:\
'C:\Program Files\Exchsrvr\MDBDATA\ /Exclude
'D:\
'D:\backup\ /Exclude
'D:\ClientApps\ /Exclude
'D:\program files\exchsrvr\mdbdata\ /Exclude
'D:\Public\Wildland\Computers\removed e-mail attachments\ /Exclude
'D:\RECYCLER\ /Exclude
'D:\softlib\ /Exclude
'D:\WUTemp\ /Exclude
'JET SERVER\Microsoft Information Store\First Storage Group\
'SystemState


logtype = "s" 'f=full, s=summary, n=none
runusername = "administrator"
If dayname = "sunday" then
    baktype = "normal"
else
    baktype = "differential"
End If

'-------------------------------------------
'Run backup
'-------------------------------------------
sh.Run "C:\WINDOWS\system32\ntbackup.exe backup " & chr(34) & "@" & backupjob & chr(34) & " /n " & chr(34) & "Backup created 10/15/2009 at 10:53 PM" & chr(34) & " /d " & chr(34) & "Set created 10/15/2009 at 10:53 PM" & chr(34) & " /v:yes /r:no /rs:no /hc:off /m " & baktype & " /j " & chr(34) & "backup-full-ss" & chr(34) & " /l:s /f " & chr(34) & "F:\backup-" & baktype & "-" & dayname & ".bkf" & chr(34) & ""

'-------------------------------------------
'wait for backup to complete
'-------------------------------------------
iniproc = 1
Do While iniproc >= 1
    set svc=getobject("winmgmts:root\cimv2")
    sQuery="select * from win32_process where name='ntbackup.exe'"
    set cproc=svc.execquery(sQuery)
    iniproc=cproc.count
    wscript.sleep 5000
Loop
set cproc=nothing
set svc=nothing

'-------------------------------------------
'determine which log file is newest
'-------------------------------------------
baklogfolder = "C:\Documents and Settings\" & runusername & "\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"

newbaklogfolder = "C:\temp"
newbaklogfile = month(now)& day(now)& right(year(now),2) & "." & second(now) & "_" & baktype & ".log"

'101609.58_differential.log

Dim fNewest
set oFolder=createobject("scripting.filesystemobject").getfolder(baklogfolder)
For Each aFile In oFolder.Files
If lcase(RIGHT(aFile.Name,3)) = "log" then
    If fNewest = "" Then
        Set fNewest = aFile
    Else
    
        If fNewest.DateLastModified < aFile.DateLastModified Then
            Set fNewest = aFile
           End If

    End If
End If
Next

'-------------------------------------------
'do file copy
'-------------------------------------------
fso.CopyFile baklogfolder & "\" & fNewest.Name, newbaklogfolder & "\" & newbaklogfile


Wscript.quit

No comments: