-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLBackupWeekly.ps1
54 lines (46 loc) · 1.73 KB
/
SQLBackupWeekly.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
#Written by: Evan Lane
#Version 2.5
#Latest Revision: May 18th 2017
Import-Module BitsTransfer
$today = Get-Date -Format dd-MM-yyyy
#ServerPaths Config#######################################
$backupFol = "C:\SQLBackups\"
$backupFile = "SQLWeekly_$($today).bck"
$netFol = "\\192.168.x.x\<folder>"
#SQL Config###############################################
$sqlUsername = "username"
$sqlPassword = "password"
$sqlServer = "localhost\sqlserver"
$sqlDatabase = "DATABASE" #all Caps
#SMTP Config##############################################
$smtp = $true #set $false for no emails
$mailServer = "mail.domain.com"
$fromAddress = "SQLBackups@domain.com"
$toAddress = "user@domain.com"
$serverName = gc env:computername
#SMTP Message Configs#####################################
$successBody = @"
Complete: Backup Successful!
Output: $backupFile
"@
$errorBody = @"
Error: Backup Failed!
Please investigate $ServerName!
$ErrorMessage
$FailedItem
"@
#####################END USER CONFIG#######################
Try{
OSQL -U $sqlUserName -P $sqlPassword -S $sqlServer -Q "BACKUP DATABASE $sqlDatabase to disk = '$($backupFol)$($backupFile)'"
Start-BitsTransfer -Source "$($backupFol)$($backupFile)" -Destination "$($netFol)$($backupFile)" -Description "SQLBackup" -DisplayName "SQLBackup"
if ($smtp -eq $true){
Send-MailMessage -SmtpServer $mailServer -From $fromAddress -To $toAddress -Subject "$serverName SQL Weekly Backup - Successful" -Body $successBody
}
}
Catch{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
if ($smtp -eq $true){
Send-MailMessage -SmtpServer $mailServer -From $fromAddress -To $toAddress -Subject "$serverName SQL Weekly Backup - Failed" -Body $errorBody
}
}