Thursday, June 3, 2021

Using Azure cache for Redis continued ...

 Introduction: This article describes the two approaches to transfering data between redis servers.

param (

    [Parameter(Mandatory=$true)][string]$environment,

    [Parameter(Mandatory=$true)][string]$subscriptionId,

    [Parameter(Mandatory=$true)][string]$resourceGroupName,

    [Parameter(Mandatory=$true)][string]$storageAccount

)

Write-Host Script begin

Write-Host Connecting to Azure

Connect-AzAccount -Environment $environment -ErrorAction Stop

Write-Host Connected to Azure

 

Write-Host Setting context to Subscription: $subscriptionId

Set-AzContext -subscriptionId $subscriptionId -ErrorAction Stop

Write-Host Context switched to Subscription: $subscriptionId

 

$caches = Get-AzRedisCache -ResourceGroupName $resourceGroupName

foreach ($cache in $caches) {

   if ($cache.Zone -eq $null) {

       Write-Host Migrating $cache.Name to be Az ready

       $zones = @()

       $zones += "1"

       $zones += "2"

       $zones += "3"

 

       if ($cache.RedisConfiguration -eq $null) {

           $cache.RedisConfiguration = @{}

       }

 

       $cache.RedisConfiguration["rdb-backup-enabled"] = $False

       $newCacheName = $cache.Name+"-AzReady"

       $sku = "Premium"

 

       New-AzRedisCache `

          -ResourceGroupName $resourceGroupName `

          -Name $newCacheName `

          -Location $cache.Location `

          -Size $cache.Size   `

          -Sku $sku   `

          -Zone $zones  `

          -RedisConfiguration $cache.RedisConfiguration `

          -ErrorAction Stop

 

        $newCache = Get-AzRedisCache -ResourceGroupName $resourceGroupName -Name $newCacheName

        while (-not ($newCache.ProvisioningState -eq "Succeeded")) {

            Write-Host "Sleeping 5 seconds for new cache to be provisioned."

            Start-Sleep -Seconds 5

            $newCache = Get-AzRedisCache -ResourceGroupName $resourceGroupName -Name $newCacheName

        }

 

        <# We have an AZ redundant server now, it does not have the same data yet.

        # The link based replication would be fast but it does not work with AZ redundant Redis server.

        New-AzRedisCacheLink `

          -PrimaryServerName $cache.Name  `

                    -SecondaryServerName $newCacheName  `

          -ErrorAction Stop

      

        Remove-AzRedisCacheLink `

          -PrimaryServerName $cache.Name `

          -SecondaryServerName $newCacheName `

          -ErrorAction Stop

        #>

 

        <#

         # Instead we import and export data

         #>

         Export-AzRedisCache `

         -ResourceGroupName $resourceGroupName `

         -Name $oldCache.Name -Prefix "export-" `

         -Container $storageAccount

 

         Import-AzRedisCache `

         -ResourceGroupName $resourceGroupName `

         -Name $newCacheName -Files @($storageAccount) -Force

 

         Write-Host New cache $newCacheName is AZ-Ready and replicated from existing.

   }

}

Write-Host Script end


No comments:

Post a Comment