-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL-Schema-Backup.ps1
281 lines (239 loc) · 12.1 KB
/
SQL-Schema-Backup.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
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)]
[System.String]
$database,
[Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false)]
[System.String]
$server,
[Parameter(Mandatory=$True, Position=2, ValueFromPipeline=$false)]
[System.String]
$SourceControlDirectory,
[Parameter(Mandatory=$True, Position=3, ValueFromPipeline=$false)]
[System.String]
$logDirectory,
[Parameter(Mandatory=$True, Position=4, ValueFromPipeline=$false)]
[System.Boolean]
$pushToGit
)
$logName = 'SQL-Schema-Export-'
$logFileName = $logName + (Get-Date -f yyyy-MM-dd-HH-mm) + ".log"
$logFullPath = Join-Path $logDirectory $logFileName
$logFileLimit = (Get-Date).AddDays(-15)
$svr = Connect-dbaInstance -SqlInstance $server
if(-Not(Test-Path -Path $logFullPath -PathType Leaf))
{
try
{
$null = New-Item -ItemType File -Path $logFullPath -Force -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - The log file '$logFileName' has been created"
}
catch
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to log file in '$logFullPath'. The Error was: $error"
}
}
try {
Add-Content -Path $logFullPath -Value "$(Get-Date -f "yyyy-MM-dd-HH-mm") - Attempting to delete old log files"
Get-ChildItem -Path $logFullPath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $logFileLimit } | Remove-Item -Force
Add-Content -Path $logFullPath -Value "$(Get-Date -f "yyyy-MM-dd-HH-mm") - Old log files deleted"
}
catch {
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to delete old log files from '$logFullPath'. The Error was: $error"
}
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Script starting" -ForegroundColor Gray
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Script starting"
$SourceControlDirectory = $SourceControlDirectory + '\'
$tablePath = $SourceControlDirectory + 'Tables'
$storedProcedurePath = $SourceControlDirectory + 'StoredProcedures'
$viewPath = $SourceControlDirectory + 'Views'
$schemaPath = $SourceControlDirectory + 'Schemas'
$constraintPath = $SourceControlDirectory + 'Constraints'
if(Get-Module -ListAvailable -name dbatools)
{
Import-Module -Name dbatools
} else
{
try
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to install dbatools as it is not present on this system."
Install-Module dbatools -Confirm $false
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - DbaTools has now been installed."
}
catch {
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to install dbatools. The Error was: $error"
}
}
if (-not (Test-Path -LiteralPath $tablePath) -and (Get-DbaDbTable -SqlInstance $svr -Database $database | Measure-Object).Count -gt 0)
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Directory '$tablePath' doesn't exist, attempting to create."
try
{
New-Item -Path $tablePath -ItemType Directory -ErrorAction Stop | Out-Null
}
catch
{
Write-Error -Message "Unable to create directory '$tablePath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to create directory '$tablePath'. Error was: $error"
}
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Successfully created directory '$tablePath'."
}
else
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - '$tablePath' already existed"
}
if (-not (Test-Path -LiteralPath $schemaPath) -and (Get-DbaDbStoredProcedure -SqlInstance $svr -Database $database -ExcludeSystemSp | Measure-Object).Count -gt 0)
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Directory '$schemaPath' doesn't exist, attempting to create."
try
{
New-Item -Path $schemaPath -ItemType Directory -ErrorAction Stop | Out-Null #-Force
}
catch
{
Write-Error -Message "Unable to create directory '$schemaPath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to create directory '$schemaPath'. Error was: $error" -ErrorAction Stop
}
Add-Content -Path $logFullPath -Value "Successfully created directory '$schemaPath'."
}
else
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - '$schemaPath' already existed"
}
if (-not (Test-Path -LiteralPath $storedProcedurePath) -and (Get-DbaDbStoredProcedure -SqlInstance $svr -Database $database -ExcludeSystemSp | Measure-Object).Count -gt 0)
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Directory '$storedProcedurePath' doesn't exist, attempting to create."
try
{
New-Item -Path $storedProcedurePath -ItemType Directory -ErrorAction Stop | Out-Null #-Force
}
catch
{
Write-Error -Message "Unable to create directory '$storedProcedurePath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to create directory '$storedProcedurePath'. Error was: $error" -ErrorAction Stop
}
Add-Content -Path $logFullPath -Value "Successfully created directory '$storedProcedurePath'."
}
else
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - '$storedProcedurePath' already existed"
}
if (-not (Test-Path -LiteralPath $viewPath) -and (Get-DbaDbView -SqlInstance $svr -Database $database -ExcludeSystemView | Measure-Object).Count -gt 0)
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Directory '$viewPath' doesn't exist, attempting to create."
try
{
New-Item -Path $viewPath -ItemType Directory -ErrorAction Stop | Out-Null #-Force
}
catch
{
Write-Error -Message "Unable to create directory '$viewPath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to create directory '$viewPath'. Error was: $error"
}
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Successfully created directory '$viewPath'."
}
else
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - '$viewPath' already existed"
}
if (-not (Test-Path -Path $constraintPath) -and (Get-DbaDbTable -SqlInstance $svr -Database $database | Measure-Object).Count -gt 0)
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Directory '$constraintPath' doesn't exist, attempting to create."
try
{
New-Item -Path $constraintPath -ItemType Directory -ErrorAction Stop | Out-Null
}
catch
{
Write-Error -Message "Unable to create directory '$constraintPath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to create directory '$constraintPath'. Error was: $error"
}
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Successfully created directory '$constraintPath'."
}
else
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - '$constraintPath' already existed"
}
try
{
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to export Table objects to '$tablePath' for instance '$server' from database '$database'"
$options = New-DbaScriptingOption
$options.ScriptSchema = $true
$options.IncludeDatabaseContext = $false
$options.IncludeHeaders = $false
$Options.NoCommandTerminator = $false
$options.DriPrimaryKey = $true
$Options.ScriptBatchTerminator = $true
$options.DriAllConstraints = $false
$Options.AnsiFile = $true;
try {
Get-DbaDbTable -SqlInstance $svr -Database $database | ForEach-Object { Export-DbaScript -InputObject $_ -FilePath (Join-Path $tablePath -ChildPath "$($_.Name).sql") -ScriptingOptionsObject $options }
}
catch {
Write-Error -Message "Unable to export tables to '$tablePath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to export tables to '$tablePath'. Error was: $error"
}
$options = New-DbaScriptingOption
$options.ContinueScriptingOnError = $false
$options.PrimaryObject = $false
$options.DriAllKeys = $true
$options.DriForeignKeys = $true
$options.IncludeHeaders = $false
$options.Triggers = $true;
$allTables = Get-DbaDbTable -SqlInstance $server -Database $database
$constraintFilePath = $constraintPath + '\constraints.sql'
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to export constraints to $constraintFilePath"
try
{
$allTables | Export-DbaScript -FilePath $constraintFilePath -ScriptingOptionsObject $options -EnableException -NoPrefix
}
catch {
Write-Error -Message "Unable to export constraints to '$constraintFilePath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to export constraints to '$constraintPath'. Error was: $error"
}
}
catch
{
Write-Error -Message "Unable to export table objects '$tablePath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to export table objects '$tablePath'. Error was: $error"
}
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to export Stored Procedures to '$tablePath' for instance '$server' from database '$database'"
try
{
Get-DbaDbSchema -SqlInstance $svr -Database $database | ForEach-Object { Export-DbaScript -InputObject $_ -FilePath (Join-Path $schemaPath -ChildPath "$($_.Name).sql") }
}
catch
{
Write-Error -Message "Unable to export stored procedures '$schemaPath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to export stored procedures '$schemaPath'. Error was: $error"
}
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to export Stored Procedures to '$tablePath' for instance '$server' from database '$database'"
try
{
Get-DbaDbStoredProcedure -SqlInstance $svr -Database $database -ExcludeSystemSp | ForEach-Object { Export-DbaScript -InputObject $_ -FilePath (Join-Path $storedProcedurePath -ChildPath "$($_.Name).sql") -ScriptingOptionsObject $options }
}
catch
{
Write-Error -Message "Unable to export stored procedures '$viewPath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to export stored procedures '$viewPath'. Error was: $error"
}
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Attempting to export Views to '$viewPath' for instance '$server' from database '$database'"
try
{
Get-DbaDbView -SqlInstance $svr -Database $database -ExcludeSystemView | ForEach-Object { Export-DbaScript -InputObject $_ -FilePath (Join-Path $viewPath -ChildPath "$($_.Name).sql") -ScriptingOptionsObject $options }
}
catch
{
Write-Error -Message "Unable to export Views to '$viewPath'. Error was: $error" -ErrorAction Stop
Add-Content -Path $logFullPath -Value "$(Get-Date -f yyyy-MM-dd-HH-mm) - Unable to export Views to '$viewPath'. Error was: $error"
}
if($pushToGit -eq $true) {
Set-Location $SourceControlDirectory
Write-Host -Message "Staging all changes to git" -ForegroundColor Gray
git add .
Write-Host -Message "Commiting changes to git" -ForegroundColor Gray
git commit -m "$(Get-Date -f yyyy-MM-dd-HH-mm) backup"
Write-Host -Message "Pushing changes to the remote git repository" -ForegroundColor Gray
git push origin main
}
Write-Host -Message "$(Get-Date -f yyyy-MM-dd-HH-mm) - Script Complete" -ForegroundColor Gray