Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Компьютеры » В помощь системному администратору » Автоматизация администрирования

Модерирует : lynx, Crash_Master, dg, emx, ShriEkeR

emx (13-12-2006 21:07): http://forum.ru-board.com/topic.cgi?forum=8&topic=19088#1  Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

   

rosalin



Silver Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору

'----------------------------------------------------------------------------------------
' -- some kind of (c) by tomcat --  
' This VB-script must be executed in "everyday mode" by some kind of Sheduler
' It runs backup program (nnbackup, see http://nncron.ru) that makes dumps from level 0 to level 3
' 0 - every quorter at the last day of quoter
' 1 - every month at the last day of month
' 2 - every week on Fridays
' 3 - every day
' Trick is: if some level's backup hadn't been executed at day it should be,
' script executes it at next start
' See consts for tuning:
' PathToExe and Arg_N concatenates together
' In RegSection script stores execution dates - don't touch them manually if unsure!
' Script writes log-file Scriptname.log
' Script must be in one folder with nnbackup.exe, else edit PathToExe and Arg_N
'----------------------------------------------------------------------------------------
Option Explicit
 
' command strings to execute
Const PathToExe = "nnbackup.exe"
Const Arg_0 = " -f cfg\d0.cfg"
Const Arg_1 = " -f cfg\d1.cfg"
Const Arg_2 = " -f cfg\d2.cfg"
Const Arg_3 = " -f cfg\d3.cfg"
 
 
Const RegSection = "HKCU\Software\Backup.VBS\" ' registry section where stores backup dates
Const LMarkName = "DateLevel_" ' template for adding digit of level for registry
 
Dim WSHL ' WshShell
Dim Processed ' boolean, signs level successfully processed
Const SW_SHOWMINNOACTIVE = 7
 
Set WSHL = Wscript.CreateObject("Wscript.Shell")
 
Dim fso
Dim LogFileStr, LogFile
Const ForAppending = 8
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
    ' get name of log-file - at same dir and same name with .vbs self
    LogFileStr = Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName) - 3) & "log"
    Set LogFile = fso.OpenTextFile(LogFileStr, ForAppending, True) ' creates it, if absent
 
ProcessLevel 0
If Not Processed Then ProcessLevel 1
If Not Processed Then ProcessLevel 2
If Not Processed Then ProcessLevel 3
If Not Processed Then
    LogMsg "Processing skipped all levels up to 3"
    Wscript.Quit 2
End If
 
    LogFile.Close
 
 
 
'++++++++++++++++++++++++++++++++ Functions +++++++++++++++++++++++++++++++++
 
Function ReadRegistry(RegPath)
On Error Resume Next
    ReadRegistry = WSHL.RegRead(RegPath)
    Err.Clear
End Function
 
Sub ProcessLevel(LevelNumber)
Dim LMark ' LMarkName with digit of level : DateLevel_0, for ex.
Dim LevelDate, LastPeriodDate
Dim Sign
 
Processed = False
LMark = LMarkName & LevelNumber
LevelDate = ReadRegistry(RegSection & LMark)
' level record found
If LevelDate <> "" Then
    If IsDate(LevelDate) Then
       Select Case LevelNumber
       Case 0
            Sign = "q" ' quarter
            LastPeriodDate = LastDayOfQuarter(Date)
       Case 1
            Sign = "m" ' month
            LastPeriodDate = LastDayOfMonth(Date)
       Case 2
            Sign = "ww" ' week
            LastPeriodDate = LastWorkdayOfWeek(Date)
       Case 3
            Sign = "d" ' day
       End Select
       ' if now is last day of period or
       If (Date = LastPeriodDate) And (Abs(DateDiff(Sign, LevelDate, Date, vbMonday)) = 1) _
        Or (Abs(DateDiff(Sign, LevelDate, Date, vbSaturday)) > 1) Then
         ExecuteBackup LevelNumber
         Processed = True
       End If
    Else
     ' raise error and quit
     LogMsg "Error: In '" & RegSection & "': date record for " & LMark & " seems invalid."
     Wscript.Quit 1
    End If
     
Else
' level record not found
       ExecuteBackup LevelNumber
       Processed = True
End If
 
End Sub
 
'*************************** Functions for ExecuteBackup()**********************
Function LastDayOfQuarter(ADate)
' returns date representing the last day of quarter for which given ADate belongs
Dim sDate
' give the first day of the last month of given date' quarter
sDate = DateSerial(Year(ADate), 3 * DatePart("q", ADate, vbUseSystemDayOfWeek, vbUseSystem), 1)
' give the first day of the next month
sDate = DateAdd("m", 1, sDate)
' subtract one day backward
LastDayOfQuarter = DateAdd("d", -1, sDate)
End Function
 
Function LastDayOfMonth(ADate)
Dim sDate
' returns date representing the last day of month of given ADate
' give the first day of the next month
sDate = DateAdd("m", 1, DateSerial(Year(ADate), Month(ADate), 1))
' subtract one day backward
LastDayOfMonth = DateAdd("d", -1, sDate)
End Function
 
Function LastWorkdayOfWeek(ADate)
' returns date representing Friday at week of given ADate
Const vbFriday = 5 ' in Russia
LastWorkdayOfWeek = DateAdd("d", vbFriday - Weekday(ADate, vbUseSystemDayOfWeek), ADate)
End Function
'**************************************************************************
 
Function ExecuteBackup(LevelNumber)
Dim ExecStr, ExecExitCode
Dim CurDate, PrevDate, RecDate
 
If Not fso.FileExists(PathToExe) Then
    LogMsg "Abort: external not found: '" & PathToExe & "'"
    Wscript.Quit 3
End If
 
Select Case LevelNumber
Case 0 ' Quarter
    ExecStr = PathToExe & Arg_0
    CurDate = LastDayOfQuarter(Date)
    PrevDate = LastDayOfQuarter(DateAdd("q", -1, Date))
Case 1 ' Month
    ExecStr = PathToExe & Arg_1
    CurDate = LastDayOfMonth(Date)
    PrevDate = LastDayOfMonth(DateAdd("m", -1, Date))
Case 2 ' Week
    ExecStr = PathToExe & Arg_2
    CurDate = LastWorkdayOfWeek(Date)
    PrevDate = LastWorkdayOfWeek(DateAdd("w", -1, Date))
    ExecuteAdditionalWeekBackup
Case 3 ' Day
    ExecStr = PathToExe & Arg_3
End Select
     
    LogMsg "Execute external: '" & ExecStr & "'"
    ExecExitCode = WSHL.Run(ExecStr, SW_SHOWMINNOACTIVE, True)
     
    If ExecExitCode = 0 Then
     If LevelNumber = 3 Then 'if day backup then not need in all this date tricks
        WSHL.RegWrite RegSection & LMarkName & LevelNumber, Date
        LogMsg "Level '" & LevelNumber & "' proceed successfully. Record basepoint date: " & Date
     Else
        ' real processing date
        WSHL.RegWrite RegSection & "Real_" & LMarkName & LevelNumber, Date
        ' basepoint (referenced) date
        If Abs(DateDiff("d", CurDate, Date, vbUseSystemDayOfWeek, vbUseSystem)) < DateDiff("d", PrevDate, Date, vbUseSystemDayOfWeek, vbUseSystem) Then
            RecDate = CurDate
        Else
            RecDate = PrevDate
        End If
        WSHL.RegWrite RegSection & LMarkName & LevelNumber, RecDate
        LogMsg "Level '" & LevelNumber & "' proceed successfully. Record basepoint date: " & RecDate
     End If
    Else
        LogMsg "Error: external returns non-zero exit code: " & ExecExitCode
    End If
     
End Function
 
Sub ExecuteAdditionalWeekBackup()
Dim ExecStr, ExecExitCode
' if need some additional week backups, for ex. - email
ExecStr = PathToExe & " -f cfg\bat.cfg"
ExecExitCode = WSHL.Run(ExecStr, SW_SHOWMINNOACTIVE, True)
If ExecExitCode = 0 Then
    LogMsg "Additional week backup successfully completed"
Else
    LogMsg "Error: Additional week backup returns non-zero exit code: " & ExecExitCode
End If
End Sub
 
Sub LogMsg(Msg)
   LogFile.WriteLine Now & " " & Msg
End Sub
 

Всего записей: 2588 | Зарегистр. 15-04-2003 | Отправлено: 09:21 09-03-2006
   

На первую страницук этому сообщениюк последнему сообщению

Компьютерный форум Ru.Board » Компьютеры » В помощь системному администратору » Автоматизация администрирования
emx (13-12-2006 21:07): http://forum.ru-board.com/topic.cgi?forum=8&topic=19088#1


Реклама на форуме Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Рейтинг.ru