diff --git a/HCI - BAG symmetricDS/hci_bag_backup.ps1 b/HCI - BAG symmetricDS/hci_bag_backup.ps1 index 6289d3d..72deeec 100644 --- a/HCI - BAG symmetricDS/hci_bag_backup.ps1 +++ b/HCI - BAG symmetricDS/hci_bag_backup.ps1 @@ -263,7 +263,7 @@ enum backupBehavior{ restore product_superset : 00:05:40 #> - #push-superset -supersetToCopy sl2007_superset -backupAutoBehavior Auto + push-superset -supersetToCopy sl2007_superset -backupAutoBehavior Auto push-superset -supersetToCopy Artikel_History_Superset -backupAutoBehavior Auto - #push-superset -supersetToCopy product_superset -backupAutoBehavior Auto + push-superset -supersetToCopy product_superset -backupAutoBehavior Auto \ No newline at end of file diff --git a/HCI - BAG symmetricDS/proddb to mi backup.ps1 b/HCI - BAG symmetricDS/proddb to mi backup.ps1 index ae08bc9..dfbd99e 100644 --- a/HCI - BAG symmetricDS/proddb to mi backup.ps1 +++ b/HCI - BAG symmetricDS/proddb to mi backup.ps1 @@ -8,7 +8,38 @@ function log-out{ ) Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") :: $msg" } - + + +<#1 +.SYNOPSIS +Check if a db with the specified name exists on the managed instant + +.PARAMETER +[string] $superset +The name of the database to check if it exists on the managed instance + +#> +function find-db-exists-MI{ + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [string] $superset + ) + $ret = $false + + $sql_check=" +select 1 as ret +from sys.databases d +where name = '$superset' + " + $check_result = Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sql_check -Credential $credentialMI + if($check_result -and $check_result["ret"] -eq "1"){ + $ret = $true + } + + return $ret +} + <# .SYNOPSIS Push a superset from prodDB to the BAG Azure managed sql instance @@ -343,30 +374,47 @@ END } - #restore superset - $url=$backupFile - $sqlRestore="RESTORE DATABASE [$supersetToCopy] FROM URL = N'$url'; " - $retryCount = 0 - $success = $false - while (-not $success -and $retryCount -lt $maxRetries) { - try{ - Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlRestore -Credential $credentialMI - log-out -msg "Restored $supersetToCopy" - $success=$true + #restore superset if not found on the MI instance + $db_retry_count = 0 + $db_retry_max_count = 5 + $db_exists = find-db-exists-MI -superset $supersetToCopy + + while(($db_exists -eq $false) -and ($db_retry_count -lt $db_retry_max_count) ){ + $url=$backupFile + $sqlRestore="RESTORE DATABASE [$supersetToCopy] FROM URL = N'$url'; " + $retryCount = 0 + $success = $false + while (-not $success -and $retryCount -lt $maxRetries) { + try{ + Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlRestore -Credential $credentialMI + log-out -msg "Restored $supersetToCopy" + $success=$true + } + catch{ + $retryCount++ + Start-Sleep -Seconds $retryInterval + } } - catch{ - $retryCount++ + + if(-not $success){ + write-error "Error restoring db $supersetToCopy" + exit 1 + } + + $db_retry_count += 1 + $db_exists = find-db-exists-MI -superset $supersetToCopy + + if(-not($db_exists)){ Start-Sleep -Seconds $retryInterval } } - if(-not $success){ - write-error "Error restoring db $supersetToCopy" + #check that after all tries, we have a db + if(-not(find-db-exists-MI -superset $supersetToCopy)){ + Write-Error "Superset $supersetToCopy is not present after restore !" exit 1 } - - #create user for sql-au_bag_apv $sqlUser="