Skip to content

Commit

Permalink
Merge pull request #39 from crimdon/Backup-Compression
Browse files Browse the repository at this point in the history
Backup compression
  • Loading branch information
crimdon authored Sep 22, 2017
2 parents 4627bc2 + 0c28f98 commit 38a309c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
56 changes: 32 additions & 24 deletions SqlBackup/SqlBackup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ Try {
[string]$userName = Get-VstsInput -Name userName
[string]$userPassword = Get-VstsInput -Name userPassword
[string]$queryTimeout = Get-VstsInput -Name queryTimeout
[string]$backupCompression = Get-VstsInput -Name backupCompression

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection

if([string]::IsNullOrEmpty($userName)) {
if ([string]::IsNullOrEmpty($userName)) {
$SqlConnection.ConnectionString = "Server=$serverName;Initial Catalog=$databaseName;Trusted_Connection=True;Connection Timeout=30;"
}
else {
Expand All @@ -34,7 +35,7 @@ Try {
$SqlCmd.CommandTimeout = $queryTimeout

#Specify the Action property to generate a FULL backup
switch($backupType.ToLower()) {
switch ($backupType.ToLower()) {
"full" {
$backupAction = "DATABASE"
}
Expand All @@ -47,29 +48,42 @@ Try {
}

#Initialize the backup if set
switch($withInit) {
switch ($withInit) {
$false {
$mediaInit = "NOINIT"
$mediaInit = "NOFORMAT"
}
$true {
$mediaInit = "INIT"
$mediaInit = "FORMAT"
}
}

#Set WITH options
if($backupType -eq "differential") {
$withOptions = "DIFFERENTIAL, " + $mediaInit;

#Set copy only option
switch ($copyOnly) {
$false {$copyOnlyAction = ""}
$true {$copyOnlyAction = ", COPY_ONLY"}
}

#Set compression option
$sqlCmd.CommandText = "SELECT ISNULL((SELECT value FROM sys.configurations WHERE name = 'backup compression default'),-1)"
[int]$returnVal = $sqlCmd.ExecuteScalar()
if ($returnVal -eq -1) {
$compressionAction = ""
}
else {
switch($copyOnly) {
$false {
$withOptions = $mediaInit
}
$true {
$withOptions = $mediaInit + ", COPY_ONLY"
}
switch ($backupCompression) {
"Default" {$compressionAction = ""}
"Compress" {$compressionAction = ", COMPRESSION"}
"NoCompress" {$compressionAction = ", NO_COMPRESSION"}
}
}

#Set WITH options
if ($backupType -eq "differential") {
$withOptions = "DIFFERENTIAL, " + $mediaInit + $compressionAction;
}
else {
$withOptions = $mediaInit + $copyOnlyAction + $compressionAction;
}

#Build the backup query using Windows Authenication
$sqlCommand = "BACKUP " + $backupAction + " " + $databaseName + " TO DISK = N'" + $backupFile + "' WITH " + $withOptions;
Expand All @@ -78,6 +92,7 @@ Try {

#Execute the backup
$SqlCmd.CommandText = $sqlCommand
Write-Host $sqlCommand
$reader = $SqlCmd.ExecuteNonQuery()

$SqlConnection.Close()
Expand All @@ -88,11 +103,4 @@ Try {
Catch {
Write-Host "Error running SQL Backup: $_" -ForegroundColor Red
throw $_
}







}
18 changes: 16 additions & 2 deletions SqlBackup/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"category": "Utility",
"version": {
"Major": "2",
"Minor": "0",
"Patch": "0"
"Minor": "1",
"Patch": "3"
},
"groups": [
{
Expand Down Expand Up @@ -94,6 +94,20 @@
"defaultValue": "30",
"required": true,
"groupName": "advanced"
},
{
"name": "backupCompression",
"type": "pickList",
"label": "Backup Compression",
"defaultValue": "Default",
"required": true,
"options": {
"Default": "User the default server setting",
"Compress": "Compress backup",
"NoCompress": "Do not compress backup"
},
"groupName": "advanced",
"helpMarkDown": "Set backup compression if your server supports it"
}

],
Expand Down
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 1,
"id": "sql-toolkit",
"name": "SQL Toolkit",
"version": "2.6.10",
"version": "2.7.3",
"publisher": "andrewlackenby",
"public": true,
"categories": [
Expand Down

0 comments on commit 38a309c

Please sign in to comment.