Home Forums Programming SMO/RMO/DMO restoring a backupdevice on another machine RE: restoring a backupdevice on another machine

  • Solution:

    First check if the backupdevice exists on the machine (which it shouldn't, because the restore is taking place on a machine other than the one that the backup was done on.)

    oBackupDeviceCheck = CreateObject("sqldmo.BackupDevice")  

    For Each oBackupDeviceCheck In oServer.BackupDevices

        If oBackupDeviceCheck.Name = BackupFileName Then

            blnBackupDeviceExists = True

            Exit For

        End If

    Next

    If the backupdevice does not exist, create it...

      If Not blnBackupDeviceExists Then

      oBackupDevice = CreateObject("sqldmo.BackupDevice") 

         With oBackupDevice

            .Name = BackupFileName

            .Type = 2

            'add the physical location of the device (file device)

            .PhysicalLocation = "C:\Data\BackupFileName.zzz"

         End With

         oServer.BackupDevices.Add(oBackupDevice)

      End If

    Finally, create the SQLRestore object, and get the physical file name locations of the .mdb/.ldf files from the QueryResults object returned by the .ReadFileList method of the restore object:

    Try

      oRestoreDB = CreateObject("sqldmo.Restore")    'New sqldmo.Restore

      With oRestoreDB

         .devices() = strDeviceNameToRestore

         .database = DBName

         .medianame = BackupFileName

         .ReplaceDatabase = True

         QueryResults = .ReadFileList(oServer)

         strDBRestorePath = QueryResults.GetColumnString(1, 2)

         'create the directory tree...

         oFile = New clsFile

         If oFile.CreateDirectoryTree(strDBRestorePath) Then

           .sqlverify(oServer)

           .SQLRestore(oServer)

         Else

           Return False

           Exit Function

         End If

      End With

      SQLRestore = True

    Catch ex As Exception

       MsgBox("Error Restoring database: " & ex.Message)

       SQLRestore = False

    End Try

    Regards,

    Jax