-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathRestore-DMSSQLDatabase.ps1
146 lines (120 loc) · 5.26 KB
/
Restore-DMSSQLDatabase.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#Requires -Version 5.0
#Requires -Modules SQLServer
<#
.SYNOPSIS
Restores a database from a backup or transaction log records
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires Module SQLServer
Requires the library script DMSSqlServer.ps1
.LINK
https://github.com/scriptrunner/ActionPacks/blob/master/DBSystems/SQLServer
.Parameter ServerInstance
Specifies the name of the target computer including the instance name, e.g. MyServer\Instance
.Parameter ServerCredential
Specifies a PSCredential object for the connection to the SQL Server. ServerCredential is ONLY used for SQL Logins.
When you are using Windows Authentication you don't specify -Credential. It is picked up from your current login.
.Parameter DBName
Specifies the name of the database to restore
.Parameter BackupFile
Specifies the location and file name of the backup
.Parameter RestoreAction
Specifies the type of backup operation to perform
.Parameter Checksum
Indicates that a checksum value is calculated during the restore operation
.Parameter ClearSuspectPageTable
Indicates that the suspect page table is deleted after the restore operation
.Parameter ContinueAfterError
Indicates that the operation continues when a checksum error occurs.
If not set, the operation will fail after a checksum error
.Parameter DatabaseFile
Specifies the database files targeted by the restore operation, comma separated.
This is only used when the RestoreAction parameter is set to File
.Parameter DatabaseFileGroup
Specifies the database file groups targeted by the restore operation, comma separated.
This is only used when the RestoreAction parameter is set to File
.Parameter KeepReplication
Indicates that the replication configuration is preserved
.Parameter NoRecovery
Indicates that the database is restored into the restoring state
.Parameter ReplaceDatabase
Indicates that a new image of the database is created. This overwrites any existing database with the same name.
If not set, the restore operation will fail when a database with that name already exists on the server.
.Parameter ToPointInTime
Specifies the endpoint for database log restoration. This only applies when RestoreAction is set to Log
.Parameter ConnectionTimeout
Specifies the time period to retry the command on the target server
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$ServerInstance,
[Parameter(Mandatory = $true)]
[string]$DBName,
[Parameter(Mandatory = $true)]
[string]$BackupFile,
[pscredential]$ServerCredential,
[ValidateSet('Database', 'Files', 'Log', 'OnlinePage', 'OnlineFiles')]
[string]$RestoreAction = "Database",
[switch]$CheckSum,
[switch]$ClearSuspectPageTable,
[switch]$ContinueAfterError,
[string]$DatabaseFile,
[string]$DatabaseFileGroup,
[switch]$KeepReplication,
[switch]$NoRecovery,
[switch]$ReplaceDatabase,
[Parameter(HelpMessage="ASRDisplay(Date)")]
[datetime]$ToPointInTime,
[int]$ConnectionTimeout = 30
)
Import-Module SQLServer
try{
$instance = GetSQLServerInstance -ServerInstance $ServerInstance -ServerCredential $ServerCredential -ConnectionTimeout $ConnectionTimeout
[hashtable]$cmdArgs = @{'ErrorAction' = 'Stop'
'InputObject' = $instance
'Database' = $DBName
'Confirm' = $false
'RestoreAction' = $RestoreAction
'CheckSum' = $CheckSum
'ClearSuspectPageTable' = $ClearSuspectPageTable
'ContinueAfterError' = $ContinueAfterError
'BackupFile' = $BackupFile
'KeepReplication' = $KeepReplication
'ReplaceDatabase' = $ReplaceDatabase
'NoRecovery' = $NoRecovery
'PassThru' = $true
}
if($RestoreAction -eq 'Files'){
if([System.String]::IsNullOrWhiteSpace($DatabaseFile) -eq $false){
$cmdArgs.Add('DatabaseFile',$DatabaseFile.Split(','))
}
elseif([System.String]::IsNullOrWhiteSpace($DatabaseFileGroup) -eq $false){
$cmdArgs.Add('DatabaseFileGroup',$DatabaseFileGroup.Split(','))
}
}
elseif($RestoreAction -eq 'Log'){
if($null -ne $ToPointInTime){
$cmdArgs.Add('ToPointInTime',$ToPointInTime)
}
}
$result = Restore-SqlDatabase @cmdArgs | Select-Object *
if($SRXEnv) {
$SRXEnv.ResultMessage = $result
}
else{
Write-Output $result
}
}
catch{
throw
}
finally{
}