Azure Blob Storage에 여러 데이터베이스 백업 - PowerShell

적용 대상:SQL Server

이 항목에서는 PowerShell cmdlet을 사용하여 Azure Blob Storage로의 백업을 자동화하는 데 사용할 수 있는 샘플 스크립트를 제공합니다.

백업 및 복원에 대한 PowerShell cmdlet 개요

Backup-SqlDatabaseRestore-SqlDatabase 는 백업 및 복원 작업을 수행하는 데 사용할 수 있는 두 가지 주요 cmdlet입니다.

또한 SqlCredential cmdlet 세트와 같이 Windows Azure Blob Storage에 대한 백업을 자동화하는 데 필요할 수 있는 다른 cmdlet도 있습니다.

사용 가능한 모든 cmdlet 목록은 SqlServer cmdlet을 참조 하세요.

SqlCredential cmdlet은 Azure Blob Storage로 백업 및 복원 시나리오에서 사용됩니다. 자격 증명과 해당 사용에 관한 자세한 내용은 Microsoft Azure Blob Storage로 SQL Server 백업 및 복원을 참조하세요.

다중 데이터베이스, 다중 인스턴스 백업 작업을 위한 PowerShell

아래의 섹션에는 여러 SQL Server 인스턴스에서 SQL 자격 증명을 만들고, 한 SQL Server 인스턴스의 모든 사용자 데이터베이스를 백업하는 등의 다양한 작업에 대한 스크립트가 포함되어 있습니다. 이러한 스크립트를 사용하여 환경의 요구 사항에 따라 백업 작업을 자동화하거나 예약할 수 있습니다. 여기에 제공된 스크립트는 예제이며 사용자 환경에 맞게 수정되거나 확장될 수 있습니다.

예제 스크립트에 대한 고려 사항은 다음과 같습니다.

  • SQL Server PowerShell은 cmdlet을 구현하여 PowerShell 공급자가 지원하는 개체의 계층 구조를 나타내는 경로 구조를 탐색합니다. 경로의 노드로 이동한 경우 다른 cmdlet을 사용하여 현재 개체에 대한 기본 작업을 수행할 수 있습니다.

    자세한 내용은 Navigate SQL Server PowerShell Paths을 참조하세요.

  • Get-ChildItem cmdlet: Get-ChildItem에서 반환되는 정보는 SQL Server PowerShell 경로의 위치에 따라 달라집니다. 예를 들어 위치가 컴퓨터 수준에 있는 경우 이 cmdlet은 컴퓨터에 설치된 모든 SQL Server 데이터베이스 엔진 인스턴스를 반환합니다. 또는 위치가 데이터베이스와 같은 개체 수준에 있는 경우 데이터베이스 개체 목록을 반환합니다. 기본적으로 Get-ChildItem cmdlet은 시스템 개체를 반환하지 않습니다. 매개 변수를 –Force 사용하여 시스템 개체를 확인합니다.

  • Azure Storage 계정 및 SQL 자격 증명은 필수 구성 요소이며 Azure Blob Storage에 대한 모든 백업 및 복원 작업에 필수 구성 요소입니다.

SQL Server의 모든 인스턴스에서 SQL 자격 증명 만들기

다음 스크립트를 사용하여 컴퓨터의 모든 SQL Server 인스턴스에서 일반 SQL 자격 증명을 만들 수 있습니다. 컴퓨터의 인스턴스 중 하나에 동일한 이름의 기존 자격 증명이 이미 있는 경우 스크립트는 오류를 표시하고 계속됩니다.

# load the sqlps module
import-module sqlps  
  
# set parameters
$sqlPath = "sqlserver:\sql\$($env:COMPUTERNAME)"
$storageAccount = "<myStorageAccount>"  
$storageKey = "<myStorageAccessKey>"  
$secureString = ConvertTo-SecureString $storageKey -AsPlainText -Force  
$credentialName = "myCredential-$(Get-Random)"

Write-Host "Generate credential: " $credentialName
  
#cd to sql server and get instances  
cd $sqlPath
$instances = Get-ChildItem

#loop through instances and create a SQL credential, output any errors
foreach ($instance in $instances)  {
    try {
        $path = "$($sqlPath)\$($instance.DisplayName)\credentials"
        New-SqlCredential -Name $credentialName -Identity $storageAccount -Secret $secureString -Path $path -ea Stop | Out-Null
        Write-Host "...generated credential $($path)\$($credentialName)."  }
    catch { Write-Host $_.Exception.Message } }

참고 항목

-ea Stop | Out-Null 문은 사용자 정의 예외 출력에 사용됩니다. 기본 PowerShell 오류 메시지를 선호하는 경우 이 문을 제거할 수 있습니다.

모든 SQL Server 인스턴스에서 SQL 자격 증명 제거

다음 스크립트를 사용하여 컴퓨터에 설치된 모든 SQL Server 인스턴스에서 특정 자격 증명을 제거할 수 있습니다. 특정 인스턴스에 자격 증명이 없으면 오류 메시지가 표시되고 모든 인스턴스를 확인할 때까지 스크립트가 계속됩니다.

import-module sqlps

$sqlPath = "sqlserver:\sql\$($env:COMPUTERNAME)"
$credentialName = "<myCredential>"

Write-Host "Delete credential: " $credentialName

cd $sqlPath
$instances = Get-ChildItem

#loop through instances and delete a SQL credential
foreach ($instance in $instances)  {
    try {
        $path = "$($sqlPath)\$($instance.DisplayName)\credentials\$($credentialName)"
        Remove-SqlCredential -Path $path -ea Stop | Out-Null
        Write-Host "...deleted credential $($path)."  }
    catch { Write-Host $_.Exception.Message } }

모든 데이터베이스에 대한 전체 백업

다음 스크립트에서는 컴퓨터에 있는 모든 데이터베이스의 백업을 만듭니다. 여기에는 사용자 데이터베이스뿐만 아니라 msdb 시스템 데이터베이스도 포함됩니다. 스크립트는 tempdb모델 시스템 데이터베이스를 필터링합니다.

import-module sqlps  

$sqlPath = "sqlserver:\sql\$($env:COMPUTERNAME)"
$storageAccount = "<myStorageAccount>"  
$blobContainer = "<myBlobContainer>"  
$backupUrlContainer = "https://$storageAccount.blob.core.windows.net/$blobContainer/"  
$credentialName = "<myCredential>"

Write-Host "Backup database: " $backupUrlContainer
  
cd $sqlPath
$instances = Get-ChildItem

#loop through instances and backup all databases (excluding tempdb and model)
foreach ($instance in $instances)  {
    $path = "$($sqlPath)\$($instance.DisplayName)\databases"
    $databases = Get-ChildItem -Force -Path $path | Where-object {$_.name -ne "tempdb" -and $_.name -ne "model"}

    foreach ($database in $databases) {
        try {
            $databasePath = "$($path)\$($database.Name)"
            Write-Host "...starting backup: " $databasePath
            Backup-SqlDatabase -Database $database.Name -Path $path -BackupContainer $backupUrlContainer -SqlCredential $credentialName -Compression On
            Write-Host "...backup complete."  }
        catch { Write-Host $_.Exception.Message } } }

특정 SQL Server 인스턴스에서만 시스템 데이터베이스의 전체 백업

전체 스크립트를 사용하여 명명된 SQL Server 인스턴스에서 마스터msdb 데이터베이스를 백업할 수 있습니다. 인스턴스 매개 변수 값을 변경하여 모든 인스턴스에 동일한 스크립트를 사용할 수 있습니다. SQL Server의 기본 인스턴스 이름은 DEFAULT.입니다.

import-module sqlps  

$sqlPath = "sqlserver:\sql\$($env:COMPUTERNAME)"
$instanceName = "DEFAULT"
$storageAccount = "<myStorageAccount>"  
$blobContainer = "<myBlobContainer>"  
$backupUrlContainer = "https://$storageAccount.blob.core.windows.net/$blobContainer/"  
$credentialName = "<myCredential>"

Write-Host "Backup database: " $instanceName " to " $backupUrlContainer
  
cd "$($sqlPath)\$($instanceName)"

#loop through instance and backup specific databases
$databases = "master", "msdb"  
foreach ($database in $databases) {
    try {
        Write-Host "...starting backup: " $database
        Backup-SqlDatabase -Database $database -BackupContainer $backupUrlContainer -SqlCredential $credentialName -Compression On
        Write-Host "...backup complete." }
    catch { Write-Host $_.Exception.Message } }

참고 항목

Azure Blob Storage를 사용하여 SQL Server 백업 및 복원

URL에 SQL Server 백업 모범 사례 및 문제 해결