Skip to content

Instantly share code, notes, and snippets.

@rupertbenbrook
Created September 28, 2016 20:17
Show Gist options
  • Save rupertbenbrook/11ae71cf0323a55ea6647c6e67324a0c to your computer and use it in GitHub Desktop.
Save rupertbenbrook/11ae71cf0323a55ea6647c6e67324a0c to your computer and use it in GitHub Desktop.
A demo of using AzCopy to create backups of Azure files shares into blob storage, and restoring them again
#############################################################
# SET UP DEMO STORAGE
#############################################################
# Parameters
$prefix = "rbdemo" # CHANGE THIS!
$rgName = $prefix + "backup"
$location = "northeurope"
$filesStorage = $prefix + "files"
$filesStorageSku = "Standard_LRS" # Use LRS for file share
$filesShareName = "share"
$backupStorage = $prefix + "backup"
$backupStorageSku = "Standard_RAGRS" # USe RAGRS for backup to enable restores from another region
$restoreStorage = $prefix + "restore"
$restoreStorageSku = "Standard_LRS" # Use LRS for file share
$restoreShareName = "share"
Write-Output "Check resource group exists and create if not"
$rg = Get-AzureRmResourceGroup -Name $rgName -ErrorAction SilentlyContinue
if ($rg -eq $null) {
$rg = New-AzureRmResourceGroup -Name $rgName -Location $location
}
Write-Output "Check files storage exists and create if not"
$files = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $filesStorage -ErrorAction SilentlyContinue
if ($files -eq $null) {
$files = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $filesStorage -Location $location -SkuName $filesStorageSku -Kind Storage
}
Write-Output "Get context for file storage"
$filesKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $rgName -Name $filesStorage)[0].Value
$filesContext = New-AzureStorageContext -StorageAccountName $filesStorage -StorageAccountKey $filesKey
Write-Output "Check if files share exists and create if not"
$share = Get-AzureStorageShare -Context $filesContext -Name $filesShareName -ErrorAction SilentlyContinue
if ($share -eq $null) {
$share = New-AzureStorageShare -Context $filesContext -Name $filesShareName
}
Write-Output "Check if backup storage exists and create if not"
$backup = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $backupStorage -ErrorAction SilentlyContinue
if ($backup -eq $null) {
$backup = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $backupStorage -Location $location -SkuName $backupStorageSku -Kind Storage
}
Write-Output "Get context for backup storage"
$backupKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $rgName -Name $backupStorage)[0].Value
$backupContext = New-AzureStorageContext -StorageAccountName $backupStorage -StorageAccountKey $backupKey
Write-Output "Check restore storage exists and create if not"
$restore = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $restoreStorage -ErrorAction SilentlyContinue
if ($restore -eq $null) {
$restore = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $restoreStorage -Location $location -SkuName $restoreStorageSku -Kind Storage
}
Write-Output "Get context for restore storage"
$restoreKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $rgName -Name $restoreStorage)[0].Value
$restoreContext = New-AzureStorageContext -StorageAccountName $restoreStorage -StorageAccountKey $restoreKey
Write-Output "Check if restore share exists and create if not"
$restoreShare = Get-AzureStorageShare -Context $restoreContext -Name $restoreShareName -ErrorAction SilentlyContinue
if ($restoreShare -eq $null) {
$restoreShare = New-AzureStorageShare -Context $restoreContext -Name $restoreShareName
}
#############################################################
# SET UP DEMO CONTENT ON FILE SHARE
#############################################################
Write-Output "Put some files in the file share"
$root = "${env:SystemRoot}\system32"
Get-ChildItem -Path "$root\a*.exe" -Recurse -ErrorAction SilentlyContinue | %{
$path = $_.Directory.FullName.Substring($root.Length)
if ($path.Length -gt 0) {
$dir = New-AzureStorageDirectory -Context $filesContext -ShareName $filesShareName -Path $path -ErrorAction SilentlyContinue
}
Set-AzureStorageFileContent -Context $filesContext -ShareName $filesShareName -Source $_.FullName -Path "$path\$($_.Name)" -ConcurrentTaskCount 10 -Force
}
#############################################################
# DEMO BACKING UP FILES INTO STORAGE USING AZCOPY
#############################################################
Write-Output "Create a storage container for the backup named with a timestamp"
$timestamp = (Get-Date).ToString("yyyyMMddHHmm")
$container = New-AzureStorageContainer -Context $backupContext -Name "backup$timestamp"
Write-Output "Backup the files into backup storage using AzCopy"
Start-Process -Wait -FilePath "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\AzCopy\AzCopy.exe" -ArgumentList "/source:$($share.StorageUri.PrimaryUri) /sourcekey:$($filesKey) /dest:$($container.CloudBlobContainer.StorageUri.PrimaryUri) /destkey:$($backupKey) /s /nc:16"
#############################################################
# DEMO RESTORING FILES FROM STORAGE INTO SHARE USING AZCOPY
#############################################################
Write-Output "Restore the files into restore storage using AzCopy"
Start-Process -Wait -FilePath "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\AzCopy\AzCopy.exe" -ArgumentList "/source:$($container.CloudBlobContainer.StorageUri.PrimaryUri) /sourcekey:$($backupKey) /dest:$($restoreShare.StorageUri.PrimaryUri) /destkey:$($restoreKey) /s /nc:16"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment