Azure 스토리지 계정은 Azure 생태계에서 데이터 저장 솔루션의 초석으로, SQL 백업 저장에서 미디어 파일 제공에 이르기까지 다양한 작업 부하를 지원합니다. 스토리지 컨테이너에서 오래되었거나 중복된 Blob을 삭제하는 것과 같은 작업을 자동화하면 저장 비용을 최적화하고 효율성을 보장할 수 있습니다.
이 가이드는 PowerShell을 사용하여 Azure 스토리지 계정에서 Blob을 안전하고 효과적으로 삭제하는 방법을 안내합니다. SQL 백업, 애플리케이션 로그 또는 기타 비구조화된 데이터를 관리하는 경우 이 프로세스는 정리 작업이 일상적인 요구사항이 되는 다양한 시나리오에 적용될 수 있습니다.
스토리지 계정이 처음이신가요?
Microsoft Azure의 핵심 서비스 중 하나는 스토리지 계정 서비스입니다. 많은 서비스가 가상 머신 디스크, 진단 로그(특히 애플리케이션 로그), SQL 백업 등과 같은 데이터를 저장하기 위해 스토리지 계정을 활용합니다. Azure 스토리지 계정 서비스를 사용하여 Blob 또는 이진 데이터와 같은 자신의 데이터를 저장할 수도 있습니다.
MSDN에 따르면, Azure Blob 스토리지는 대량의 비구조화된 객체 데이터를 저장할 수 있도록 합니다. Blob 스토리지를 사용하여 미디어, 콘텐츠 또는 애플리케이션 데이터를 사용자에게 수집하거나 노출할 수 있습니다. 모든 Blob 데이터는 컨테이너 내에 저장되기 때문에 데이터를 업로드하기 시작하기 전에 스토리지 컨테이너를 생성해야 합니다.
단계별로 안내합니다.
1단계: 필수 입력값 가져오기
이 예에서는 SQL 컨테이너에 저장된 bacpac
형식의 SQL 데이터베이스(백업 또는 저장된)를 삭제할 것입니다.
## prerequisite Parameters
$resourceGroupName="rg-dgtl-strg-01"
$storageAccountName="sadgtlautomation01"
$storageContainerName="sql"
$blobName = "core_2022110824.bacpac"
단계 2: Azure 구독에 연결
az login
명령을 사용하여 서비스 주체와 연결하는 것은 자동화 작업 및 스크립트에 대한 인증 및 Azure 구독에 연결하는 안전하고 효율적인 방법입니다. Azure 관리 작업을 자동화하거나 대화형이 아닌 방식으로 스크립트를 실행해야 하는 시나리오에서 서비스 주체를 사용하여 인증할 수 있습니다. 서비스 주체는 애플리케이션 또는 스크립트가 Azure 리소스에 안전하게 액세스할 수 있도록 만든 식별자입니다.
## Connect to your Azure subscription
az login --service-principal -u "210f8f7c-049c-e480-96b5-642d6362f464" -p "c82BQ~MTCrPr3Daz95Nks6LrWF32jXBAtXACccAV" --tenant "cf8ba223-a403-342b-ba39-c21f78831637"
단계 3: 저장소 계정에 컨테이너가 있는지 확인
Azure Storage를 사용할 때는 저장소 계정에 컨테이너가 있는지 확인하거나 없으면 만들어야 할 수도 있습니다. 컨테이너의 존재 여부를 확인하려면 Get-AzStorageContainer
cmdlet을 사용할 수 있습니다.
## Get the storage account to check container exist or need to be create
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
## Get the storage account context
$context = $storageAccount.Context
단계 4: Blob 삭제 전 컨테이너의 존재 확인
Azure Storage 컨테이너에서 블롭을 삭제하려면 Remove-AzStorageBlob
cmdlet을 사용해야 합니다.
## Check if the storage container exists
if(Get-AzStorageContainer -Name $storageContainerName -Context $context -ErrorAction SilentlyContinue)
{
Write-Host -ForegroundColor Green $storageContainerName ", the requested container exit,started deleting blob"
## Create a new Azure Storage container
Remove-AzStorageBlob -Container $storageContainerName -Context $context -Blob $blobName
Write-Host -ForegroundColor Green $blobName deleted
}
else
{
Write-Host -ForegroundColor Magenta $storageContainerName "the requested container does not exist"
}
다음은 전체 코드입니다:
## Delete a Blob from an Azure Storage
## Input Parameters
$resourceGroupName="rg-dgtl-strg-01"
$storageAccountName="sadgtlautomation01"
$storageContainerName="sql"
$blobName = "core_2022110824.bacpac"
## Connect to your Azure subscription
az login --service-principal -u "210f8f7c-049c-e480-96b5-642d6362f464" -p "c82BQ~MTCrPr3Daz95Nks6LrWF32jXBAtXACccAV" --tenant "cf8ba223-a403-342b-ba39-c21f78831637"
## Function to create the storage container
Function DeleteblogfromStorageContainer
{
## Get the storage account to check container exist or need to be create
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
## Get the storage account context
$context = $storageAccount.Context
## Check if the storage container exists
if(Get-AzStorageContainer -Name $storageContainerName -Context $context -ErrorAction SilentlyContinue)
{
Write-Host -ForegroundColor Green $storageContainerName ", the requested container exit,started deleting blob"
## Remove the blob in Azure Storage container
Remove-AzStorageBlob -Container $storageContainerName -Context $context -Blob $blobName
Write-Host -ForegroundColor Green $blobName deleted
}
else
{
Write-Host -ForegroundColor Magenta $storageContainerName "the requested container does not exist"
}
}
#Call the Function
DeleteblogfromStorageContainer
다음은 출력입니다:
결론
PowerShell을 사용하여 Azure 저장소 계정에서 블롭 삭제를 자동화하는 것은 깔끔하고 효율적인 저장 시스템을 유지하는 실용적인 방법입니다. 제시된 단계를 따라 이 프로세스를 워크플로에 매끄럽게 통합하여 시간을 절약하고 수동 노력을 줄일 수 있습니다.
이 방법은 SQL 백업 파일에만 국한되지 않습니다. Azure Storage에 저장된 애플리케이션 로그, 진단 파일 또는 미디어 콘텐츠와 같은 다른 유형의 데이터 관리로도 확장할 수 있습니다. 컨테이너의 존재를 보장하고 PowerShell의 강력한 cmdlet을 활용함으로써, 자동화되고 오류 없는 방식으로 Azure 리소스를 자신 있게 관리할 수 있습니다.
Source:
https://dzone.com/articles/how-to-automate-blob-deletion-azure-storage-powershell