다음을 통해 공유


Fabric REST API를 사용하여 SQL 데이터베이스 미러링 시작 및 중지

적용 대상:Microsoft Fabric의 SQL 데이터베이스

Fabric REST API를 사용하여 SQL 데이터베이스에서 패브릭의 OneLake로의 미러링을 시작하고 중지할 수 있습니다. 이 문서 및 샘플 스크립트는 PowerShell을 사용하여 패브릭 REST API 시작 또는 중지 미러링을 호출하는 방법을 보여 줍니다.

OneLake에 대한 SQL 데이터베이스 미러링이 기본적으로 항상 실행됩니다. 패브릭의 SQL 데이터베이스에 대한 미러링을 중지해야 하는 시나리오가 있습니다. 예를 들어 기존 테이블에서 클러스터형 열 인덱스를 만들도록 설정하려면 미러링을 실행할 때 만들 수 없습니다.

필수 조건

패브릭의 OneLake에 대한 SQL 데이터베이스 미러링 중지

다음 PowerShell 예제에서는 패브릭의 OneLake에 대한 SQL 데이터베이스의 미러링을 중지합니다.

이 예제 스크립트는 자격 증명을 묻기 위해 Connect-AzAccount의 별칭인 az login을 사용합니다. 이러한 자격 증명을 사용하여 REST API 호출에 사용할 액세스 토큰을 가져옵니다. SQLCMD는 Connect-AzAccount에 제공된 계정의 컨텍스트를 사용합니다.

다음 스크립트에서는 작업 영역 ID 및 데이터베이스 ID를 제공해야 합니다. 둘 다 URL에서 찾을 수 있습니다. https://powerbi.com/groups/<fabric_workspace_id>/sqldatabases/<fabric_sql_database_id>; URL의 첫 번째 문자열은 패브릭 작업 영역 ID이고 두 번째 문자열은 SQL 데이터베이스 ID입니다.

  • <your workspace id>를 패브릭 작업 영역 ID로 바꾸세요. URL에서 작업 영역의 ID를 쉽게 찾을 수 있습니다. 이 ID는 브라우저 창에서 두 / 문자 안에 있는 /groups/ 고유한 문자열입니다.
  • 패브릭 데이터베이스 ID에서 <your database id>를 자신의 SQL 데이터베이스로 대체합니다. URL에서 데이터베이스 항목의 ID를 쉽게 찾을 수 있습니다. 이 ID는 브라우저 창에서 두 / 문자 안에 /sqldatabases/ 있는 고유한 문자열입니다.

이 스크립트는 다음을 보여 줍니다.

  1. 보안 문자열에서 액세스 토큰을 Get-AzAccessToken을 사용하여 검색하고 변환합니다. PowerShell 7을 사용하는 경우 ConvertFrom-SecureString 옵션도 있습니다.
  2. API 호출을 어셈블합니다.
  3. API 호출을 호출합니다.
Import-Module Az.Accounts

az login

$workspaceid = '<your workspace id>' # Find in the URL
$databaseid = '<your database id>' # Find in the URL

$headers = $null

# 1. Get the access token and add it to the headers

$access_token = (Get-AzAccessToken -AsSecureString -ResourceUrl https://api.fabric.microsoft.com)

$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($access_token.Token)

try {
$headers = @{ 
       Authorization = $access_token.Type + ' ' + ([System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr))
    }

$access_token.UserId -match('^[^@]+') | Out-Null

$stopMirroringUri = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceid/sqlDatabases/$databaseid/stopMirroring"

$parameters = @{
        Method="Post"
        Headers=$headers
        Uri = $stopMirroringUri
    }

Invoke-RestMethod @parameters -ErrorAction Stop

 } finally {
    # The following lines ensure that sensitive data is not left in memory.
    $headers = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}

패브릭의 OneLake에 SQL 데이터베이스 미러링 시작

다음 PowerShell 예제에서는 SQL 데이터베이스를 패브릭의 OneLake에 미러링을 시작합니다.

이 예제 스크립트는 자격 증명을 묻기 위해 Connect-AzAccount의 별칭인 az login을 사용합니다. 이러한 자격 증명을 사용하여 REST API 호출에 사용할 액세스 토큰을 가져옵니다. SQLCMD는 Connect-AzAccount에 제공된 계정의 컨텍스트를 사용합니다.

다음 스크립트에서 "<your workspace id>"를 당신의 패브릭 워크스페이스 ID로 교체하세요. URL에서 작업 영역의 ID를 쉽게 찾을 수 있습니다. 이 ID는 브라우저 창에서 두 / 문자 안에 있는 /groups/ 고유한 문자열입니다. 예를 들어 11aa111-a11a-1111-1abc-aa1111aaaahttps://fabric.microsoft.com/groups/11aa111-a11a-1111-1abc-aa1111aaaa/에서.

이 스크립트는 다음을 보여 줍니다.

  1. 보안 문자열에서 액세스 토큰을 Get-AzAccessToken을 사용하여 검색하고 변환합니다. PowerShell 7을 사용하는 경우 ConvertFrom-SecureString 옵션도 있습니다.
  2. API 호출을 어셈블합니다.
  3. API 호출을 호출합니다.
Import-Module Az.Accounts

az login

$workspaceid = '<your workspace id>' # Find in the URL
$databaseid = '<your database id>' # Find in the URL

$headers = $null

# 1. Get the access token and add it to the headers

$access_token = (Get-AzAccessToken -AsSecureString -ResourceUrl https://api.fabric.microsoft.com)

$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($access_token.Token)

try {
$headers = @{ 
       Authorization = $access_token.Type + ' ' + ([System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr))
    }

$access_token.UserId -match('^[^@]+') | Out-Null

$startMirroringUri = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceid/sqlDatabases/$databaseid/startMirroring"

$parameters = @{
        Method="Post"
        Headers=$headers
        Uri = $startMirroringUri
    }

Invoke-RestMethod @parameters -ErrorAction Stop

 } finally {
    # The following lines ensure that sensitive data is not left in memory.
    $headers = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}