Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This PowerShell script performs a copy between two storage accounts using AzCopy.
# Define variables
$SrcStgAccURI = "https://$"
$SrcBlobContainer = "$sourceFolder"
$SrcSASToken = "$sourceSasToken"
$SrcFullPath = "$($SrcStgAccURI)$($SrcBlobContainer)?$($SrcSASToken)"
$DstStgAccURI = "https://$"
if ($triggerPeriod -eq 'daily')
$DstFileShare = "daily/$(Get-Date -format yyyyMMdd)/$targetFolder"
$DstFileShare = "weekly/$(Get-Date -format yyyyMMdd)/$targetFolder"
$DstSASToken = "$targetSasToken"
$DstFullPath = "$($DstStgAccURI)$($DstFileShare)?$($DstSASToken)"
if ($triggerPeriod -eq 'daily')
$IncludeAfterDateTimeISOString = (Get-Date).AddHours(-25).ToString("o") # One hour overlap with the previous daily run
Write-Output "Initializing backup process"
Write-Output "Source: $sourceFolder"
Write-Output "Target: $DstFileShare"
Write-Output "Trigger period: $triggerPeriod `n"
if ($triggerPeriod -eq 'daily')
Write-Output ("Trigger period: {0}" -f $IncludeAfterDateTimeISOString)
# Test if AzCopy.exe exists in current folder
$WantFile = "azcopy.exe"
$AzCopyExists = Test-Path $WantFile
Write-Output ("AzCopy exists: {0}" -f $AzCopyExists)
# Download AzCopy if it doesn't exist
If ($AzCopyExists -eq $False)
Write-Output "AzCopy not found. Downloading..."
#Download AzCopy
Invoke-WebRequest -Uri "" -OutFile -UseBasicParsing
#Expand Archive
Write-Output "Expanding archive...`n"
Expand-Archive ./ ./AzCopy -Force
# Copy AzCopy to current dir
Get-ChildItem ./AzCopy/*/azcopy.exe | Copy-Item -Destination "./azcopy.exe"
Write-Output "AzCopy found, skipping download.`n"
$env:AZCOPY_CONCURRENCY_VALUE = $azCopyConcurrency
# Run AzCopy from source blob to destination file share
Write-Host "Backing up storage account..."
$stopLoop = $false
$retryCount = 0
do {
Write-Host "Attempt: $retryCount"
if ($triggerPeriod -eq 'daily')
Write-Host ("./azcopy.exe copy $SrcFullPath $DstFullPath --block-blob-tier Cool --recursive --overwrite=ifsourcenewer --log-level=NONE --include-after $IncludeAfterDateTimeISOString`n")
./azcopy.exe copy $SrcFullPath $DstFullPath --block-blob-tier Cool --recursive --overwrite=ifsourcenewer --log-level=NONE --include-after $IncludeAfterDateTimeISOString
Write-Host ("./azcopy.exe copy $SrcFullPath $DstFullPath --block-blob-tier Cool --recursive --overwrite=ifsourcenewer --log-level=NONE`n")
./azcopy.exe copy $SrcFullPath $DstFullPath --block-blob-tier Cool --recursive --overwrite=ifsourcenewer --log-level=NONE
if ($LASTEXITCODE -ne 0) {
elseif ($LASTEXITCODE -eq 0){
$stopLoop = $true
if ($retryCount -eq 3) {
throw "Failed after $retryCount attempts"
While ($stopLoop -eq $false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment