Cleanup SQL Backups in Azure Storage
Cleanup SQL Backups in Azure Storage
Cleanup SQL Backups in Azure Storage
import-module Azure
## Global Variables
# SubscriptionName
$subname = "Your Azure Subscription Name"
# Storage Credentials
$stracctname = "Your Storgae Account Name"
$stracctkey = "Your Storage Account Key"
$context = New-AzureStorageContext -StorageAccountName $stracctname -StorageAcco
untKey $stracctkey
# Container variables
$contsqllogs = "Your Container Name where backups are stored"
$contsqldb = "Your Container Name where backups are stored"
# Date Filter variable anything older from 7 days
$isOldDate = [DateTime]::UtcNow.AddDays(-7)
#Get the current date and time.
$DateTime=Get-Date
$filedate = Get-Date -format "MMdyyyyHMs"
#Change the path of log file to a desired location below
$LogFilePath = "C:\Scripts\Output\"
#Build the log file name. Do not update this variable
$LogFile = $LogFilePath + "CleanUpSQLAzureBKPs" + $filedate + ".log"
Echo "Script Starting: $DateTime" | Out-File $LogFile -Append
## Future part of script will also use a stored windows credential to log on ins
tead of of calling a generic
# Connecting to Azure Instance
# $cred = Get-Credential
# Add-AzureAccount -Credential $cred
# Select-subscription -Name $subname
## Future script function using another script to release Lease Locks on blobs
# releasing the Leases on locked Blobs in sql-logs and sql-db
$DateTime=Get-Date
Echo "Script Ending: $DateTime" | Out-File $LogFile -Append
# Send a notification Email
Send-MailMessage -from "NAME <[email protected]>" -to "NAME <NAME@CUSTOMDOMA
IN.com>" -subject "Remove Older Azure SQL Backups" -body "Removing older than 7
days old SQL Log Backups in Azure Storage" -Attachments $LogFile -smtpServer SMT
PSERVER