/
AboutBackupDatabases.Koans.ps1
180 lines (168 loc) · 7.71 KB
/
AboutBackupDatabases.Koans.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
#Requires -Modules dbatools
using module PSKoans
[Koan(Position = 1004, Module = 'dbatools')]
param()
<#
Backup-DbaDatabase
Backing up a database is crucially important to all who use databases.
If there is a need to recover data from an earlier time, then backups get used to recover the data.
There are many filters and options for backing up a database. dbatools helps you to take a backup
with the Backup-DbaDatabase command.
#>
Describe "Backup-DbaDatabase" {
#region Mocks
<#
Let's setup the environment for you. Unless you want the Koans to nearly always fail, I would
suggest not messing with this bit.
#>
BeforeAll {
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
1..10 | ForEach-Object -Process {
$RestoreStart = $StartDate
$RestoreEnd = $StartDate.AddSeconds(10)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = ('Database_{0:d2}' -f $_)
Type = 'Full'
TotalSize = ('{0}' -f ([Math]::PI * $_))
DeviceType = 'Disk'
Start = $RestoreStart
Duration = $RestoreEnd - $RestoreStart
End = $RestoreEnd
}
$StartDate = $RestoreEnd
}
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
1..2 | ForEach-Object -Process {
$RestoreStart = $StartDate
$RestoreEnd = $StartDate.AddSeconds(15)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = ('Database_{0:d2}' -f $_)
Type = 'Full'
TotalSize = ('{0}' -f ([Math]::PI * $_))
DeviceType = 'Disk'
Start = $RestoreStart
Duration = $RestoreEnd - $RestoreStart
End = $RestoreEnd
}
$StartDate = $RestoreEnd
}
} -ParameterFilter {
$_.Database -contains ('Database_01', 'Database_02')
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
$RestoreEnd = $StartDate.AddSeconds(5)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = 'Database_01'
Type = 'Differential'
TotalSize = ('{0}' -f ([Math]::PI))
DeviceType = 'Disk'
Start = $StartDate
Duration = $RestoreEnd - $StartDate
End = $RestoreEnd
}
} -ParameterFilter {
$_.Type -eq 'Differential'
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
$RestoreEnd = $StartDate.AddSeconds(5)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = 'Database_01'
Type = 'Differential'
TotalSize = ('{0}' -f ([Math]::PI))
DeviceType = 'Disk'
Start = $StartDate
Duration = $RestoreEnd - $StartDate
End = $RestoreEnd
Path = 'E:\Backups\Database_01_201901011200.bak'
}
} -ParameterFilter {
$_.Path -like 'E:\Backups\*'
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
$RestoreEnd = $StartDate.AddSeconds(5)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = 'Database_01'
Type = 'Differential'
TotalSize = ('{0}' -f ([Math]::PI))
DeviceType = 'Disk'
Start = $StartDate
Duration = $RestoreEnd - $StartDate
End = $RestoreEnd
Path = 'E:\Backups\Database01-Full.bak'
}
} -ParameterFilter {
$_.FilePath -eq 'dbname-backuptype.bak'
}
}
#endregion
It 'takes a database backup of all SQL Server databases' {
<#
By default, Backup-DbaDatabase will backup every database on the SQL Instance.
These backups will get saved to the default backup directory.
Complete the below command to backup all the databases on the localhost instance.
#>
$AllBackups = Backup-DbaDatabase -SqlInstance '____'
$AllBackups.ComputerName | Should -Be 'localhost'
}
It 'takes a database backup of specific SQL Server databases' {
<#
By using the -Database parameter, you can backup a single or many databases.
Complete the below command to backup the databases Database_01 and Database_02
#>
$SpecificBackups = Backup-DbaDatabase -SqlInstance localhost -Database '____','____'
$SpecificBackups.Database | Should -Contain 'Database_01', 'Database_02'
}
It 'takes different types of SQL Server backups' {
<#
There are different types of database backups that can be taken. The 3 main ones
are a Full backup (Full/Database), a Differential backup (Diff/Differential), and
a Transaction Log backup (Log).
Backup-DbaDatabase can take each of these types of backups.
Complete the below command to take a differential backup of the Database_01 database.
#>
$DifferentialBackup = Backup-DbaDatabase -SqlInstance localhost -Database Database_01 -Type '____'
$DifferentialBackup.Type | Should -Be 'Differential'
}
It 'takes backups and stores them at a specific location' {
<#
Managing where you store your backups is something that is unique to each person.
Backup-DbaDatabase allows you to specify where you want the backups to be stored.
It allows this specification by using the -Path parameter.
Complete the below command to store the backup file in the E:\Backups\ folder.
#>
$SpecificPathBackup = Backup-DbaDatabase -SqlInstance localhost -Database Database_01 -Path '____'
$SpecificPathBackup.Path | Should -BeLike 'E:\Backups\'
}
It 'takes backups and substitutes keywords such as database name and database backup type' {
<#
There are different conventions for specifying where the backup file will go to
and what the backup file will be called. There are different keywords that
dbatools provides that can be used with the -FilePath and -ReplaceInName.
instancename - will be replaced with the instance name
servername - will be replaced with the server name
dbname - will be replaced with the database name
timestamp - will be replaced with the timestamp
backuptype - will be replaced with Full, Log or Differential as appropriate.
Complete the below command to backup the database "Database01" as "Database01-Full.bak"
#>
$KeywordBackup = Backup-DbaDatabase -SqlInstance localhost -Database Database01 -FilePath '____-____.bak' -ReplaceInName
$BackupFileName = Split-Path $KeywordBackup.Path -Leaf
$BackupFileName | Should -Be 'Database01-Full.bak'
}
}