BackupSQL.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<#PSScriptInfo
 
.VERSION 1.1
 
.GUID 1069276e-50b4-414a-ae8c-b8801445ae7e
 
.AUTHOR Juan Granados
 
.COPYRIGHT 2021 Juan Granados
 
.TAGS SQL Server WID Windows Internal Database backup mail email rotation network share
 
.LICENSEURI https://raw.githubusercontent.com/juangranados/powershell-scripts/main/LICENSE
 
.PROJECTURI https://github.com/juangranados/powershell-scripts/tree/main/SQL%20Server%20Backup
 
.EXTERNALMODULEDEPENDENCIES
 
.RELEASENOTES
    Fix switch parameters
#>


<#
.SYNOPSIS
    Full and Log Backup of SQL Server instance databases with SMO
.DESCRIPTION
    Performs Full and Log Backup of SQL Server instance databases with SMO in a zip file and sends an email with the result.
     
    Deletes backups older than n days.
     
    For detailed how to use, run: Get-Help BackupSQL.ps1 -Full
     
    Requisites to run backup in a computer without SQL Server installed
     
    1. Navigate to: https://www.microsoft.com/en-US/download/details.aspx?id=55992 and install SQLSysClrTypes.msi
     
    2. Run from PowerShell
        - Register-PackageSource -provider NuGet -name nugetRepository -location https://www.nuget.org/api/v2
        - Install-Package Microsoft.SqlServer.SqlManagementObjects
     
    3. Run from PowerShell: Install-Module -Name SqlServer
.PARAMETER BackupDirectory
    Directory where zip file will be saved. UNC paths are supported.
    Default "...\My Documents\SQLBackup"
    Example: \\SRV-FS01\Backups\SQL01
.PARAMETER DataBases
    Array of databases to backup.
    If empty all instance databases will be saved.
    Example: BEDB,msdb,model
.PARAMETER Instance
    Instance Name.
    Default: default instance.
    Example: SQLSVR01\BKUPEXEC
.PARAMETER FullBackup
    Copy database and logs
.PARAMETER RetainDays
    Days to keep backups in BackupDirectory. Backups prior to this number of days will be deleted.
    Default: keeps all backups
    Example: 30
.PARAMETER TempDirectory
    Temporary directory to save backups files in order to make a zip file.
    Warning: this folder will be deleted after backup.
    Default "C:\temp\SQLBackup"
    Example: E:\SQLBackupTemp
.PARAMETER SMTPServer
    Sets smtp server in order to sent an email with backup result.
    Default: None
.PARAMETER Recipient
    Array of emails addresses which will receive the backup result.
    Default: None
.PARAMETER Sender
    Email address which will send the backup result.
    Default: None
.PARAMETER Username
    Username in case of smtp server requires authentication.
    Default: None
.PARAMETER Password
    Password in case of smtp server requires authentication.
    Default: None
.PARAMETER SSL
    Use of SSL in case of smtp server requires SSL.
    Default: False
.PARAMETER Port
    Port to connect to smtp server.
    Default: 25
.PARAMETER WriteEvent
    Writes an event with script result in Windows Application Event Log
.EXAMPLE
    Backup default instance databases to a network share
    C:\PS>.\BackupSQL.ps1 -BackupDirectory \\FS-SERVER01\BackupSQL
.EXAMPLE
    Backup WID to a network share
    BackupSQL.ps1 -BackupDirectory "\\MV0SRV-C01\Backups" -Instance "\\.\pipe\MICROSOFT##WID\tsql\query"
.EXAMPLE
    Backup default instance databases to a network share and send an email with result using gmail
    C:\PS>.\BackupSQL.ps1 -BackupDirectory \\FS-SERVER01\BackupSQL -SMTPServer smtp.gmail.com -Recipient jgranados@contoso.com,administrator@contoso.com -Sender backupSQL@gmail.com -Username backupSQL@gmail.com -Password Pa$$W0rd -SSL True -Port 587
.EXAMPLE
    Backup named instance databases to a network share
    C:\PS>.\BackupSQL.ps1 -BackupDirectory \\FS-SERVER01\BackupSQL -Instance SQLSVR01\BKUPEXEC
.EXAMPLE
    Backup default instance databases to a network share, delete from network share files older than a week and
    write result in Windows Application Event
    C:\PS>.\BackupSQL.ps1 -BackupDirectory \\FS-SERVER01\BackupSQL -RetainDays 7 -WriteEvent True
.EXAMPLE
    Backup only specified databases of a named instance
    C:\PS>.\BackupSQL.ps1 -Instance SQLSVR01\BKUPEXEC -DataBases BEDB,msdb,model
.LINK
    https://github.com/juangranados/powershell-scripts/tree/main/SQL%20Server%20Backup
.NOTES
    Author: Juan Granados
#>

    Param(
        [Parameter(Mandatory=$false,Position=0)] 
        [ValidateNotNullOrEmpty()]
        [string]$BackupDirectory=[environment]::getfolderpath("mydocuments") + "\SQLBackup",
        [Parameter(Mandatory=$false,Position=1)] 
        [ValidateNotNullOrEmpty()]
        [string[]]$DataBases="all",
        [Parameter(Mandatory=$false,Position=2)] 
        [ValidateNotNullOrEmpty()]
        [string]$Instance=$env:computerName,
        [Parameter()] 
        [switch]$FullBackup,
        [Parameter(Mandatory=$false,Position=4)] 
        [ValidateRange(0,36500)]
        [int]$RetainDays=0,
        [Parameter(Mandatory=$false,Position=5)] 
        [ValidateNotNullOrEmpty()]
        [string]$TempDirectory = "C:\temp\SQLBackup",
        [Parameter(Mandatory=$false,Position=6)] 
        [ValidateNotNullOrEmpty()]
        [string]$SMTPServer="None",
        [Parameter(Mandatory=$false,Position=7)] 
        [ValidateNotNullOrEmpty()]
        [string[]]$Recipient,
        [Parameter(Mandatory=$false,Position=8)] 
        [ValidateNotNullOrEmpty()]
        [string]$Sender,
        [Parameter(Mandatory=$false,Position=9)] 
        [ValidateNotNullOrEmpty()]
        [string]$Username="None",
        [Parameter(Mandatory=$false,Position=10)] 
        [ValidateNotNullOrEmpty()]
        [string]$Password="None",
        [Parameter(Mandatory=$false,Position=11)] 
        [ValidateSet("True","False")]
        [string[]]$SSL="False",
        [Parameter(Mandatory=$false,Position=12)] 
        [ValidateNotNullOrEmpty()]
        [int]$Port=25,
        [Parameter()] 
        [switch]$WriteEvent
    )
Import-Module SQLServer -ErrorAction SilentlyContinue
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo')            
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.Sdk.Sfc')            
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')            

# Required if SQL Server 2008 (SMO 10.0).
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended')            
$ErrorActionPreference = "Stop"

# Function to find dabatabase in $Databases[] variable
Function Get-Database {
    Param([string]$Database)
    for($i = 0; $i -le $Databases.length -1; $i++) {
        if ($Databases[$i] -eq $Database) {
            return $true
        }
    }
    return $false
}

# Variable to measure script execution time
$startDTM = (Get-Date)

# Data sumatory
$DataSum = 0

# Timestamp
$timestamp = Get-Date -format yyyy-MM-dd-HH-mm-ss

# Create application event source
if ($WriteEvent) {
    if (!(Get-Eventlog -LogName "Application" -Source "BackupSQL")) {
        New-Eventlog -LogName "Application" -Source "BackupSQL"
    }
}
if ($TempDirectory -eq $BackupDirectory) {
    Write-Output "BackupDirectory can not be the same as TempDirectory. Script can not continue"
    if ($WriteEvent) {
        Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Error �EventID 2 �Message "BackupDirectory can not be the same as TempDirectory. Script can not continue."
    }
    Exit
}
if ($TempDirectory.Chars($TempDirectory.Length - 1) -eq '\') {
        $TempDirectory = ($TempDirectory.TrimEnd('\'))
}
if ($BackupDirectory.Chars($BackupDirectory.Length - 1) -eq '\') {
        $BackupDirectory = ($BackupDirectory.TrimEnd('\'))
}
# Check if backup directory exist and try to create if not
if(!(Test-Path -Path $BackupDirectory)) {
    try {
        New-Item -ItemType directory -Path $BackupDirectory
    } catch {
        Write-Output "$BackupDirectory does not exists and can not be created. Script can not continue"
        if ($WriteEvent) {
           Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Error �EventID 2 �Message "SQL Backup Failed. $BackupDirectory does not exists and can not be created."
        }
        Exit
    }
}      

# Check if local directory exist and try to create if not
if(!(Test-Path -Path $TempDirectory)) {
    try {
        New-Item -ItemType directory -Path $TempDirectory
    } catch {
         Write-Output "$TempDirectory does not exists and can not be created. Script can not continue"
         if ($WriteEvent) {
             Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Error �EventID 2 �Message "SQL Backup Failed.$TempDirectory does not exists and can not be created."
         }
         Exit 
    }
}

try {
    Write-Output "Starting backup"
    # SQL Server
    $srv = New-Object Microsoft.SqlServer.Management.Smo.Server $Instance  
    
    #Disable timeouts monitorization
    $srv.ConnectionContext.StatementTimeout = 0

    # Delete temp folder archives
    Get-ChildItem �Path  $TempDirectory | Remove-Item
    
    # Copy databases
    foreach ($db in $srv.Databases) {
        If( (($DataBases[0] -eq "all") -or (Get-Database $db.Name)) -and ($db.Name -ne "tempdb") ) {            
            $timestamp = Get-Date -format yyyy-MM-dd-HH-mm-ss  
            $backup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")        
            $backup.Action = "Database"           
            $backup.Database = $db.Name
            Write-Output "Copying $db"          
            $backup.Devices.AddDevice("$TempDirectory\" + $db.Name + "_full_" + $timestamp + ".bak", "File")            
            $backup.BackupSetDescription = "Full backup of " + $db.Name + " " + $timestamp            
            $backup.Incremental = 0            
            # Full backup
            $backup.SqlBackup($srv)     
            # Log backup if database recovery mode is not simple
            If (($db.RecoveryModel -ne 3) -and ($FullBackup))            
            {                     
                $backup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")            
                $backup.Action = "Log"            
                $backup.Database = $db.Name            
                $backup.Devices.AddDevice("$TempDirectory\" + $db.Name + "_log_" + $timestamp + ".trn", "File")            
                $backup.BackupSetDescription = "Log backup of " + $db.Name + " " + $timestamp            
                # Truncate log prior to backup
                $backup.LogTruncation = "Truncate"
                # Log backup
                $backup.SqlBackup($srv)            
            }            
        }
    }

    # Creation time
    $timestamp = Get-Date -format yyyy-MM-dd-HH-mm-ss

    # Set zip file and delete '\'
    $InstanceName = $Instance -Replace "\\","-"
    $ZipFile = "$BackupDirectory\$timestamp" + "_" + $InstanceName + "_Backup.zip"
    If(Test-path $ZipFile) {
        Remove-item $ZipFile -Force
    }
    Write-Output "Creating zip file in $BackupDirectory"
    # Zip compression
    Add-Type -assembly "system.io.compression.filesystem"
    [io.compression.zipfile]::CreateFromDirectory($TempDirectory, $ZipFile) 
    #Get-Childitem $TempDirectory -Recurse | Write-Zip -IncludeEmptyDirectories -OutputPath $ZipFile -EntryPathRoot $TempDirectory

    # Show information about the size of file copied
    $DataSum = "{0:N3}" -f (((Get-Item $ZipFile).length) / 1MB)
    Write-Output "Backup $ZipFile stored. $DataSum MB copied"
    
    # Show information about the size of file copied on Windows Event Log
    if ($WriteEvent) {
        Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Information �EventID 1 �Message "Backup $ZipFile stored. $DataSum MB copied"
    }
    # Preparing mail sending
    if($SMTPServer -ne "None") {
        #Creating a Mail object
        $msg = new-object Net.Mail.MailMessage

        #Creating SMTP server object
        $smtp = new-object Net.Mail.SmtpClient($SMTPServer,$Port)

        #Email structure
        $msg.From = $Sender
        $msg.ReplyTo = $Sender
        ForEach($mail in $Recipient) {
            $msg.To.Add($mail)
        }
        if ($Username -ne "None" -and $Password -ne "None") {
                $smtp.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
        }
        if ($SSL -ne "False") {
                $smtp.EnableSsl = $true 
        }
    }
    # Sending email with backup result
    if ($SMTPServer -ne "None") {
        #Email subject
        $msg.subject = $Instance + " SQL Backup Success"
        #Email body
        $msg.body = "Backup $ZipFile stored. $DataSum MB copied"
        #Sending email
        try{
            Write-Output "Sending email"
            $smtp.Send($msg)
            Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Information �EventID 4 �Message "Success backup result sent to $Recipient"
           } catch {
            # Write error on application event log
            if ($WriteEvent) {
                Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Error �EventID 3 �Message "Error sending mail. " + "Exception Type: $($_.Exception.GetType().FullName)" + ". Exception Message: $($_.Exception.Message)"
            }
            write-host "Caught an exception:" -ForegroundColor Red
            write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
            write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red 
        }
    }
    # Delete temp directory
    Write-Output "Deleting folder $TempDirectory"         
    Remove-Item -Recurse -ErrorAction SilentlyContinue -Confirm:$false �Path $TempDirectory
    
    # Delete old files from backup directory
    if ($RetainDays -ne 0) {
        get-childitem $BackupDirectory -recurse | where {$_.lastwritetime -lt (get-date).adddays(-$RetainDays) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}
    }
    
    # Get execution time
    $endDTM = (Get-Date)
    
    Write-Output "Execution time: $(($endDTM-$startDTM).hours) hours $(($endDTM-$startDTM).Minutes) minutes $(($endDTM-$startDTM).Seconds) seconds"
} catch {
    write-host "Caught an exception:" -ForegroundColor Red
    write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
    write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
    # Delete temp directory
    Remove-Item -Recurse -ErrorAction SilentlyContinue -Confirm:$false �Path $TempDirectory
    Write-Host "Script can not continue."
    # Write error on application event log
    if ($WriteEvent) {
        Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Error �EventID 2 �Message "SQL Backup Failed" + "Exception Type: $($_.Exception.GetType().FullName)" + ". Exception Message: $($_.Exception.Message)"
    }
    # Sending email with backup result
    if ($SMTPServer -ne "None") {
        #Email subject
        $msg.subject = $Instance + " SQL Backup Error"
        #Email body
        $msg.body = "SQL Backup Failed" + "Exception Type: $($_.Exception.GetType().FullName)" + ". Exception Message: $($_.Exception.Message)"
        #Sending email
        Write-Output "Sending email"
        try{
        $smtp.Send($msg)
        Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Information �EventID 4 �Message "Error backup result sent to $Recipient"
        } catch {
            # Write error on application event log
            if ($WriteEvent) {
                Write-EventLog �LogName Application �Source "BackupSQL" �EntryType Error �EventID 3 �Message "Error sending mail. " + "Exception Type: $($_.Exception.GetType().FullName)" + ". Exception Message: $($_.Exception.Message)"
            }
            write-host "Caught an exception:" -ForegroundColor Red
            write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
            write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
}