ObjectFilesHandling/Backup-CompanyData.ps1

<#
.SYNOPSIS
    Export data from selected tables and save it to json files
.DESCRIPTION
    Make a backup of data from selected tables and exports it to json files
.EXAMPLE
    Backup-CompanyData -containerName bccontainer -Path ~/MyProject/Apps -companyName TestCompany
.NOTES
    The command uses commands from BcContainerHelper to export and backup the objects
#>

function Install-DemoDataManagerApp {
    Param (

        [string] $containerName,
        [string] $appName,
        [string] $appFile
    )

    Publish-BcContainerApp -containerName $containerName -appFile $appFile -skipVerification -scope Tenant
    Sync-BcContainerApp -containerName $containerName -appName $appName -Mode ForceSync -Force
    Install-BcContainerApp -containerName $containerName -appName $appName -Force
}
function Uninstall-DemoDataManagerApp {
    Param(

        [string] $containerName,
        [string] $appName
    )

    UnInstall-BcContainerApp -containerName $containerName -appName $appName
    Sync-BcContainerApp -containerName $containerName -appName $appName-Mode Clean -Force
    UnPublish-BcContainerApp -containerName $containerName -appName $appName -doNotSaveData -force
}

function Test-Preconditions {

    if (!(Test-Path $path)) {

        Throw "Selected Demo Data path is missing! If you left parameter empty, make sure that folder 'DemoData' is present in your working directory."
    }

    if (!((docker ps) -match $containerName)) {

        Throw "Selected container is either not existing or not started."
    }

    if ((Get-CompanyInBcContainer $containerName | Select-Object -ExpandProperty CompanyName) -contains $companyName) {

        Throw "Selected company does not exist."
    }

    if (!(Test-Path $parametersFilePath)) {

        Write-Warning "You did not select any json file with parameters or the path is invalid. Script will use default parameters."
        New-Item -Path $path -Name "parameters.json"
        $parametersFilePath = Join-Path ($path, "parameters.json")
    }
}

function Format-OutputJsonFiles {
    Param(

        [string] $path
    )

    Get-Childitem $path -Recurse | Where-Object { $_.extension -like ".json" } |
    ForEach-Object {
        Get-Content $_.FullName | ConvertFrom-Json | ConvertTo-Json | Set-Content $_.FullName
    }
}
function Backup-CompanyData {
    [CmdletBinding()]
    Param (

        [string]$path = './DemoData',
        [Parameter(Mandatory = $true)]
        [string]$containerName,
        [Parameter(Mandatory = $true)]
        [string]$companyName,
        [string]$parametersFilePath
    )

    #check preconditions
    Test-Preconditions

    #initialize variables
    $appName = "DemoDataManager"
    $appFile = Get-ChildItem (Join-Path ($PSScriptRoot, "SourceApp")) -Depth 2 -Filter "*.app" | Select-Object -ExpandProperty FullName
    $argument = Get-Content -Path $parametersFilePath | Out-String
    $AppInfo = Get-BcContainerAppInfo $ContainerName | Where-Object { $_.Name -eq $appName }

    #chceck for installed app
    if ($AppInfo.AppId -like "7672c184-b8c4-4437-8e22-e6ec5fc5fd53") {
        Uninstall-DemoDataManagerApp -containerName $containerName -appName $appName
    }

    #publish and install DemoDataManager app to the container
    Write-Host 'Publish and install Demo Data Manager App' -ForegroundColor Green
    Install-DemoDataManagerApp -containerName $containerName -appName $appName -appFile $appFile

    #backup all selected data and uninstall DemoDataManager app after backup process
    Write-Host 'Backup Demo data' -ForegroundColor Green
    Invoke-NavContainerCodeunit -containerName $containerName -Codeunitid 90005 -MethodName StartBackupCompanyData -Argument $argument -CompanyName $companyName
    Uninstall-DemoDataManagerApp -appName $appName

    #delete any old backuped files if exist
    Write-Host 'Delete old backup files' -ForegroundColor Green
    Remove-Item (Join-Path -Path $path -ChildPath "*")

    #copy archive file with tables' data to local directory
    Write-Host 'Copy archive file from Container to local folder'
    Copy-FileFromNavContainer -containerName $containerName -containerPath "c:\CompanyBackup.zip" -localPath (Join-Path -Path $path -ChildPath "CompanyBackup.zip")

    #expand archived data
    Write-Host 'Expand backup archive file' -ForegroundColor Green
    Expand-Archive -Path (Join-Path -Path $path -ChildPath "CompanyBackup.zip") -DestinationPath $path -Force

    #delete backupe archive file after being expanded
    Write-Host 'Delete backup archive file' -ForegroundColor Green
    Remove-Item (Join-Path -Path $path -ChildPath "CompanyBackup.zip")

    #format all expanded json files to human readable format
    Write-Host 'Format all json files' -ForegroundColor Green
    Format-OutputJsonFiles -path $path
}

Export-ModuleMember -Function Backup-CompanyData