Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<#
Written with version 1.3.0 of the Az PowerShell Module
Run Get-InstalledModule to check installed modules
Install PowerShell Core 6 https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-6
The Az PowerShell Module is available from here https://github.com/Azure/azure-powershell/releases/tag/v1.4.0-February2019
... or run: Install-Module -Name Az -RequiredVersion 1.4.0 -AllowClobber
Migration instructions Azure.RM to Az - https://azure.microsoft.com/en-au/blog/how-to-migrate-from-azurerm-to-az-in-azure-powershell/
#>
<#
https://github.com/Azure-Samples/Use-PowerShell-for-long-term-retention-of-Azure-Files-Backup
#>
######################################################
# Sign into Azure
######################################################
Connect-AzAccount
Get-AzSubscription | ? {$_.Name -match 'Internal Consumption'} | Select-AzSubscription
######################################################
# Preliminary steps
######################################################
# Set some variables
[int]$RetentionDays = '300'
$currentDate = Get-Date
$RetailTill = $currentDate.AddDays($RetentionDays)
$BackupPolicyName = 'NewAFSPolicy'
$StorageAccountName = 'marcmedia'
$AzureFilesShareName = 'aidem2019'
$RecoveryVaultName = 'testvault'
# Set the vault context
Get-AzRecoveryServicesVault -Name $RecoveryVaultName | Set-AzRecoveryServicesVaultContext
######################################################
# Configure backup for an Azure file share
######################################################
<#
Create a generic protection policy - $BackupPolicyName - if it's not already created
This takes a daily backup and retains it for 30 days
#>
$afsPol = Get-AzRecoveryServicesBackupProtectionPolicy `
-WorkloadType "AzureFiles" | Where-Object {$_.name -eq $BackupPolicyName} -ErrorAction SilentlyContinue
if(!($afsPol).Name){
$schPol = Get-AzRecoveryServicesBackupSchedulePolicyObject -WorkloadType "AzureFiles"
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType "AzureFiles"
New-AzRecoveryServicesBackupProtectionPolicy -Name $BackupPolicyName `
-WorkloadType "AzureFiles" -RetentionPolicy $retPol -SchedulePolicy $schPol
}
<#
Enable protection for an Azure Files Share - if it's not already enabled
#>
# Backup Containers are things like a storage account or a virtual machine - AzureVM, Windows, AzureSQL, AzureStorage
$afsContainer = Get-AzRecoveryServicesBackupContainer `
-FriendlyName $StorageAccountName -ContainerType AzureStorage -ErrorAction SilentlyContinue
# Backup items are things like Azure Files Shares inside of a Storage Account container, or VMs inside of a VM container
$afsBkpItem = Get-AzRecoveryServicesBackupItem -Container $afsContainer `
-WorkloadType "AzureFiles" -Name $AzureFilesShareName
if(!($afsBkpItem)){
$afsPol = Get-AzRecoveryServicesBackupProtectionPolicy -WorkloadType "AzureFiles" | Where-Object {$_.name -eq $BackupPolicyName}
Enable-AzRecoveryServicesBackupProtection -StorageAccountName $StorageAccountName `
-Name $AzureFilesShareName -Policy $afsPol
}
Enable-AzRecoveryServicesBackupProtection -
###############################################################################################################
############################# Unregister a backup container - only if you need to #############################
###############################################################################################################
<#
# Unregister a backup container
#>
Get-AzRecoveryServicesBackupContainer -ContainerType AzureStorage
$afsContainer = Get-AzRecoveryServicesBackupContainer -FriendlyName 'Name' -ContainerType AzureStorage
Unregister-AzRecoveryServicesBackupContainer -Container $afsContainer
######################################################
# On-Demand Backup
######################################################
<#
# Trigger an on-demand backup
#>
$afsContainer = Get-AzRecoveryServicesBackupContainer -FriendlyName $StorageAccountName -ContainerType AzureStorage
$afsBkpItem = Get-AzRecoveryServicesBackupItem -Container $afsContainer `
-WorkloadType "AzureFiles" -Name $AzureFilesShareName
$job = Backup-AzRecoveryServicesBackupItem -Item $afsBkpItem -ExpiryDateTimeUTC $RetailTill
######################################################
# Check Recovery Points
######################################################
<#
Restore Azure file shares to an alternate location
#>
# Fetch recovery points
$afsContainer = (Get-AzRecoveryServicesBackupContainer -FriendlyName $StorageAccountName -ContainerType AzureStorage)[0]
$afsBkpItem = Get-AzRecoveryServicesBackupItem -Container $afsContainer `
-WorkloadType "AzureFiles" -Name $AzureFilesShareName
if($afsBkpItem -ne $null){
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
$rp = Get-AzRecoveryServicesBackupRecoveryPoint -Item $afsBkpItem `
-StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime()
$rp[0] | fl *
}
######################################################
# Restore Azure file shares and Azure files
######################################################
# Restore an Azure file share
Restore-AzRecoveryServicesBackupItem -RecoveryPoint $rp[0] -TargetStorageAccountName $StorageAccountName `
-TargetFileShareName "DestAFS" -TargetFolder "testAzureFS_restored" -ResolveConflict Overwrite
# Restore an Azure file
Restore-AzRecoveryServicesBackupItem -RecoveryPoint $rp[0] -TargetStorageAccountName $StorageAccountName `
-TargetFileShareName "DestAFS" -TargetFolder "testAzureFS_restored" `
-SourceFileType File -SourceFilePath "TestDir/TestDoc.docx" -ResolveConflict Overwrite
<#
Restore Azure file shares to the original location
#>
# Overwrite an Azure file share
Restore-AzRecoveryServicesBackupItem -RecoveryPoint $rp[0] -ResolveConflict Overwrite
# Overwrite an Azure file
Restore-AzRecoveryServicesBackupItem -RecoveryPoint $rp[0] -SourceFileType File `
-SourceFilePath "TestDir/TestDoc.docx" -ResolveConflict Overwrite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.