Currently, we have existing Azure app services and we need to copy the files in one kudu server to another kudu server using a script.
Loading
Currently, we have existing Azure app services and we need to copy the files in one kudu server to another kudu server using a script.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Saravanan GanesanPosted Aug 4, 2023, 8:31 PM
To copy files from one Azure App Service (Kudu server) to another Azure App Service (Kudu server) using a script, you can use the Azure CLI along with the Kudu REST API. The Kudu REST API allows you to interact with the file system of your App Service, enabling you to copy files between different instances.
Below is a PowerShell script that demonstrates how to achieve this:
# Replace these with your actual Azure App Service details
$sourceAppName = "source-app-name"
$sourceResourceGroup = "source-resource-group"
$sourceSlotName = "production" # If using deployment slots, specify the source slot name
$destinationAppName = "destination-app-name"
$destinationResourceGroup = "destination-resource-group"
$destinationSlotName = "production" # If using deployment slots, specify the destination slot name
# Authenticate with Azure
az login
# Get the source Kudu URL
$sourceKuduUrl = az webapp deployment list-publishing-profiles --name $sourceAppName --resource-group $sourceResourceGroup --query "[?publishMethod=='MSDeploy'].publishUrl" -o tsv
# Get the destination Kudu URL
$destinationKuduUrl = az webapp deployment list-publishing-profiles --name $destinationAppName --resource-group $destinationResourceGroup --query "[?publishMethod=='MSDeploy'].publishUrl" -o tsv
# Source Kudu REST API endpoint to list files
$sourceKuduFilesUrl = "$sourceKuduUrl/api/vfs/site/wwwroot"
# Destination Kudu REST API endpoint to upload files
$destinationKuduFilesUrl = "$destinationKuduUrl/api/zip/site/wwwroot"
# Invoke the Kudu REST API to get the list of files from the source
$sourceFiles = Invoke-RestMethod -Uri $sourceKuduFilesUrl -Headers @{"Authorization"="Basic $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username_goes_here:password_goes_here")))"}
# Loop through the files and copy them to the destination
foreach ($file in $sourceFiles) {
$sourceFileUrl = "$sourceKuduFilesUrl/$file"
$destinationFileUrl = "$destinationKuduFilesUrl/$file"
Invoke-RestMethod -Uri $sourceFileUrl -Headers @{"Authorization"="Basic $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username_goes_here:password_goes_here")))" -Method Get -OutFile $file
Invoke-RestMethod -Uri $destinationFileUrl -Headers @{"Authorization"="Basic $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username_goes_here:password_goes_here")))" -Method Post -InFile $file
Remove-Item $file
}
Please replace the placeholders
source-app-name,source-resource-group,production,destination-app-name, anddestination-resource-groupwith your actual App Service names and resource group names. Also, make sure to provide the necessary credentials for the Kudu REST API authentication.The script first retrieves the source and destination Kudu URLs using the Azure CLI. Then, it invokes the Kudu REST API to list files from the source and copy them to the destination using a loop.
Keep in mind that this script does a one-time file copy from the source to the destination. If you need continuous synchronization or a more complex deployment process, you may need to consider other deployment tools or CI/CD pipelines.
Janarthanan SPosted Jun 2, 2023, 2:27 AM
$sourceUsername = "source-kudu-username"
$sourcePassword = "source-kudu-password"
$sourceSiteName = "source-app-service-name"
$destinationUsername = "destination-kudu-username"
$destinationPassword = "destination-kudu-password"
$destinationSiteName = "destination-app-service-name"
$sourceKuduApiUrl = "https://$sourceSiteName.scm.azurewebsites.net/api/vfs/"
$destinationKuduApiUrl = "https://$destinationSiteName.scm.azurewebsites.net/api/vfs/"
function Copy-Files($sourceUrl, $sourceCredentials, $destinationUrl, $destinationCredentials) {
$files = Invoke-RestMethod -Uri $sourceUrl -Credential $sourceCredentials -Method GET
foreach ($file in $files) {
$filePath = $file.href
$fileUrl = $sourceUrl + $filePath
$destinationFileUrl = $destinationUrl + $filePath
$fileContent = Invoke-RestMethod -Uri $fileUrl -Credential $sourceCredentials -Method GET -ContentType "application/octet-stream"
Invoke-RestMethod -Uri $destinationFileUrl -Credential $destinationCredentials -Method PUT -Body $fileContent -ContentType "application/octet-stream"
}
}
$sourceCredentials = New-Object System.Management.Automation.PSCredential($sourceUsername, (ConvertTo-SecureString -String $sourcePassword -AsPlainText -Force))
$destinationCredentials = New-Object System.Management.Automation.PSCredential($destinationUsername, (ConvertTo-SecureString -String $destinationPassword -AsPlainText -Force))
Copy-Files -sourceUrl $sourceKuduApiUrl -sourceCredentials $sourceCredentials -destinationUrl $destinationKuduApiUrl -destinationCredentials $destinationCredentials
Replace the following placeholders in the script:
source-kudu-usernameandsource-kudu-passwordwith the credentials for the source Kudu instance.source-app-service-namewith the name of the source App Service instance.destination-kudu-usernameanddestination-kudu-passwordwith the credentials for the destination Kudu instance.destination-app-service-namewith the name of the destination App Service instance.Run the script, and it will copy all the files from the source Kudu instance to the destination Kudu instance.
Rajkiran SwainPosted Jun 1, 2023, 3:53 PM
Mohamed Azarudeen ZPosted Jun 1, 2023, 3:09 PM
To copy files between two Kudu servers in Azure App Service, you can use the Kudu REST API along with a scripting language like PowerShell or Bash. Here's a high-level overview of the steps involved:
1. Obtain the necessary credentials:
- For the source Kudu server: Get the username and password for authentication.
- For the destination Kudu server: Get the username and password for authentication.
2. Choose a scripting language:
- PowerShell: This is the recommended option for Windows-based environments.
- Bash: This is the recommended option for Linux-based environments.
3. Write the script:
- PowerShell: You can use the Invoke-RestMethod cmdlet to make HTTP requests to the Kudu REST API.
- Bash: You can use the curl command to make HTTP requests to the Kudu REST API.
4. Authenticate with the Kudu servers:
- Include the credentials for both the source and destination Kudu servers in your script. This will involve sending the authentication headers in the HTTP requests.
5. Use the Kudu REST API to copy files:
- Obtain the list of files from the source Kudu server using the `/api/vfs/{path}` endpoint.
- Iterate through the list of files and copy each file using the `/api/vfs/{path}` endpoint with appropriate HTTP methods (e.g., GET for reading the file, PUT for writing the file) to the destination Kudu server.
Here's an example PowerShell script that demonstrates how to copy files between two Kudu servers:
```powershell
# Credentials for the source Kudu server
$sourceUsername = "source-username"
$sourcePassword = "source-password"
$sourceKuduUrl = "https://source-appname.scm.azurewebsites.net"
# Credentials for the destination Kudu server
$destinationUsername = "destination-username"
$destinationPassword = "destination-password"
$destinationKuduUrl = "https://destination-appname.scm.azurewebsites.net"
# Function to copy files between Kudu servers
function Copy-Files($sourcePath, $destinationPath) {
$sourceUrl = "$sourceKuduUrl/api/vfs/$sourcePath"
$destinationUrl = "$destinationKuduUrl/api/vfs/$destinationPath"
# Get the file content from the source
$fileContent = Invoke-RestMethod -Uri $sourceUrl -Method Get -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sourceUsername, $sourcePassword)
# Put the file content to the destination
Invoke-RestMethod -Uri $destinationUrl -Method Put -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $destinationUsername, $destinationPassword) -ContentType "application/octet-stream" -Body $fileContent
}
# Copy files from source to destination
Copy-Files "source-path/file.txt" "destination-path/file.txt"
```
Please make sure to replace `source-username`, `source-password`, `source-appname`, `destination-username`, `destination-password`, `destination-appname`, `source-path/file.txt`, and `destination-path/file.txt` with the appropriate values for your scenario.
Remember to execute the script from a PowerShell environment where you have the required permissions to access the Kudu servers.
You can adapt this example to Bash if you are working in a Linux-based environment.
Note: The Kudu REST API endpoints mentioned in the script are subject to change. It's always a good idea to refer to the official documentation for the most up-to-date information on the Kudu REST API.