-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMove-TenantBackupFiles.ps1
217 lines (181 loc) · 6.87 KB
/
Move-TenantBackupFiles.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
Add-PSSnapin -Name VeeamPSSnapin
function Show-Options
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]
$Message,
[Parameter(Mandatory=$true)]
[string[]]
$Options
)
Write-Host -Object "`n$Message`n"
foreach ($option in $Options)
{
Write-Host -Object "`t[$($Options.IndexOf($option) + 1)] $($option)"
}
}
function Get-UserChoice
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]
$Message,
[Parameter(Mandatory=$true)]
[string[]]
$Options
)
$inputIsValid = $false
do
{
try
{
[int]$userInput = (Read-Host -Prompt "`n$Message")
}
catch
{
Write-Host -Object "Invalid input! Must be an integer."
continue
}
if ($userInput -ge 1 -and $userInput -le $Options.Length)
{
$inputIsValid = $true
}
else
{
Write-Host -Object "Invalid input! Must be in the range 1..$($Options.Length)."
}
}
until ($inputIsValid)
return $userInput - 1
}
function Get-RepositoryFreeSpace
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[Veeam.Backup.Core.CBackupRepository]
$Repository
)
$repoAccessor = [Veeam.Backup.Core.CRepositoryAccessorFactory]::Create($Repository)
$fsDriveInfo = $repoAccessor.FileCommander.FindDirInfo($Repository.FriendlyPath)
return $fsDriveInfo.FreeSpace
}
$allRepos = Get-VBRBackupRepository -ScaleOut
if (!$allRepos)
{
Write-Host -Object "Backup server has no scale-out repositories."
return
}
Show-Options -Message "Scale-out repositories:" -Options $allRepos.Name
$repoIndex = Get-UserChoice -Message "Choose a repository" -Options $allRepos.Name
$repo = $allRepos[$repoIndex]
Show-Options -Message "[$($repo.Name)] > Extents:" -Options $repo.Extent.Name
$sourceExtentIndex = Get-UserChoice -Message "Choose a source extent" -Options $repo.Extent.Name
$sourceExtent = $repo.Extent[$sourceExtentIndex]
$tenantsWithBackups = New-Object -TypeName System.Collections.Generic.List[PSCustomObject]
$sourceExtentAccessor = [Veeam.Backup.Core.CRepositoryAccessorFactory]::Create($sourceExtent.Repository)
$tenantQuotas = [Veeam.Backup.Core.CTenantQuota]::DbFindByRepositoryId($repo.Id)
foreach ($quota in $tenantQuotas)
{
$tenantFolderPath = [Veeam.Backup.Model.SPathConverter]::RepositoryPathToString(
$sourceExtent.Repository.FullPath.Combine($quota.PartialFolderPath),
$sourceExtent.Repository.Type
)
if ($sourceExtentAccessor.FileCommander.IsExists($tenantFolderPath))
{
$tenantFolderSize = $sourceExtentAccessor.FileCommander.GetDirSize($tenantFolderPath)
}
else
{
$tenantFolderSize = 0
}
if ($tenantFolderSize)
{
$tenantStats = [PSCustomObject]@{
tenantName = $quota.OwnerName;
tenantFolder = $quota.PartialFolderPath.ToString();
tenantFolderSize = $tenantFolderSize
}
$tenantsWithBackups.Add($tenantStats)
}
}
if (!$tenantsWithBackups)
{
Write-Host -Object "`n[$($sourceExtent.Name)] has no backups."
return
}
Show-Options -Message "[$($sourceExtent.Name)] > Tenants with backups:" -Options ($tenantsWithBackups |
ForEach-Object -Process {"$($_.tenantName) ($([System.Math]::Round($_.tenantFolderSize / 1GB, 2)) GB used)"})
$tenantIndex = Get-UserChoice -Message "Choose a tenant" -Options $tenantsWithBackups
$tenantIsActive = [Veeam.Backup.Core.CCloudTenant]::GetRunningJobTypesForTenants().ContainsKey(
$tenantsWithBackups[$tenantIndex].tenantName
)
if ($tenantIsActive)
{
Write-Host -Object "`n[$($tenantsWithBackups[$tenantIndex].tenantName)] has active jobs and will not be disabled."
return
}
Write-Host -Object "`nDisabling [$($tenantsWithBackups[$tenantIndex].tenantName)]."
Get-VBRCloudTenant -Name $tenantsWithBackups[$tenantIndex].tenantName | Disable-VBRCloudTenant
$tenantIsDisabled = ($tenantQuotas |
Where-Object -FilterScript {$_.OwnerName -eq $tenantsWithBackups[$tenantIndex].tenantName}).CachedTenant.Disabled
if (!$tenantIsDisabled)
{
Write-Host -Object "[$($tenantsWithBackups[$tenantIndex].tenantName)] has not been disabled."
return
}
Write-Host -Object "[$($tenantsWithBackups[$tenantIndex].tenantName)] has been disabled."
$possibleTargetExtents = $repo.Extent | Where-Object -FilterScript {$_.Id -ne $sourceExtent.Id}
Show-Options -Message "[$($repo.Name)] > Extents:" -Options ($possibleTargetExtents |
ForEach-Object -Process {
"$($_.Name) ($([System.Math]::Round((Get-RepositoryFreeSpace -Repository $_.Repository) / 1GB, 2)) GB free)"
})
$targetExtentIndex = Get-UserChoice -Message "Choose a target extent" -Options $possibleTargetExtents.Name
$targetExtent = $possibleTargetExtents[$targetExtentIndex]
$targetExtentFreeSpace = Get-RepositoryFreeSpace -Repository $targetExtent.Repository
if ($targetExtentFreeSpace -lt $tenantsWithBackups[$tenantIndex].tenantFolderSize)
{
Write-Host -Object "`nThere is not enough free space on [$($targetExtent.Name)]."
return
}
$jobSrc = [System.IO.File]::ReadAllText("$PSScriptRoot\job_src.ps1")
[Veeam.Backup.Common.CStringCoder]::Code($jobSrc, $true) | Out-File -FilePath "$PSScriptRoot\job.txt"
$jobDescription = "Job will move [{0}] backup files from [{1}] to [{2}]" -f
$tenantsWithBackups[$tenantIndex].tenantName,
$sourceExtent.Name,
$targetExtent.Name
$jobParams = New-Object -TypeName 'System.Collections.Generic.Dictionary[string, string]'
$jobParams.Add(
"TenantName", [Veeam.Backup.Common.CStringCoder]::Code($tenantsWithBackups[$tenantIndex].tenantName, $true)
)
$jobParams.Add("SobrId", [Veeam.Backup.Common.CStringCoder]::Code($repo.Id.ToString(), $true))
$jobParams.Add(
"SourceExtentId", [Veeam.Backup.Common.CStringCoder]::Code($sourceExtent.Repository.Id.ToString(), $true)
)
$jobParams.Add(
"TenantFolder",
[Veeam.Backup.Common.CStringCoder]::Code($tenantsWithBackups[$tenantIndex].tenantFolder, $true)
)
$jobParams.Add(
"TargetExtentId", [Veeam.Backup.Common.CStringCoder]::Code($targetExtent.Repository.Id.ToString(), $true)
)
$jobSpecArgs = @{
name = "Move Tenant Backup Files";
description = $jobDescription;
type = [Veeam.Backup.Model.CPowerShellScriptJobSpec+EResourcesType]::Script;
resources = @("$PSScriptRoot\job.txt");
parameters = $jobParams
}
$jobSpec = [Veeam.Backup.Model.CPowerShellScriptJobSpec]::Create(
$jobSpecArgs.name,
$jobSpecArgs.description,
$jobSpecArgs.type,
$jobSpecArgs.resources,
$jobSpecArgs.parameters
)
$jobManagementService = [Veeam.Backup.Core.SVeeamBackupService]::Instance.Session.GetJobManagementService()
[void]$jobManagementService.StartPowerShellScriptJob($jobSpec)
Write-Host -Object "`nJob has been started. Job log is available in the VB&R console > HISTORY > Orchestrated Tasks."