Dela via


Restore Class

The Restore object provides programmatic access to restore operations.

Inheritance Hierarchy

System.Object
  Microsoft.SqlServer.Management.Smo.BackupRestoreBase
    Microsoft.SqlServer.Management.Smo.Restore

Namespace:  Microsoft.SqlServer.Management.Smo
Assembly:  Microsoft.SqlServer.SmoExtended (in Microsoft.SqlServer.SmoExtended.dll)

Syntax

'Declaration
Public NotInheritable Class Restore _
    Inherits BackupRestoreBase
'Usage
Dim instance As Restore
public sealed class Restore : BackupRestoreBase
public ref class Restore sealed : public BackupRestoreBase
[<SealedAttribute>]
type Restore =  
    class 
        inherit BackupRestoreBase 
    end
public final class Restore extends BackupRestoreBase

The Restore type exposes the following members.

Constructors

  Name Description
Public method Restore() Initializes a new instance of the Restore class.
Public method Restore(String, BackupSet) Initializes a new instance of the Restore class with specified destination database name and backup set.

Top

Properties

  Name Description
Public property Action Gets or sets the type of restore to be performed.
Public property AsyncStatus Gets the asynchronous status of a backup or restore process. (Inherited from BackupRestoreBase.)
Public property BlockSize Gets or sets the physical block size of the backup or restore, in bytes. (Inherited from BackupRestoreBase.)
Public property BufferCount Gets or sets the total number of I/O buffers to be used for the backup or restore operation. (Inherited from BackupRestoreBase.)
Public property Checksum Gets or sets a Boolean property value that determines whether a checksum value is calculated during backup or restore operations. (Inherited from BackupRestoreBase.)
Public property ClearSuspectPageTableAfterRestore Gets or sets a Boolean value that determines whether entries in the suspect page table will be deleted following the restore operation.
Public property ContinueAfterError Gets or sets a Boolean property value that determines whether the backup or restore continues after a checksum error occurs. (Inherited from BackupRestoreBase.)
Public property Database Gets or sets the database on which the backup or restore operation runs. (Inherited from BackupRestoreBase.)
Public property DatabaseFileGroups Gets the SQL Server file groups targeted by the backup or restore operation. (Inherited from BackupRestoreBase.)
Public property DatabaseFiles Gets the operating system files targeted by the backup or restore operation. (Inherited from BackupRestoreBase.)
Public property DatabasePages Gets the list of the restore pages.
Public property Devices Gets the backup devices specified for the backup or restore operation. (Inherited from BackupRestoreBase.)
Public property FileNumber Gets or sets the index number used to identify the targeted backup set on the backup medium.
Public property KeepReplication Gets or sets a Boolean property value that specifies whether to maintain a replication configuration during a restore operation.
Public property MaxTransferSize Gets or sets the maximum number of bytes to be transferred between the backup media and the instance of SQL Server. (Inherited from BackupRestoreBase.)
Public property MediaName Gets or sets the name used to identify a particular media set. (Inherited from BackupRestoreBase.)
Public property NoRecovery Gets or sets a Boolean property value that determines whether the tail of the log is backed up and whether the database is restored into the Restoring state. (Inherited from BackupRestoreBase.)
Public property NoRewind Gets or sets a Boolean property value that determines whether Microsoft SQL Server will keep the tape open after the backup or restore operation. (Inherited from BackupRestoreBase.)
Public property Offset Gets or sets the array of page addresses to be restored.
Public property Partial Gets or sets a Boolean property value that specifies whether a partial or full restore operation will be performed.
Public property PercentCompleteNotification Gets or sets the percentage interval for PercentCompleteEventHandler event handler calls. (Inherited from BackupRestoreBase.)
Public property RelocateFiles Gets the array of database logical file names and operating system physical file names used to redirect database storage when a database is restored to a new physical location.
Public property ReplaceDatabase Gets or sets a Boolean value that specifies whether a restore operation creates a new image of the restored database.
Public property Restart Gets or sets a Boolean property value that determines whether the backup or restore operation will restart if it is interrupted. (Inherited from BackupRestoreBase.)
Public property RestrictedUser Gets or sets a Boolean property value that specifies whether access to the restored database is restricted to members of the db_owner fixed database role and the dbcreator and sysadmin fixed server roles.
Public property StandbyFile Gets or sets the name of an undo file that is used as part of the imaging strategy for an instance of SQL Server.
Public property StopAtMarkAfterDate Gets or sets the date to be used in conjunction with the mark name specified by StopAtMarkName to determine the stopping point of the recovery operation.
Public property StopAtMarkName Gets or sets the mark name to be used in conjunction with the date specified by StopAtMarkAfterDate to determine the stopping point of the recovery operation.
Public property StopBeforeMarkAfterDate Gets or sets the date to be used in conjunction with the mark name specified by StopBeforeMarkName to determine the stopping point of the recovery operation.
Public property StopBeforeMarkName Gets or sets the mark name to be used in conjunction with the date specified by StopBeforeMarkAfterDate to determine the stopping point of the recovery operation.
Public property ToPointInTime Gets or sets the endpoint for database log restoration.
Public property UnloadTapeAfter Gets or sets a Boolean property value that determines whether tape media are rewound and unloaded after the backup or restore operation completes. (Inherited from BackupRestoreBase.)

Top

Methods

  Name Description
Public method Abort Terminates a running backup or restore operation. (Inherited from BackupRestoreBase.)
Public method ClearSuspectPageTable Deletes entries in the suspect page table.
Public method Equals (Inherited from Object.)
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Public method ReadBackupHeader Reads the backup header and returns information about the media contents.
Public method ReadFileList Returns the list of database files on the targeted media.
Public method ReadMediaHeader Reads the media header and returns the media header values.
Public method ReadSuspectPageTable Returns the suspect page table for the targeted instance of SQL Server.
Public method Script Generates a Transact-SQL script that can be used to re-create the restore operation.
Public method SetMediaPassword(SecureString) Obsolete. Sets the password for a media set. (Inherited from BackupRestoreBase.)
Public method SetMediaPassword(String) Obsolete. Sets the password for a media set. (Inherited from BackupRestoreBase.)
Public method SetPassword(SecureString) Sets the password for a backup set. (Inherited from BackupRestoreBase.)
Public method SetPassword(String) Sets the password for a backup set. (Inherited from BackupRestoreBase.)
Public method SqlRestore Performs a restore operation on the specified server.
Public method SqlRestoreAsync Performs a restore operation asynchronously.
Public method SqlVerify(Server) Checks the media on the instance of SQL Server that is represented by the specified Server object.
Public method SqlVerify(Server, Boolean) Checks the media on instance of SQL Server that is represented by the specified Server object and loads backup history information into the history tables.
Public method SqlVerify(Server, String%) Checks the media on the instance of SQL Server that is represented by the specified Server object and returns an error message.
Public method SqlVerify(Server, Boolean, String%) Checks the media on the instance of SQL Server that is represented by the specified Server object, loads backup history information into the history tables, and returns an error message.
Public method SqlVerifyAsync(Server) Checks the media on the instance of SQL Server that is represented by the specified Server object asynchronously.
Public method SqlVerifyAsync(Server, Boolean) Checks the media on the instance of SQL Server that is represented by the specified Server object asynchronously and loads backup history information into the history tables.
Public method SqlVerifyLatest(Server) Verifies the most recent backup but does not restore it, and checks to see that the backup set is complete and the entire backup is readable.
Public method SqlVerifyLatest(Server, SqlVerifyAction) Verifies the most recent backup with the specified action, and checks to see that the backup set is complete and the entire backup is readable.
Public method SqlVerifyLatest(Server, String%) Verifies the most recent backup but does not restore it, checks to see that the backup set is complete and the entire backup is readable, and returns an error message.
Public method SqlVerifyLatest(Server, SqlVerifyAction, String%) Verifies the most recent backup with the specified action, checks to see that the backup set is complete and the entire backup is readable, and returns an error message.
Public method ToString (Inherited from Object.)
Public method Wait Waits for the current backup or restore operation to complete before moving on to the next process. (Inherited from BackupRestoreBase.)

Top

Events

  Name Description
Public event Complete Occurs when the backup or restore operation completes. (Inherited from BackupRestoreBase.)
Public event Information Occurs when any informational message is issued by Microsoft SQL Server that cannot be handled by the Complete NextMedia or PercentComplete events. (Inherited from BackupRestoreBase.)
Public event NextMedia Occurs when the backup or restore operation exhausts the medium in a target device. (Inherited from BackupRestoreBase.)
Public event PercentComplete Occurs when a backup or restore operation reaches a completion unit. (Inherited from BackupRestoreBase.)

Top

Fields

  Name Description
Public field VerifyComplete Specifies the VerifyCompleteEventHandler.

Top

Remarks

By using a Restore object you can do the following:

  • Restore all or part of a database.

  • Restore backup images of transaction log records.

  • Verify the integrity of backup media.

  • Report the contents of backup media.

  • Monitor a restore operation, reporting status to the user.

SQL Server can write a backup to one of four media types: disk, tape, named pipe, or a backup device. SQL Server supports backup striping. A striped backup is one directed to more than a single device. Striping is supported to a single media type only. That is, a backup can be written to two tape devices. Half of a backup cannot be written to a tape device and the other half to a disk.

At a minimum, supply values for a restore source when using a Restore object. Use one media type property to specify the restore operation source.

Setting other properties in the Restore object may be required by the restore operation desired.

Thread Safety

Any public static (Shared in Microsoft Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

To get Restore object properties, users can be a member of the public fixed server role.

To set Restore object properties, users must have CREATE DATABASE permission on the server, or be a member of sysadmin or dbcreator fixed server roles, or be a member of db_owner fixed database role.

To perform a restore operation, users must have CREATE DATABASE permission on the server, or be a member of sysadmin or dbcreator fixed server roles. If the database already exists, then members of the db_owner fixed database role can also perform a restore operation.

Examples

The following code example demonstrates how to create a normal and an asynchronous backup of the AdventureWorks2012  database and use each of those backups to restore it.

VB

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'Store the current recovery model in a variable.
Dim recoverymod As Integer
recoverymod = db.DatabaseOptions.RecoveryModel
'Define a Backup object variable. 
Dim bk As New Backup
'Specify the type of backup, the description, the name, and the database to be backed up.
bk.Action = BackupActionType.Database
bk.BackupSetDescription = "Full backup of AdventureWorks2012"
bk.BackupSetName = "AdventureWorks2012 Backup"
bk.Database = "AdventureWorks2012"
'Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file.
Dim bdi As BackupDeviceItem
bdi = New BackupDeviceItem("Test_Full_Backup1", DeviceType.File)
'Add the device to the Backup object.
bk.Devices.Add(bdi)
'Set the Incremental property to False to specify that this is a full database backup.
bk.Incremental = False
'Set the expiration date.
Dim backupdate As New Date
backupdate = New Date(2006, 10, 5)
bk.ExpirationDate = backupdate
'Specify that the log must be truncated after the backup is complete.
bk.LogTruncation = BackupTruncateLogType.Truncate
'Run SqlBackup to perform the full database backup on the instance of SQL Server.
bk.SqlBackup(srv)
'Inform the user that the backup has been completed.
Console.WriteLine("Full Backup complete.")
'Remove the backup device from the Backup object.
bk.Devices.Remove(bdi)
'Make a change to the database: in this case, add a table called test_table.
Dim t As Table
t = New Table(db, "test_table")
Dim c As Column
c = New Column(t, "col", DataType.Int)
t.Columns.Add(c)
t.Create()
'Create another file device for the differential backup and add the Backup object.
Dim bdid As BackupDeviceItem
bdid = New BackupDeviceItem("Test_Differential_Backup1", DeviceType.File)
'Add the device to the Backup object.
bk.Devices.Add(bdid)
'Set the Incremental property to True for a differential backup.
bk.Incremental = True
'Run SqlBackup to perform the incremental database backup on the instance of SQL Server.
bk.SqlBackup(srv)
'Inform the user that the differential backup is complete.
Console.WriteLine("Differential Backup complete.")
'Remove the device from the Backup object.
bk.Devices.Remove(bdid)
'Delete the AdventureWorks2012 database before restoring it.
srv.Databases("AdventureWorks2012").Drop()
'Define a Restore object variable.
Dim rs As Restore
rs = New Restore
'Set the NoRecovery property to true, so the transactions are not recovered.
rs.NoRecovery = True
'Add the device that contains the full database backup to the Restore object.
rs.Devices.Add(bdi)
'Specify the database name.
rs.Database = "AdventureWorks2012"
'Restore the full database backup with no recovery.
rs.SqlRestore(srv)
'Inform the user that the Full Database Restore is complete.
Console.WriteLine("Full Database Restore complete.")
'Remove the device from the Restore object.
rs.Devices.Remove(bdi)
'Set te NoRecovery property to False.
rs.NoRecovery = False
'Add the device that contains the differential backup to the Restore object.
rs.Devices.Add(bdid)
'Restore the differential database backup with recovery.
rs.SqlRestore(srv)
'Inform the user that the differential database restore is complete.
Console.WriteLine("Differential Database Restore complete.")
'Remove the device.
rs.Devices.Remove(bdid)
'Set the database recovery mode back to its original value.
srv.Databases("AdventureWorks2012").DatabaseOptions.RecoveryModel = recoverymod
'Drop the table that was added.
srv.Databases("AdventureWorks2012").Tables("test_table").Drop()
srv.Databases("AdventureWorks2012").Alter()
'Remove the backup files from the hard disk.
My.Computer.FileSystem.DeleteFile("C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Test_Full_Backup1")
My.Computer.FileSystem.DeleteFile("C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Test_Differential_Backup1")

Powershell

$srv = new-object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("AdventureWorks2012")
$rs = new-object Microsoft.SqlServer.Management.Smo.Restore
$rs.NoRecovery = $TRUE
$rs.Devices.Add($bdi)
$recoverymod = $db.DatabaseOptions.RecoveryModel
$bk = new-object Microsoft.SqlServer.Management.Smo.Backup
$bk.Action = [Microsoft.SqlServer.Management.Smo.BackupActionType]::Database
$bk.BackupSetDescription = "Full backup of AdventureWorks2012"
$bk.BackupSetName = "AdventureWorks2012 Backup"
$bk.Database = "AdventureWorks2012"
$bdi = new-object Microsoft.SqlServer.management.Smo.BackupDeviceItem("Test_Full_Backup1", [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
$bk.Devices.Add($bdi)
$bk.Incremental = $FALSE
$backupdate = new-object System.DateTime(2009, 3, 2)
$bk.ExpirationDate = $backupdate
$bk.LogTruncation = [Microsoft.SqlServer.Management.Smo.BackupTruncateLogType]::Truncate
$bk.SqlBackup($srv)
Write-Host "Full Backup complete."
$bk.Devices.Remove($bdi)
$t = new-object Microsoft.SqlServer.Management.Smo.Table($db, "test_table")
$c = new-object Microsoft.SqlServer.Management.Smo.Column($t, "col", [Microsoft.SqlServer.Management.Smo.DataType]::Int)
$t.Columns.Add($c)
$t.Create()
$bdid = new-object Microsoft.SqlServer.Management.Smo.BackupDeviceItem("Test_Differential_Backup1", [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
$bk.Devices.Add($bdid)
$bk.Incremental = $TRUE
$bk.SqlBackup($srv)
Write-Host "Differential Backup complete."
$bk.Devices.Remove($bdid)
$rs.Database = "AdventureWorks2012"
$srv.Databases["AdventureWorks2012"].Drop()
$rs.SqlRestore($srv)
Write-Host "Full Database Restore complete."
$rs.Devices.Remove($bdi)
$rs.NoRecovery = $FALSE
$rs.Devices.Add($bdid)
$rs.SqlRestore($srv)
Write-Host "Differential Database Restore complete."
$rs.Devices.Remove($bdid)
$srv.Databases["AdventureWorks2012"].DatabaseOptions.RecoveryModel = $recoverymod
$srv.Databases["AdventureWorks2012"].Tables["test_table"].Drop()
$srv.Databases["AdventureWorks2012"].Alter()
remove-item C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Test_Full_Backup1
remove-item C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Test_Differential_Backup1

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.SqlServer.Management.Smo Namespace

Other Resources

RESTORE (Transact-SQL)

Back Up and Restore of SQL Server Databases

Backing Up and Restoring Databases and Transaction Logs