How to compress wwwroot folder of azure webapp

MoTaar 310 Reputation points
2024-01-02T22:46:51.89+00:00

How to compress wwwroot folder of azure webapp with a powershell script so that I can download it as a file using sftp script?

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,937 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,742 questions
0 comments No comments
{count} votes

Accepted answer
  1. VenkateshDodda-MSFT 24,766 Reputation points Microsoft Employee Moderator
    2024-01-03T07:20:41.07+00:00

    @MoTaar Thank you for reaching out to Microsoft Q&A, apologize for any inconvenience caused on this.

    Hope you have seen the response from Udaiappa Ramachandran and helped. Adding to that if you want to use service principal-based credentials instead of publishing profile credentials.

    You can refer to the PowerShell script mentioned in this blog post which uses service principal credentials +Kudu Rest Api to download the files under site/wwwroot folder contents as zip folder.

    Feel free to reach back to me if you have any further questions on this.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Udaiappa Ramachandran 726 Reputation points MVP
    2024-01-03T06:09:48.7+00:00

    Hi @MoTaar

    You can use the Azure App Services Backup/Restore to backup your site as zip files

    https://learn.microsoft.com/en-us/azure/app-service/manage-backup?WT.mc_id=AZ-MVP-5004665

    If you still need to use the script, then you can use something like the below:

    # Ensure you have the Azure PowerShell module installed and imported
    Import-Module Az
    
    # Variables
    $resourceGroupName = "<YourResourceGroupName>"
    $webAppName = "<YourWebAppName>"
    $zipFileName = "$webAppName-$(Get-Date -Format 'yyyyMMddHHmm').zip"
    $destinationPath = "<YourLocalBackupPath>\" + $zipFileName
    
    # Authenticate with Azure if not already logged in
    # Connect-AzAccount
    
    # Get the publishing profile for the Azure Web App
    $publishingProfile = Get-AzWebAppPublishingProfile -ResourceGroupName $resourceGroupName -Name $webAppName
    
    # Extracting deployment credentials from the publishing profile
    $username = $publishingProfile.Properties.PublishingUserName
    $password = $publishingProfile.Properties.PublishingPassword
    
    # Define the base64 authorization for the header
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    
    # Define the Kudu API zip endpoint
    $apiUrl = "https://$webAppName.scm.azurewebsites.net/api/zip/site/wwwroot/"
    
    # Make the web request to the Kudu API to get the zip file
    Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET -OutFile $destinationPath
    
    Write-Host "Backup of $webAppName completed successfully. File saved to $destinationPath"
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.