'==============================================================================
'
' Microsoft SharePoint Portal Server Backup/Restore Utility
' Copyright (C) 2000 Microsoft Corp.  All Rights Reserved.
'
'==============================================================================

On Error Resume Next

'------------------------------------------------------------------------------
' Localized Strings
'------------------------------------------------------------------------------
Const L_Usage1_Text  = "Usage is: "
Const L_Usage2_Text  = "MSDMBack < /b | /r > <Path> [Password] [/o] [/m MachineName]"
Const L_Usage3_Text  = "MSDMBack /a <Domain\User> <Password>"
Const L_Usage4_Text  = "Requests server backup"
Const L_Usage5_Text  = "Requests server restore"
Const L_Usage6_Text  = "Target path for backup, or source path for restore"
Const L_Usage7_Text  = "Path"
Const L_Usage8_Text  = "Password"
Const L_Usage9_Text  = "Optional, password to encrypt content source passwords"
Const L_Usage10_Text = "Optional, restores catalogs to their original paths"
Const L_Usage11_Text = "Set and save backup/restore user account"
Const L_Usage12_Text = "Optional, backup/restore workspaces for remote machine"
Const L_Usage13_Text = "MachineName"
Const L_Usage14_Text = "Name of machine containing workspaces"

Const L_Banner1_Text = "Microsoft SharePoint Portal Server Backup/Restore Utility"
Const L_Banner2_Text = "Copyright (C) 2000 Microsoft Corp.  All Rights Reserved."

Const L_BackupCaption_Text      = "SharePoint Portal Server Backup Progress"
Const L_RestoreCaption_Text     = "SharePoint Portal Server Restore Progress"
Const L_BytesTransferred_Text   = "Bytes Transferred:"
Const L_Abort_Text              = "Abort"

Const L_CreateObjectFailed_Text = "Failed to create COM wrapper object. Contact Microsoft product support."
Const L_BackupError_Text        = "An error occurred during backup.  See the event log for more details."
Const L_RestoreError_Text       = "An error occurred during restore.  See the event log for more details."
Const L_SetAccountError_Text    = "An error occurred while setting the backup/restore user account."
Const L_MissingMachineName_Text = "Expected machine name"
Const L_InvalidParameter_Text   = "Invalid command line parameter"
Const L_NoBackup_Text           = "A backup or restore operation was not specified"
Const L_BothSpecified_Text      = "Both backup and restore were specified"
Const L_MissingPath_Text        = "Expected path for the backup/restore"
Const L_MissingAccount_Text     = "Missing user account"

Const L_Error1_Text = "Error:  "
Const L_Error2_Text = "ErrNo:  "
Const L_Error3_Text = "Desc:   "
Const L_Error4_Text = "Source: "

'--- add by PRL
Const L_ERROR_CREATE_CDNOTS   = "Unable to create CDONTS.NEWMAIL"
Const R_Email_Address  = "TO@MYADDRESS.com"
Const R2_Email_Address = "TO@MYADDRESS.com, TO.MYPAGER@MYADDRESS.com"
Const L_Email_Address  = "FROM.SHAREPOINT.ADMIN@MYADDRESS.com"


'------------------------------------------------------------------------------
' Constants
'------------------------------------------------------------------------------
Dim objEMAIL
Set objEMAIL = WScript.CreateObject("CDONTS.NEWMAIL")
ReportErrorNOEMAIL (L_ERROR_CREATE_CDNOTS)



' Usage string
Dim strUsage
strUsage =  L_Usage1_Text & vbCRLF &_
            vbTab & L_Usage2_Text & vbCRLF &_
            vbTab & L_Usage3_Text & vbCRLF &_
            vbCRLF &_
            vbTab & "/b" & vbTab & L_Usage4_Text & vbCRLF &_
            vbTab & "/r" & vbTab & L_Usage5_Text & vbCRLF &_
            vbTab & L_Usage7_Text & vbTab & L_Usage6_Text & vbCRLF &_
            vbTab & L_Usage8_Text & vbTab & L_Usage9_Text & vbCRLF &_
            vbTab & "/o" & vbTab & L_Usage10_Text & vbCRLF &_
            vbTab & "/a" & vbTab & L_Usage11_Text & vbCRLF &_
            vbTab & "/m" & vbTab & L_Usage12_Text & vbCRLF &_
            vbTab & L_Usage13_Text & vbTab & L_Usage14_Text

' Banner
Dim strBanner
strBanner = L_Banner1_Text & vbCRLF & L_Banner2_Text & vbCRLF

' Usage error code
Const lUsageError           = &h80070057

'------------------------------------------------------------------------------
' Entry
'------------------------------------------------------------------------------

'WScript.Echo strBanner

' Create the backup/restore COM wrapper
Dim objMSDMBackupWrap
Set objMSDMBackupWrap = CreateObject ("MSDMBack.MSDMBackupWrap")
ReportError (L_CreateObjectFailed_Text)

' Read command line
Dim objArgs
Set objArgs = WScript.Arguments

Dim fBackupMode
Dim strBackupPath
Dim strPassword
Dim strMachineName
Dim fOriginalPaths
Dim paramPos
Dim cChar
Dim strParam
Dim fBR

' Parse the command line arguments

fOriginalPaths = False
strMachineName = "localhost"
strPassword = ""
paramPos = 0
fBR = False

Do While paramPos < objArgs.Count

    strParam = ucase(objArgs(paramPos))
    Select Case strParam

        ' Backup: Expect a directory and an optional password
        ' Restore: Expect a directory and an optional password
        Case "/B", "-B", "/R", "-R"

            ' Allow ONLY backup or restore
            If ( fBR ) Then
                Usage(L_BothSpecified_Text)
            End If

            fBR = True

            if ("/B" = strParam) OR ("-B" = strParam) Then
                fBackupMode = True
            Else
                fBackupMode = False
            End If

            paramPos = paramPos + 1

            ' The next paramater must be the path, not a '/' paramater
            Call CheckParamPos(L_MissingPath_Text)
            cChar = left(objArgs(paramPos), 1)
            If ("/" = cChar) OR ("-" = cChar) Then
                Usage(L_MissingPath_Text)
            Else
                strBackupPath = objArgs(paramPos)
                paramPos = paramPos + 1

                ' Check for the OPTIONAL password parameter
                If paramPos < objArgs.Count Then
                    cChar = left(objArgs(paramPos), 1)
                    If ("/" <> cChar) AND ("-" <> cChar) Then
                        strPassword = objArgs(paramPos)
                       paramPos = paramPos + 1
                    End If
                End If
            End If

        ' Original paths: no associated parameters
        Case "/O", "-O"
            fOriginalPaths = True
            paramPos = paramPos + 1

        ' Machine name: backup/restore the remote machine named
        Case "/M", "-M"
            paramPos = paramPos + 1

            ' Check for the existence of a machine name
            Call CheckParamPos(L_MissingMachineName_Text)

            cChar = left(objArgs(paramPos), 1)
            If ("/" = cChar) OR ("-" = cChar) Then
                Usage(L_MissingMachineName_Text)
            Else
                strMachineName = objArgs(paramPos)
                paramPos = paramPos + 1
            End If

        ' Account: set backup/restore user account
        Case "/A", "-A"
            paramPos = paramPos + 1

            ' Check for a ID & password following the /A parameter
            If (paramPos+1) >= objArgs.Count Then
                Usage(L_MissingAccount_Text)
            Else
                objMSDMBackupWrap.SetBackupAccount objArgs(paramPos), objArgs(paramPos+1)
                ReportError (L_SetAccountError_Text)
                WScript.Quit 0
            End If

        ' Everything else
        Case Else
            Usage(L_InvalidParameter_Text)

    End Select
Loop

' We must have at LEAST a backup/restore path.  If this was not specified,
' then we didn't get enough parameters.
If Not fBR Then
    Usage(L_NoBackup_Text)
End If

If fBackupMode Then
    objMSDMBackupWrap.Backup strBackupPath, strPassword, strMachineName, L_BackupCaption_Text, L_Abort_Text, L_BytesTransferred_Text
    ReportError (L_BackupError_Text)
Else
    objMSDMBackupWrap.Restore strBackupPath, strPassword, fOriginalPaths, strMachineName, L_RestoreCaption_Text, L_Abort_Text, L_BytesTransferred_Text
    ReportError (L_RestoreError_Text)
End If


ReportSuccess

set objemail = nothing


WScript.Quit 0

'------------------------------------------------------------------------------
' ReportError
'------------------------------------------------------------------------------
Sub ReportError (strMsg)
   dim strFullMsg
   If Err.Number <> 0 Then

   strFullMsg = "SharePoint Backup Image Creation " & L_Error1_Text & strMsg & vbCRLF &_
                     L_Error2_Text & Hex(Err.Number)   & vbCRLF &_
                     L_Error3_Text & Err.Description   & vbCRLF &_
                     L_Error4_Text & Err.Source

 objEmail.send L_Email_Address, R2_Email_Address,strFullMsg,strFullMsg

 set objemail = nothing

        WScript.Quit Err.Number
    End If
End Sub

Sub ReportSuccess
   dim strFullMsg

   strFullMsg = "SharePoint - Successful Backup Image Creation" & strMsg & vbCRLF &_
                     L_Error2_Text & Hex(Err.Number)   & vbCRLF &_
                     L_Error3_Text & Err.Description   & vbCRLF &_
                     L_Error4_Text & Err.Source

   objEmail.send L_Email_Address, R_Email_Address,strFullMsg,strFullMsg


End Sub


Sub ReportErrorNOEMAIL (strMsg)
    If Err.Number <> 0 Then
        WScript.Echo L_Error1_Text & strMsg            & vbCRLF &_
                     L_Error2_Text & Hex(Err.Number)   & vbCRLF &_
                     L_Error3_Text & Err.Description   & vbCRLF &_
                     L_Error4_Text & Err.Source
        WScript.Quit Err.Number
    End If
End Sub

'------------------------------------------------------------------------------
' Usage
'------------------------------------------------------------------------------
Sub Usage (strErrorMsg)
   dim strFullMsg
   strFullMsg = strErrorMsg & vbCRLF  & _
                strUsage

   objEmail.send L_Email_Address, L_Email_Address,strFullMsg,1

   set objemail = nothing

    WScript.Quit lUsageError
End Sub

'------------------------------------------------------------------------------
' CheckParamPos
'------------------------------------------------------------------------------
Sub CheckParamPos (strErrorMsg)
    If paramPos >= objArgs.Count Then
        Usage(strErrorMsg)
    End If
End Sub

