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

Advertisements

About Ramasankar

My name is Ramasankar Molleti, and I have been working on various database systems (SQL Server, Oracle, Postgres, DynamoDB, Redshift) over 10 years, primarily focusing on Database Administration, Architect , Development and Business intelligence. I also have working experience on Amazon Web Services DevOps. I hold various Microsoft certifications (Microsoft Certified Information Technology Professional, Microsoft Certified Technology Specialist, and Microsoft Certified Solution Associate). 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://sqlram.wordpress.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.
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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s