Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/backup.ps1

raw
1 #!powershell
2 # -*- coding: utf-8 -*-
3
4 # (c) 2022, John McCall (@lowlydba)
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 #AnsibleRequires -CSharpUtil Ansible.Basic
8 #AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils
9 #Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" }
10
11 $ErrorActionPreference = "Stop"
12
13 $spec = @{
14 supports_check_mode = $true
15 options = @{
16 database = @{type = 'str'; required = $true }
17 path = @{type = 'str'; required = $false }
18 file_path = @{type = 'str'; required = $false }
19 increment_prefix = @{type = 'bool'; required = $false; default = $false }
20 replace_in_name = @{type = 'bool'; required = $false; default = $false }
21 copy_only = @{type = 'bool'; required = $false; default = $false }
22 type = @{type = 'str'; required = $false; default = 'database'; choices = @('full', 'log', 'differential', 'diff', 'database') }
23 timestamp_format = @{type = 'str'; required = $false }
24 encryption_certificate = @{type = 'str'; required = $false }
25 encryption_algorithm = @{type = 'str'; required = $false; choices = @('AES128', 'AES192', 'AES256', 'TRIPLEDES') }
26 create_folder = @{type = 'bool'; required = $false; default = $false }
27 file_count = @{type = 'int'; required = $false; default = 0 }
28 compress = @{type = 'bool'; required = $false; default = $false }
29 checksum = @{type = 'bool'; required = $false; default = $false }
30 verify = @{type = 'bool'; required = $false; default = $false }
31 no_recovery = @{type = 'bool'; required = $false; default = $false }
32 build_path = @{type = 'bool'; required = $false; default = $false }
33 max_transfer_size = @{type = 'int'; required = $false; default = 0 }
34 with_format = @{type = 'bool'; required = $false; default = $false }
35 initialize = @{type = 'bool'; required = $false; default = $false }
36 ignore_file_checks = @{type = 'bool'; required = $false; default = $false }
37 block_size = @{type = 'str'; required = $false; choices = @('0.5kb', '1kb', '2kb', '4kb', '8kb', '16kb', '32kb', '64kb') }
38 buffer_count = @{type = 'int'; required = $false; default = 0 }
39 azure_base_url = @{type = 'str'; required = $false }
40 azure_credential = @{type = 'str'; required = $false }
41 }
42 mutually_exclusive = @(
43 , @('path', 'azure_base_url')
44 )
45 required_together = @(
46 , @('encryption_algorithm', 'encryption_certificate')
47 )
48 }
49
50 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
51 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
52 $database = $module.Params.database
53 $path = $module.Params.path
54 $filePath = $module.Params.file_path
55 $incrementPrefix = $module.Params.increment_prefix
56 $replaceInName = $module.Params.replace_in_name
57 $copyOnly = $module.Params.copy_only
58 $type = $module.Params.type
59 $createFolder = $module.Params.create_folder
60 $fileCount = $module.Params.file_count
61 $compressBackup = $module.Params.compress_backup
62 $checkSum = $module.Params.checksum
63 $verify = $module.Params.verify
64 $maxTransferSize = $module.Params.max_transfer_size
65 $blockSize = $module.Params.block_size
66 $bufferCount = $module.Params.buffer_count
67 $noRecovery = $module.Params.no_recovery
68 $buildPath = $module.Params.build_path
69 $withFormat = $module.Params.with_format
70 $initialize = $module.Params.initialize
71 $timestampFormat = $module.Params.timestamp_format
72 $ignoreFileChecks = $module.Params.ignore_file_checks
73 $encryptionAlgorithm = $module.Params.encryption_algorithm
74 $encryptionCertificate = $modules.Params.encryption_certificate
75 $azureBaseUrl = $modules.Params.azure_base_url
76 $azureCredential = $modules.Param.azure_credential
77 $checkMode = $module.CheckMode
78 $module.Result.changed = $false
79 $PSDefaultParameterValues = @{ "*:EnableException" = $true; "*:Confirm" = $false; "*:WhatIf" = $checkMode }
80
81 try {
82 $backupSplat = @{
83 SqlInstance = $sqlInstance
84 SqlCredential = $sqlCredential
85 Database = $database
86 IncrementPrefix = $incrementPrefix
87 ReplaceInName = $replaceInName
88 CopyOnly = $copyOnly
89 Type = $type
90 CreateFolder = $createFolder
91 FileCount = $fileCount
92 CompressBackup = $compressBackup
93 Checksum = $checkSum
94 Verify = $verify
95 MaxTransferSize = $maxTransferSize
96 BufferCount = $bufferCount
97 NoRecovery = $noRecovery
98 BuildPath = $buildPath
99 WithFormat = $withFormat
100 Initialize = $initialize
101 IgnoreFileChecks = $ignoreFileChecks
102 }
103 if ($null -ne $path) {
104 $backupSplat.Add("Path", $path)
105 }
106 if ($null -ne $filePath) {
107 $backupSplat.Add("FilePath", $filePath)
108 }
109 if ($null -ne $blockSize) {
110 $backupSplat.Add("BlockSize", ($blockSize / 1))
111 }
112 if ($null -ne $timestampFormat) {
113 $backupSplat.Add("TimestampFormat", $timestampFormat)
114 }
115 if ($null -ne $encryptionAlgorithm) {
116 $backupSplat.Add("EncryptionAlgorithm", $encryptionAlgorithm)
117 }
118 if ($null -ne $encryptionCertificate) {
119 $backupSplat.Add("EncryptionCertificate", $encryptionCertificate)
120 }
121 if ($null -ne $azureBaseUrl) {
122 $backupSplat.Add("AzureBaseURL", $azureBaseUrl)
123 }
124 if ($null -ne $azureCredential) {
125 $backupSplat.Add("AzureCredential", $azureCredential)
126 }
127 $output = Backup-DbaDatabase @backupSplat
128
129 if ($null -ne $output) {
130 $resultData = ConvertTo-SerializableObject -InputObject $output
131 $module.Result.data = $resultData
132 $module.Result.changed = $true
133 }
134 $module.ExitJson()
135 }
136 catch {
137 $module.FailJson("Error backing up database: $($_.Exception.Message).", $_)
138 }