0
votes

i have write a script to copy one blob container data into another blob container it copying only blob name and showing completed inside VHDs are not copying can any one please help in script,,,

what exactly i am trying is ex:- storage account 1 contain 3 vhds i have created new storage account in the same log and trying to copy all vhds into new storage account In the script i have passed few parameters to copy the vhds with date when i am running that script it only creating name but Vhds are not copying

Thanks in Advance

2

2 Answers

0
votes

According to your description, you want to copy all blobs to another storage accounts, we can use this script to do it:

Add this to a new runbook:

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
$RGName = "vm"
$SAName = "jasondisk321"
$ConName = "vhd"
$key = "UUzQRoWeIMHzwzwJW9LxtgmwaJJS/Ac3DoXnPMHFIbUmupDpQ+KXCWG8ISJ4E20zjq7ugPtgN4vtVIv3A4m2Pg=="
$Ctx = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $Key
$List = Get-AzureStorageBlob -Blob * -Container $ConName -Context $Ctx
$List = $List.ICloudBlob.Uri.AbsoluteUri
$storageAccount = "jasondisk322"
$storageKey = "6lRJq6hTS1aHfVF4/iWskq/QS+tu4Jm/2zdz7Mo6AINGZOQKUiHtOAKmdZhBAWbcNEcBQq0YxZjXHgHha/iUKw=="
$destContext = New-AzureStorageContext  –StorageAccountName $storageAccount -StorageAccountKey $storageKey
$containerName = "vhd"
foreach ( $l in $list ){
$bn = ($l -split '/')[4]
Start-AzureStorageBlobCopy -srcUri $l -context $Ctx -DestContainer $containerName -DestBlob $bn -DestContext $destContext
}

If you want to use powershell on your local PC, we can use this script:

Login-AzureRmAccount
$RGName = "vm"  #source storage account resource group name
$SAName = "jasondisk321" #source storage account name
$ConName = "vhd"  #source container name
$key = "UUzQRoWeIMHzwzwJW9LxtgmwaJJS/Ac3DoXnPMHFIbUmupDpQ+KXCWG8ISJ4E20zjq7ugPtgN4vtVIv3A4m2Pg=="#source storage account key
$Ctx = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $Key
$List = Get-AzureStorageBlob -Blob * -Container $ConName -Context $Ctx
$List = $List.ICloudBlob.Uri.AbsoluteUri
$storageAccount = "jasondisk322"#destination storage account name
$storageKey = "6lRJq6hTS1aHfVF4/iWskq/QS+tu4Jm/2zdz7Mo6AINGZOQKUiHtOAKmdZhBAWbcNEcBQq0YxZjXHgHha/iUKw=="#destination storage account key
$destContext = New-AzureStorageContext  –StorageAccountName $storageAccount -StorageAccountKey $storageKey
$containerName = "vhd"#destination container name
foreach ( $l in $list ){
$bn = ($l -split '/')[4]
Start-AzureStorageBlobCopy -srcUri $l -context $Ctx -DestContainer $containerName -DestBlob $bn -DestContext $destContext
}
0
votes

You can do it with AzCopy:

AzCopy /source:https://[SourceStorageAccountName].blob.core.windows.net/vhds /dest:https://[DestStprageAccountName].blob.core.windows.net/vhds /sourcekey:<here-is-source-key> /destkey:<here-is-destination-key> /Pattern:[vhd-name].vhd

more info: https://docs.microsoft.com/en-us/azure/storage/storage-use-azcopy

If you want to automate, then you can use powershell using Start-AzureStorageBlobCopy:

Select-AzureSubscription "my subscription"  

### Source VHD (West US) - anonymous access container ###
$srcUri = "http://mwwestus1.blob.core.windows.net/source/testcopy1.vhd" 

### Target Storage Account (East US) ###
$storageAccount = "mweastus1"
$storageKey = "STORAGEACCOUNTKEY"

### Create the destination context for authenticating the copy
$destContext = New-AzureStorageContext  –StorageAccountName $storageAccount `
                                        -StorageAccountKey $storageKey  

### Target Container Name
$containerName = "copiedvhds"

### Create the target container in storage
New-AzureStorageContainer -Name $containerName -Context $destContext 

### Start the Asynchronous Copy ###
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
                                    -DestContainer $containerName `
                                    -DestBlob "testcopy1.vhd" `
                                    -DestContext $destContext

more info:https://www.opsgility.com/blog/windows-azure-powershell-reference-guide/copying-vhds-blobs-between-storage-accounts/

UPDATE:

AzCopy /Source:https://myaccount1.blob.core.windows.net/myContainer/ /Dest:https://myaccount2.blob.core.windows.net/myContainer/ /SourceKey:key1 /DestKey:key2 /Pattern:ab /SyncCopy