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.

</pre>
#--------------------------------------------------------------------------------
#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!

Cheers

Ramasankar Molleti

LinkedIn: LinkedIn Profile

Twitter: Twitter

Published by Ramasankar

As a Principal Cloud Architect with over 18 years of experience, I am dedicated to revolutionizing IT landscapes through cutting-edge cloud solutions. My expertise spans Cloud Architecture, Security Architecture, Solution Design, Cloud Migration, Database Transformation, Development, and Big Data Analytics.Currently, I spearhead cloud initiatives with a focus on Infrastructure, Containerization, Security, Big Data, Machine Learning, and Artificial Intelligence. I collaborate closely with development teams to architect, build, and manage robust cloud ecosystems that drive business growth and technological advancement.Core Competencies: • Cloud Platforms: AWS, Google Cloud Platform, Microsoft Azure • Technologies: Kubernetes, Serverless Computing, Microservices • Databases: MS SQL Server, PostgreSQL, Oracle, MongoDB, Amazon Redshift, DynamoDB, Aurora • Industries: Finance, Retail, Manufacturing. Throughout my career, I’ve had the privilege of working with industry leaders such as OCC, Gate Gourmet, Walgreens, and Johnson Controls, gaining invaluable insights across diverse sectors.As a lifelong learner and knowledge sharer, I take pride in being the first in my organization to complete all major AWS certifications. I am passionate about mentoring and guiding fellow professionals in their cloud journey, fostering a culture of continuous learning and innovation.Let’s connect and explore how we can leverage cloud technologies to transform your business: • LinkedIn: https://www.linkedin.com/in/ramasankar-molleti-23b13218/ • Book a mentorship session: [1:1] Together, let’s architect the future of cloud computing and drive technological excellence. Disclaimer The views expressed on this website/blog are mine alone and do not reflect the views of my company. All postings on this blog are provided “AS IS” with no warranties, and confers no rights. The owner of https://ramasankarmolleti.com will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.

Leave a comment