-
Notifications
You must be signed in to change notification settings - Fork 237
/
Backup-NavContainerDatabases.ps1
129 lines (120 loc) · 6.07 KB
/
Backup-NavContainerDatabases.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
<#
.Synopsis
Backup databases in a NAV/BC Container as .bak files
.Description
If the Container is multi-tenant, this command will create an app.bak and a number of tenant .bak files
If the Container is single-tenant, this command will create one .bak file called database.bak.
.Parameter containerName
Name of the container in which you want to backup databases
.Parameter bakFolder
The folder to which the .bak files are exported (needs to be shared with the container)
.Parameter tenant
The tenant database(s) to export, only applies to multi-tenant containers. Omit to export all tenants.
.Parameter databaseCredential
database credentials if using an external sQL Server
.Parameter compress
Compress the database backup. SQL Express doesn't support compression.
.Example
Backup-BcContainerDatabases -containerName test
.Example
Backup-BcContainerDatabases -containerName test -bakfolder "c:\programdata\bccontainerhelper\extensions\test"
.Example
Backup-BcContainerDatabases -containerName test -tenant @("default")
#>
function Backup-BcContainerDatabases {
Param (
[string] $containerName = $bcContainerHelperConfig.defaultContainerName,
[string] $bakFolder = "",
[string[]] $tenant,
[pscredential] $databasecredential,
[switch] $compress
)
$telemetryScope = InitTelemetryScope -name $MyInvocation.InvocationName -parameterValues $PSBoundParameters -includeParameters @()
try {
$containerFolder = Join-Path $bcContainerHelperConfig.hostHelperFolder "Extensions\$containerName"
if ("$bakFolder" -eq "") {
$bakFolder = $containerFolder
}
elseif (!$bakFolder.Contains('\')) {
$navversion = Get-BcContainerNavversion -containerOrImageName $containerName
if ((Invoke-ScriptInBcContainer -containerName $containerName -scriptblock { $env:IsBcSandbox }) -eq "Y") {
$folderPrefix = "sandbox"
}
else {
$folderPrefix = "onprem"
}
$bakFolder = Join-Path $bcContainerHelperConfig.hostHelperFolder "$folderPrefix-$NavVersion-bakFolders\$bakFolder"
}
$containerBakFolder = Get-BcContainerPath -containerName $containerName -path $bakFolder -throw
Invoke-ScriptInBcContainer -containerName $containerName -usePwsh:$false -ScriptBlock { Param($containerBakfolder, $bakFolder, $tenant, $databasecredential, $compress)
function Backup {
Param (
[string] $serverInstance,
[string] $database,
[string] $bakFolder,
[string] $bakName,
[pscredential] $databaseCredential,
[switch] $compress
)
$bakFile = Join-Path $bakFolder "$bakName.bak"
if (Test-Path $bakFile) {
Remove-Item -Path $bakFile -Force
}
Write-Host "Backing up $database to $bakFile"
$params = @{}
if ($compress) { $params += @{ "CompressionOption" = "On" } }
if ($databaseCredential) { $params += @{ "credential" = $databaseCredential } }
Backup-SqlDatabase -ServerInstance $serverInstance -database $database -BackupFile $bakFile @params
}
$customConfigFile = Join-Path (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName "CustomSettings.config"
[xml]$customConfig = [System.IO.File]::ReadAllText($customConfigFile)
$multitenant = ($customConfig.SelectSingleNode("//appSettings/add[@key='Multitenant']").Value -eq "true")
$databaseServer = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseServer']").Value
$databaseInstance = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseInstance']").Value
$databaseName = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseName']").Value
$databaseServerInstance = $databaseServer
if ("$databaseInstance" -ne "") {
$databaseServerInstance = "$databaseServer\$databaseInstance"
}
if ($databaseServer -eq "localhost") {
$bakFolder = $containerBakFolder
}
if (!(Test-Path $bakFolder)) {
New-Item $bakFolder -ItemType Directory | Out-Null
}
if ($multitenant) {
if (!($tenant)) {
$tenant = @(get-navtenant $serverInstance | % { $_.Id }) + "tenant"
}
Backup -ServerInstance $databaseServerInstance -database $DatabaseName -bakFolder $bakFolder -bakName "app" -databasecredential $databasecredential -compress:$compress
$tenant | ForEach-Object {
$tenantInfo = Get-NAVTenant -ServerInstance $serverInstance $_ -ErrorAction SilentlyContinue
if ($tenantInfo) {
$dbName = $tenantInfo.DatabaseName
}
else {
$tenantInfo = Get-NAVTenant -ServerInstance $serverInstance default -ErrorAction SilentlyContinue
if ($tenantInfo) {
$dbName = $tenantInfo.DatabaseName.replace('default',$_)
}
else {
$dbName = $_
}
}
Backup -ServerInstance $databaseServerInstance -database $dbName -bakFolder $bakFolder -bakName $_ -databasecredential $databasecredential -compress:$compress
}
} else {
Backup -ServerInstance $databaseServerInstance -database $DatabaseName -bakFolder $bakFolder -bakName "database" -databasecredential $databasecredential -compress:$compress
}
} -ArgumentList $containerbakFolder, $bakFolder, $tenant, $databasecredential, $compress
}
catch {
TrackException -telemetryScope $telemetryScope -errorRecord $_
throw
}
finally {
TrackTrace -telemetryScope $telemetryScope
}
}
Set-Alias -Name Backup-NavContainerDatabases -Value Backup-BcContainerDatabases
Export-ModuleMember -Function Backup-BcContainerDatabases -Alias Backup-NavContainerDatabases