Advertisement
DataCCIW

Copy Production wwwroot to Azure Storage Powershell Script

Nov 9th, 2022 (edited)
1,265
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: Copy compressed wwwroot to azure storage so that it can be downloaded by dev rock server
  5. #
  6. ###############################################################################################
  7.  
  8. Write-Host -ForegroundColor Yellow "`nRunning Production wwwroot file to Azure Storage Powershell Script"
  9. Write-Host -ForegroundColor Yellow "`nInitializing Variables via Setup Code"
  10.  
  11. #### Setup ####
  12.  
  13. # Azure Storage Configuration
  14. $storageURL = "https://mystrorage.blob.core.windows.net/containername"
  15. # This key will expire on _______
  16. $sharedKey= ""
  17.  
  18. # Set the temp location to store  compressed wwwroot file
  19. $archiveFolder = "C:\Temp\"
  20.  
  21. $inetpubFolder = "C:\inetpub"
  22.  
  23. #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
  24. $compressionLevel = "Fastest"
  25.  
  26. #Year-Month-Day_Hours-Minutes
  27. $dateFormatted = Get-Date -UFormat "%Y%m%d"
  28. #ISO-8601 example 2020-08-19T15:04:00Z
  29. #Get-Date returns local time so we will blank out the time
  30. $dateFormattedISO8601 = Get-Date -UFormat "%Y-%m-%dT00:00:00Z"
  31.  
  32. #if desired we can keep the website up an running when making a copy of the wwwroot folder
  33. $stopWebsite = $false;
  34.  
  35.  
  36. ################################################################################################
  37.  
  38. ### Main Script ###
  39.  
  40. Write-Host -ForegroundColor Yellow "`nStarting Main Script"
  41.  
  42. if ( $stopWebsite )
  43. {
  44. Write-Host -ForegroundColor Yellow "`nStopping Default website and apppool..."
  45. C:\windows\syswow64\inetsrv\appcmd.exe stop site "Default Web Site"
  46. C:\windows\syswow64\inetsrv\appcmd.exe stop apppool "DefaultAppPool"
  47. }
  48.  
  49. Write-Host -ForegroundColor Yellow "`nCompressing wwwroot"
  50. Compress-Archive -Path "$inetpubFolder\wwwroot" -DestinationPath "$archiveFolder\wwwroot_$dateformatted.zip" -CompressionLevel $compressionLevel
  51.  
  52. # Write-Host -ForegroundColor Yellow "Checking Azcopy Login Status"
  53. # Using azcopy login is another option instead of leveraging a shared key below
  54. # Note Even if you are an Azure subscription service administrator you will need additional rights
  55. # to access storage container resources
  56. # https://stackoverflow.com/questions/74377463/how-to-access-azure-storage-account-container-as-azure-subscription-service-admi
  57. #Azcopy login status
  58.  
  59. # $login = Read-Host "Do you need to login to azcopy Y/N"
  60. # if ( $login -eq "Y" )
  61. # {
  62. #    azcopy login
  63. # }
  64.  
  65. Write-Host -ForegroundColor Yellow "`nUploading to Azure, this will only take a few seconds"
  66. #https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-files
  67. azcopy cp "$archiveFolder\*" "$storageURL/$sharedKey" --include-after "$dateFormattedISO8601" --include-pattern '*.zip'
  68.  
  69. Write-Host -ForegroundColor Yellow "`nDeleting compressed wwwroot file"
  70. Remove-Item -path "$archiveFolder\wwwroot*.zip"
  71.  
  72. # if we stopped the website we need to restart it
  73. if ( $stopWebsite )
  74. {
  75. Write-Host -ForegroundColor Yellow "`nStarting default website and apppool..."
  76. C:\windows\syswow64\inetsrv\appcmd.exe start site "Default Web Site"
  77. C:\windows\syswow64\inetsrv\appcmd.exe start apppool "DefaultAppPool"
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement