Last updated on April 20th, 2021
In this post, I want to present an easy way to copy data between different Azure Storage account (or other location). It doesn’t matter what region and subscription Azure Storage Accounts are in.
First, if you copy data between two Storage Accounts, generate SAS-Tokens in the source and destination Storage Accounts:
- We set ‘allowed service’ and ‘allowed resource type’ depending on what resources we want to copy.
- We only add read access to the source Storage account.
- We only add write and create rights to the target Storage account.
- We set the date when the token is to be valid, the time is best set 30-60 minutes before the planned operation because it may not work. It is important to pay attention to the time zone.
- It is also good to define the allowed IP range.
- We only allow HTTPS.
- At the end, we choose the key on the basis of which the token will be created and we can click ‘Generate SAS and connection string’
We repeat the same steps in the second Storage account, paying attention to give the appropriate permissions.
When we have generated tokens, we can log in to CLI:
After authentication, run the following command:
azcopy cp "<a href="https://source_storage_account.file.core.windows.net/SKOPIOWANY_TOKEN">https://source_storage_account.file.core.windows.net/SKOPIOWANY_TOKEN</a>" "<a href="https://destination_storage_account.file.core.windows.net/%20SKOPIOWANY_TOKEN%20">https://destination_storage_account.file.core.windows.net/ SKOPIOWANY_TOKEN </a>" --recursive=true
The block notation of the command looks like this:
azcopy cp [source] [destination] [flags]
Turning on the –overwrite = true –recursive = true flags will allow you to overwrite and copy subfolders.
Important information and a bit sad that if a folder is empty it will not be copied.
If we already have copied files and we just want to sync them then we use “sync” and not “cp“.
The synchronization can be added to a cron, thanks to which we will keep the current version of the files in 2 places.
The source / destination does not have to be of the same type, we can copy files to us locally on the computer / server. Or consider the Storage account as a backup of our local files that is called from cron from time to time:
azcopy cp "C:\local\path" "https://destination_storage_account.file.core.windows.net/mycontainer1/ SKOPIOWANY_TOKEN " --recursive=true
More information can be found on the microsoft website. If you are interested in this article, check out the others on Microsoft Azure category https://lepczynski.it/en/category/azure_en/