Powershell Script to Backup the file system folders/files to Azure blob storage

One of the requirement from the customer was backup the important application files to azure blob storage and delete the backups older than 7 days. I have written a below script to automate the task.

#Script: Backup the files to azure blob storage
#Auther : Ramasankar Molleti
#This will make sure to run the powershell as administrator for Azcopy command
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }

Write-Host "started"
#Source of the file
$source = "Path of the files"

#Initializing a destination file
$destination = "Temporary Destinationpath
#Adding current date to the file
$date = Get-Date
$date = $date.ToString("yyyy-MM-dd")
$destination = $destination+$date
$destination =$destination+'.zip'
write-host $destination
Write-Host "Compressing is in progress" -ForegroundColor Yellow
#This assembly is used to compress the folder
Add-Type -Assembly 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory($source, $destination,'Optimal',$false)
Write-Host "Compression is done" -ForegroundColor Green
# Backup the compressed Appplication folders/files to blob storage
Write-Host "Begin backup to azure blob" -ForegroundColor Yellow
AzCopy /Source:D:\temp /Dest:https://yourstorageaccount.blob.core.windows.net/Provideyourazurecontainer /DestKey:yourazurblobstorageprimarykeydetails
Write-Host "Complete Backup to AZure Blob" -ForegroundColor Green
#Define the source storage account and context.
$SourceStorageAccountName = "Provide your storage account"
$SourceStorageAccountKey = "Provide your storage account primary/master key"
$SrcContainerName = "Your AzureContainername"
$SourceContext = New-AzureStorageContext -StorageAccountName $SourceStorageAccountName -StorageAccountKey $SourceStorageAccountKey
#$blobs = Get-AzureStorageBlob -Container $SrcContainerName -Context $SourceContext
#Set the threshold value
$isOldDate = [DateTime]::UtcNow.AddDays(-7)
#Get a reference to blobs in the source container.
$blobs = Get-AzureStorageBlob -Container $SrcContainerName -Context $SourceContext | Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate }
$blobs| Remove-AzureStorageBlob
Write-Host "end" 


Hope you enjoyed the post!


Ramasankar Molleti

LinkedIn: LinkedIn Profile

Twitter: Twitter

About Ramasankar

My name is Ramasankar Molleti, I have been working on various database and cloud systems (SQL Server, Oracle, PostgreSQL, DynamoDB, Redshift, Amazon, Azure) over 12 years, primarily focusing on Architect , Development, Administration and Business intelligence. I hold various Certifications such as Amazon Web services Solution Architect Associate, Microsoft certifications (Microsoft Certified Information Technology Professional, Microsoft Certified Technology Specialist, and Microsoft Certified Solution Associate).
This entry was posted in Backup & Recovery. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s