transformed to a function

This commit is contained in:
2025-03-19 16:16:12 +01:00
parent 03dfeb47e1
commit f478d2adea

View File

@@ -1,14 +1,52 @@
<#
.SYNOPSIS
Push a superset from prodDB to the BAG Azure managed sql instance
.DESCRIPTION
This function will do those operations:
- Check if the superset given is a snapshot or a database
if it's a snapshot, the source database behind the snapshot is backed up
- Authenticate to Azur using a service principal (with a certificate authentication)
- Create a SAS tocken from Azure Blob Storage
- Refresh credentials on prodDB to access the storage account
- Check if a blob of the superset we want to push is more than 1 day old
- if no blob exists, force a backup
- if a blob exists but is aged of less than 1 full day, skip the backup
- if a blob exists but is older than 1 full day, force a new backup
- Refresh the credential with the SAS token on the BAG managed sql instance
- Drop the target database if it exists (we cannot restore over an existing db in managed instances)
- Restore the backup in the cloud db
- Create logins on the restored db for the login [sql-au_bag_apv]
- Give db_datareader and EXECUTE permission on the restored db to [sql-au_bag_apv]
.PARAMETERS
[string] $supersetToCopy
The name of the superset to transfert. for exemple: product_superset
.EXAMPLES
push-superset -supersetToCopy Artikel_History_Superset
#>
function push-superset([string] $supersetToCopy){
# Variables
$tenantId = "7844775a-a9cc-4c33-a5ae-36dcf6660f45" #Galenica
$clientId = "d28076dd-2108-4718-802e-cd3c35fd5505" #pcpl-BAGSpezListePrd-DBBackup
$supersetToCopy="product_superset"
$supersetToCopy="Artikel_History_Superset"
$skipBackup = "auto" #auto = check age and skip if backup is less than 1 day old, $true to force skip backup, $false to force backup
<#
backup product_superset__0 : 00:13:00
restore product_superset : 00:05:40
#>
write-info "Starting push of $supersetToCopy to the cloud"
##do not alter below
$serverInstance = "SWMDATASQLPRD01.centralinfra.net"
$databaseName = $null
$storageAccountName = "stbagspezlisteprdsql"
$containerName = "sqlbakfiles"
$resourceGroupName="rg-BAGSpezListePrd-SQL"
$vaultName="kv-BAGSpezListePrd-bkp"
$secretName="superuser"
$blobPermissions="rwd"
$expiryTime = (Get-Date).AddHours(1) # SAS token valid for 1 hour
$backupFileName = "$supersetToCopy.bak"
$mod = Get-Module -Name Az.Accounts
if($null -eq $mod){
@@ -26,18 +64,6 @@ if($null -eq $mod){
}
##do not alter below
$serverInstance = "SWMDATASQLPRD01.centralinfra.net"
$databaseName = $null
$storageAccountName = "stbagspezlisteprdsql"
$containerName = "sqlbakfiles"
$resourceGroupName="rg-BAGSpezListePrd-SQL"
$vaultName="kv-BAGSpezListePrd-bkp"
$secretName="superuser"
$blobPermissions="rwd"
$expiryTime = (Get-Date).AddHours(1) # SAS token valid for 1 hour
$backupFileName = "$supersetToCopy.bak"
#Managed instance related
$MILogin='superuser'
$MIPwd=$null #fetched from key vault later
@@ -202,3 +228,14 @@ GRANT EXECUTE TO [sql-au_bag_apv];
"
Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlPerms -Credential $credentialMI -Database $supersetToCopy
write-output "Added user [sql-au_bag_apv] with read and execute permissions to $supersetToCopy"
}
<#
backup product_superset__0 : 00:13:00
restore product_superset : 00:05:40
#>
#push-superset -supersetToCopy sl2007_superset
push-superset -supersetToCopy Artikel_History_Superset
#push-superset -supersetToCopy product_superset