Skip to content

Instantly share code, notes, and snippets.

@andrew-kelleher
Last active April 23, 2024 12:20
Show Gist options
  • Save andrew-kelleher/031ff5daa8849a9a9c335271f0b3bc08 to your computer and use it in GitHub Desktop.
Save andrew-kelleher/031ff5daa8849a9a9c335271f0b3bc08 to your computer and use it in GitHub Desktop.
Azure Function App PowerShell script to backup Blob storage to an Azure File share
# Input bindings are passed in via param block.
param($Timer)
# Define variables
$SrcStgAccURI = "https://sourceblobstg.blob.core.windows.net/"
$SrcBlobContainer = "myblobs"
$SrcSASToken = SAS_TOKEN_SOURCE
$SrcFullPath = "$($SrcStgAccURI)$($SrcBlobContainer)?$($SrcSASToken)"
$DstStgAccURI = "https://destinationfilestg.file.core.windows.net/"
$DstFileShare = "backupstaging"
$DstSASToken = SAS_TOKEN_DESTINATION
$DstFullPath = "$($DstStgAccURI)$($DstFileShare)?$($DstSASToken)"
# Get the current universal time in the default string format
$currentUTCtime = (Get-Date).ToUniversalTime()
# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"
# uncomment to delete azcopy and force download again
# del azcopy.exe
# Test if AzCopy.exe exists in current folder
$WantFile = "azcopy.exe"
$AzCopyExists = Test-Path $WantFile
Write-Host "AzCopy exists:" $AzCopyExists
# Download AzCopy if it doesn't exist
If ($AzCopyExists -eq $False)
{
Write-Host "AzCopy not found. Downloading..."
#Download AzCopy
Invoke-WebRequest -Uri "https://aka.ms/downloadazcopy-v10-windows" -OutFile AzCopy.zip -UseBasicParsing
#Expand Archive
write-host "Expanding archive..."
Expand-Archive ./AzCopy.zip ./AzCopy -Force
# Copy AzCopy to current dir
Get-ChildItem ./AzCopy/*/azcopy.exe | Copy-Item -Destination "./AzCopy.exe"
}
else
{
Write-Host "AzCopy found, skipping download."
}
# Set env values for AzCopy
$env:AZCOPY_LOG_LOCATION = $env:temp+'\.azcopy'
$env:AZCOPY_JOB_PLAN_LOCATION = $env:temp+'\.azcopy'
# Run AzCopy from source blob to destination file share
Write-Host "Backing up storage account..."
./azcopy.exe copy $SrcFullPath $DstFullPath --recursive --overwrite=ifsourcenewer
@GuetarniTarik
Copy link

at the end it worked for me by adding azcopy.exe directly in the package. I suggest you to do the same

@stevenjpbrown
Copy link

I did do that but couldn’t get it working. It didn’t like the path to the log files. It was changing the slash on a windows function app to / rather than . Did you use Linux or windows?

@sojha05
Copy link

sojha05 commented Jan 23, 2023

@GuetarniTarik - whats the way to integrate azcopy.exe with the package folder?

@San230897
Copy link

Hello Andrew,

I tried to execute this script, but it is not working for me, can you pass on any documentation if it is handy with you.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment