Advertisement
DataCCIW

Copy Azure Storage wwwroot to Dev VM Powershell Script

Nov 9th, 2022
1,915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###############################################################################################
  2. # Author: Tony Visconti
  3. # Date Created: 11-4-22
  4. # Purpose: Backup existing wwwroot folder, Copy Azure Storage wwwroot zip to Dev VM and unzip
  5. ###############################################################################################
  6.  
  7. Write-Host -ForegroundColor Yellow "`nRunning Copy Azure Storage wwwroot zip to Dev Server Powershell Script"
  8. Write-Host -ForegroundColor Yellow "`nInitializing Variables via Setup Code"
  9. #### Setup ####
  10.  
  11. # Azure Storage Configuration
  12. $storageURL = "https://mystorage.blob.core.windows.net/containername"
  13. # This key will expire on 11/3/23
  14. $sharedKey= ""
  15.  
  16. # Set the location to store wwwroot compressed backups
  17. # Note: You will need to create the archive folder if it does not already exist
  18. $archiveFolder = "C:\wwwroot_backups"
  19. #Default compression value is optimal, this did not significantly increase the size in my testing but was faster by about 1 min compressing 1.5 G
  20. $compressionLevel = "Fastest"
  21.  
  22. $inetpubFolder = "C:\inetpub"
  23.  
  24. #Year-Month-Day_Hours-Minutes
  25. $dateFormatted = Get-Date -UFormat "%Y%m%d"
  26. #ISO-8601 example 2020-08-19T15:04:00Z
  27. #Get-Date returns local time so we will blank out the time
  28. $newDBName = "rock-dev-sqldb-prod-copy-$dateFormatted"
  29.  
  30. ################################################################################################
  31.  
  32. ### Main Script ###
  33.  
  34. Write-Host -ForegroundColor Yellow "`nStarting Main Script"
  35.  
  36. Write-Host -ForegroundColor Yellow "Stopping Default website and apppool..."
  37. C:\windows\syswow64\inetsrv\appcmd.exe stop site "Default Web Site"
  38. C:\windows\syswow64\inetsrv\appcmd.exe stop apppool "DefaultAppPool"
  39.  
  40. Write-Host -ForegroundColor Yellow "`nCompressing wwwroot"
  41. Compress-Archive -Path "$inetpubFolder\wwwroot" -DestinationPath "$archiveFolder\wwwroot_$dateformatted.zip" -CompressionLevel $compressionLevel
  42.  
  43. Write-Host -ForegroundColor Yellow "`nDeleting wwwroot file"
  44. #-force is needed to delete some files like thumbs.db
  45. Remove-Item -path "$inetpubFolder\wwwroot\" -Recurse -force
  46.  
  47. Write-Host -ForegroundColor Yellow "`nDownload wwwroot from Azure, this will only take a few seconds"
  48. #https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-files
  49. azcopy cp "$storageURL/wwwroot_$dateformatted.zip$sharedKey" $inetpubFolder
  50.  
  51. Write-Host -ForegroundColor Yellow "`nExpanding wwwroot zip"
  52. #https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive?view=powershell-7.2
  53. Expand-Archive -Path "$inetpubFolder\wwwroot_$dateformatted.zip" -DestinationPath $inetpubFolder
  54.  
  55. Write-Host -ForegroundColor Yellow "`nRemoving wwwroot zip"
  56. Remove-Item -path "$inetpubFolder\wwwroot_$dateformatted.zip" -Recurse -force
  57.  
  58. #If desired you could script the deletion of the zip from azure storage
  59.  
  60. #Update connection string
  61. #https://learn.microsoft.com/en-us/powershell/module/webadministration/set-webconfigurationproperty?view=windowsserver2022-ps
  62. Set-WebConfigurationProperty -pspath "IIS:\sites\Default Web Site"  "connectionStrings/add[@name='RockContext']" -name "connectionString" -value "Server=tcp:sqlservername.database.windows.net,1433;Database=$newDBName;MultipleActiveResultSets=true;User ID=;Password="
  63.  
  64. Write-Host -ForegroundColor Yellow "`nStarting Default website and apppool..."
  65. C:\windows\syswow64\inetsrv\appcmd.exe start site "Default Web Site"
  66. C:\windows\syswow64\inetsrv\appcmd.exe start apppool "DefaultAppPool"
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement