-
Notifications
You must be signed in to change notification settings - Fork 22
/
Backup-OLADatabase.ps1
214 lines (174 loc) · 7.06 KB
/
Backup-OLADatabase.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<#PSScriptInfo
.VERSION 1.0
.GUID e690e558-357e-43fb-9bcf-5c39b33ce527
.AUTHOR Rob Sewell
.DESCRIPTION This function will perform an ad-hoc backup using OLA Hallengrens solution - It doesnot expose all of the capabilities but is useful for quick backups when needed Note the Database paramter is dynamically filled once you type it
.COMPANYNAME
.COPYRIGHT
.TAGS SQL, Ola Hallengren, Backup
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>
<#
.Synopsis
To perform an ad-hoc backup using OLA Hallengrens solution
.DESCRIPTION
This function will perform an ad-hoc backup using OLA Hallengrens solution - It doesnot expose all of the
capabilities but is useful for quick backups when needed
Note the Database paramter is dynamically filled once you type it
.EXAMPLE
Backup-OlaDatabase -instance SERVERNAME -Type LOG -Share \\backupshare -Database Database
This will perform a LOG backup of the Database database on the SERVERNAME instance to the \\backupshare Share.
Note the Database paramter is dynamically filled once you type it
.EXAMPLE
Backup-OlaDatabase -instance SERVERNAME -Type FULL -Share X:\backups -Database Database
This will perform a FULL backup of the Database database on the SERVERNAME instance to the X:\backups Share.
Note the Database paramter is dynamically filled once you type it
.EXAMPLE
Backup-OlaDatabase -instance SERVERNAME -Type DIFF -Share \\TheShareForBackups -Database Database -OutputResults
This will perform a DIFF backup of the Database database on the SERVERNAME instance to the \\TheShareForBackups
Share and output the message to the screen.
Note the Database paramter is dynamically filled once you type it
.NOTES
IF YOU HAVE THE STORED PROCEDURE IN A DIFFERENT DATABASE - You will need to alter line 130
AUTHOR - Rob Sewell https://sqldbawithabeard.com
DATE - 27/10/2016
#>
function Backup-OlaDatabase
{
[CmdletBinding( SupportsShouldProcess=$true,
PositionalBinding=$false,
ConfirmImpact='Medium')]
[Alias()]
Param
(
# The Server/instance
[Parameter(Mandatory=$true,HelpMessage='This is the Instance that the database is on',
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$Instance,
# The Server/instance
[Parameter(Mandatory=$true,HelpMessage='This is the type of backup',
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
Position=2)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateSet('FULL', 'DIFF', 'LOG')]
[string]$Type,
# The Share
[Parameter(Mandatory=$true,HelpMessage='This is the root of the backup directory',
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
Position=2)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[String]$Share,
#If you wish to see the results of the query
[switch]$OutputResults,
# Compression off ?
[switch]$CompressionOff,
# Copy Only ?
[switch]$CopyOnly
)
DynamicParam {
# Set the dynamic parameters' name
$ParameterName = 'Database'
# Create the dictionary
$RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes
$AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes
$ParameterAttribute = New-Object -TypeName System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 1
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
# Generate and set the ValidateSet
# Load the assembly
[void][reflection.assembly]::LoadWithPartialName( 'Microsoft.SqlServer.Smo' )
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $Instance
$arrSet = $Srv.databases.Name
$srv.ConnectionContext.Disconnect()
$ValidateSetAttribute = New-Object -TypeName System.Management.Automation.ValidateSetAttribute -ArgumentList ($arrSet)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList ($ParameterName, [string], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}
begin {
# Bind the parameter to a friendly variable
$Database = $PsBoundParameters[$ParameterName]
if($CompressionOff)
{
$Compression = 'N'
}
else
{
$Compression = 'Y'
}
if($CopyOnly)
{
$Co = 'Y'
}
else
{
$CO = 'N'
}
}
Process
{
$Query = @"
EXECUTE [master].[dbo].[DatabaseBackup]
@Databases = '$Database',
@Directory = N'$Share',
@BackupType = '$Type',
@Verify = 'Y',
@CheckSum = 'Y',
@Compress= '$Compression',
@LogToTable = 'Y',
@CopyOnly = '$CO'
"@
if ($pscmdlet.ShouldProcess("$Instance" , "Back up $Database"))
{
try
{
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $Instance
$SqlConnection = $srv.ConnectionContext
if($OutputResults)
{
Register-ObjectEvent -InputObject $SqlConnection -EventName InfoMessage -SupportEvent -Action {
Write-Host $Event.SourceEventArgs
}
}
$SqlConnection.StatementTimeout = 8000
$SqlConnection.ConnectTimeout = 10
$SqlConnection.Connect()
$SqlConnection.ExecuteNonQuery($Query)
Write-Output -InputObject "$Database has been backed up to $Share"
}
catch
{
Write-Warning -Message ("Something went Wrong. Run `$Error[0] | Fl -force to work out why")
$Query
}
}
}
End
{
$srv.ConnectionContext.Disconnect()
}
}