1+ <#
2+ . Synopsis
3+ To perform an ad-hoc backup using OLA Hallengrens solution
4+ . DESCRIPTION
5+ This function will perform an ad-hoc backup using OLA Hallengrens solution - It doesnot expose all of the
6+ capabilities but is useful for quick backups when needed
7+ Note the Database paramter is dynamically filled once you type it
8+ . EXAMPLE
9+ Backup-OlaDatabase -instance SERVERNAME -Type LOG -Share \\backupshare -Database Database
10+
11+ This will perform a LOG backup of the Database database on the SERVERNAME instance to the \\backupshare Share.
12+ Note the Database paramter is dynamically filled once you type it
13+ . EXAMPLE
14+ Backup-OlaDatabase -instance SERVERNAME -Type FULL -Share X:\backups -Database Database
15+
16+ This will perform a FULL backup of the Database database on the SERVERNAME instance to the X:\backups Share.
17+ Note the Database paramter is dynamically filled once you type it
18+ . EXAMPLE
19+ Backup-OlaDatabase -instance SERVERNAME -Type DIFF -Share \\TheShareForBackups -Database Database -OutputResults
20+
21+ This will perform a DIFF backup of the Database database on the SERVERNAME instance to the \\TheShareForBackups
22+ Share and output the message to the screen.
23+ Note the Database paramter is dynamically filled once you type it
24+ . NOTES
25+
26+ IF YOU HAVE THE STORED PROCEDURE IN A DIFFERENT DATABASE - You will need to alter line 130
27+
28+ AUTHOR - Rob Sewell https://sqldbawithabeard.com
29+ DATE - 27/10/2016
30+ #>
31+ function Backup-OlaDatabase
32+ {
33+ [CmdletBinding ( SupportsShouldProcess = $true ,
34+ PositionalBinding = $false ,
35+ ConfirmImpact = ' Medium' )]
36+ [Alias ()]
37+ Param
38+ (
39+ # The Server/instance
40+ [Parameter (Mandatory = $true , HelpMessage = ' This is the Instance that the database is on' ,
41+ ValueFromPipeline = $true ,
42+ ValueFromPipelineByPropertyName = $true ,
43+ ValueFromRemainingArguments = $false ,
44+ Position = 0 )]
45+ [ValidateNotNull ()]
46+ [ValidateNotNullOrEmpty ()]
47+ [string ]$Instance ,
48+ # The Server/instance
49+ [Parameter (Mandatory = $true , HelpMessage = ' This is the type of backup' ,
50+ ValueFromPipeline = $true ,
51+ ValueFromPipelineByPropertyName = $true ,
52+ ValueFromRemainingArguments = $false ,
53+ Position = 2 )]
54+ [ValidateNotNull ()]
55+ [ValidateNotNullOrEmpty ()]
56+ [ValidateSet (' FULL' , ' DIFF' , ' LOG' )]
57+ [string ]$Type ,
58+
59+
60+ # The Share
61+ [Parameter (Mandatory = $true , HelpMessage = ' This is the root of the backup directory' ,
62+ ValueFromPipeline = $true ,
63+ ValueFromPipelineByPropertyName = $true ,
64+ ValueFromRemainingArguments = $false ,
65+ Position = 2 )]
66+ [ValidateNotNull ()]
67+ [ValidateNotNullOrEmpty ()]
68+ [String ]$Share ,
69+
70+ # If you wish to see the results of the query
71+ [switch ]$OutputResults ,
72+
73+ # Compression off ?
74+ [switch ]$CompressionOff ,
75+
76+ # Copy Only ?
77+ [switch ]$CopyOnly
78+
79+ )
80+
81+
82+ DynamicParam {
83+ # Set the dynamic parameters' name
84+ $ParameterName = ' Database'
85+
86+ # Create the dictionary
87+ $RuntimeParameterDictionary = New-Object - TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
88+
89+ # Create the collection of attributes
90+ $AttributeCollection = New-Object - TypeName System.Collections.ObjectModel.Collection[System.Attribute ]
91+
92+ # Create and set the parameters' attributes
93+ $ParameterAttribute = New-Object - TypeName System.Management.Automation.ParameterAttribute
94+ $ParameterAttribute.Mandatory = $true
95+ $ParameterAttribute.Position = 1
96+
97+ # Add the attributes to the attributes collection
98+ $AttributeCollection.Add ($ParameterAttribute )
99+
100+ # Generate and set the ValidateSet
101+ # Load the assembly
102+ [void ][reflection.assembly ]::LoadWithPartialName( ' Microsoft.SqlServer.Smo' )
103+
104+ $srv = New-Object - TypeName Microsoft.SqlServer.Management.Smo.Server - ArgumentList $Instance
105+ $arrSet = $Srv.databases.Name
106+ $srv.ConnectionContext.Disconnect ()
107+ $ValidateSetAttribute = New-Object - TypeName System.Management.Automation.ValidateSetAttribute - ArgumentList ($arrSet )
108+
109+ # Add the ValidateSet to the attributes collection
110+ $AttributeCollection.Add ($ValidateSetAttribute )
111+
112+ # Create and return the dynamic parameter
113+ $RuntimeParameter = New-Object - TypeName System.Management.Automation.RuntimeDefinedParameter - ArgumentList ($ParameterName , [string ], $AttributeCollection )
114+ $RuntimeParameterDictionary.Add ($ParameterName , $RuntimeParameter )
115+ return $RuntimeParameterDictionary
116+ }
117+
118+ begin {
119+ # Bind the parameter to a friendly variable
120+ $Database = $PsBoundParameters [$ParameterName ]
121+
122+ if ($CompressionOff )
123+ {
124+ $Compression = ' N'
125+ }
126+ else
127+ {
128+ $Compression = ' Y'
129+ }
130+ if ($CopyOnly )
131+ {
132+ $Co = ' Y'
133+ }
134+ else
135+ {
136+ $CO = ' N'
137+ }
138+ }
139+ Process
140+ {
141+
142+ $Query = @"
143+ EXECUTE [master].[dbo].[DatabaseBackup]
144+ @Databases = '$Database ',
145+ @Directory = N'$Share ',
146+ @BackupType = '$Type ',
147+ @Verify = 'Y',
148+ @CheckSum = 'Y',
149+ @Compress= '$Compression ',
150+ @LogToTable = 'Y',
151+ @CopyOnly = '$CO '
152+ "@
153+
154+ if ($pscmdlet.ShouldProcess (" $Instance " , " Back up $Database " ))
155+ {
156+ try
157+ {
158+ $srv = New-Object - TypeName Microsoft.SqlServer.Management.Smo.Server - ArgumentList $Instance
159+ $SqlConnection = $srv.ConnectionContext
160+ if ($OutputResults )
161+ {
162+ Register-ObjectEvent - InputObject $SqlConnection - EventName InfoMessage - SupportEvent - Action {
163+ Write-Host $Event.SourceEventArgs
164+ }
165+ }
166+ $SqlConnection.StatementTimeout = 8000
167+ $SqlConnection.ConnectTimeout = 10
168+ $SqlConnection.Connect ()
169+ $SqlConnection.ExecuteNonQuery ($Query )
170+ Write-Output - InputObject " $Database has been backed up to $Share "
171+ }
172+ catch
173+ {
174+ Write-Warning - Message (" Something went Wrong. Run `$ Error[0] | Fl -force to work out why" )
175+ $Query
176+ }
177+ }
178+ }
179+ End
180+ {
181+ $srv.ConnectionContext.Disconnect ()
182+ }
183+ }
0 commit comments