LoginSignup
0
1

More than 5 years have passed since last update.

bacpacファイル出力プログラム

Last updated at Posted at 2018-10-07

Azure SQLは最大で35日までのバックアップが可能。
それ以上にバックアップを保管しておく必要があったため、bacpacファイルを出力することに。
CentOSで自動で実行できるようするための対応を記載。個人的メモ。

注意

ここで書いたプログラムは、日々行われるAzureのバージョンアップで、使えなくなってしまう可能性がある。
そうなった際はAzureのサポート要求を登録して確認すること。

Azure設定

・SQL Server作成
        --> ファイアウォールの設定で、Azureサービスの許可を on にする

・SQL Database作成

・Blobストレージ、コンテナ作成

・Azure ADのアプリケーション登録
        --> アプリケーションIDとアプリケーションのキーを取得。後で説明するconfig.ymlで使用する。

・仮想マシン作成(CentOS7)
        --> AzureのCentOS7なら、たぶん最初からPowerShellがインストールされてるはず。

CentOS

・AzureRM インストール

PowerShellのプログラムでAzureにアクセスさせるので、関連モジュールをインストール

[user@centos7 ~]$ sudo pwsh
PS /root> Install-Module AzureRM.NetCore
PS /root> Import-Module AzureRM.Netcore
PS /root> Import-Module AzureRM.Profile.Netcore



・powershell-yaml インストール

設定値をyamlファイルに定義して、PowerShellの実行プログラムで読み込んで利用するため、powershell-yamlをインストール

PS /root> Install-Module powershell-yaml

設定ファイル

PowerShellの実行プログラムで参照する設定を定義

config.yml
azure:
  resource_group: hogefuga-rg
  tenant_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
  app_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
  app_key: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

  sqlserver: 
    -
      host: sqlsv-hogefuga-001
      admin: sql-admin@sqlsv-hogefuga-001
      password: 'P@$$w0rd'
    -
      host: sqlsv-hogefuga-002
      admin: sql-admin@sqlsv-hogefuga-002
      password: 'Passw0rd!!!'

  storage:
    backup:
      storage_name: hogefuga_st001
      storage_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
      storage_key_type: StorageAccessKey
      container: 'bacpac-export'

実行ファイル

export_bacpac.ps1
# 引数(1:sqlserver配列番号, 2:DB名)
$num = $Args[0]
# config.yml の読み込み(powershell-yamlを利用)
$config = Get-Content config.yml -Raw
$config_yaml = ConvertFrom-Yaml $config

# AzureADに登録したアプリの情報でアクセス(アプリケーションID・キー・テナントID)
$AppKey = $config_yaml.azure.app_key
$AppId = $config_yaml.azure.app_id
$TenantId = $config_yaml.azure.tenant_id
$secpassword = ConvertTo-SecureString $AppKey -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($AppId, $secpassword)
Login-AzureRmAccount -ServicePrincipal -TenantId $TenantId -Credential $mycreds

# バックアップ用リソース設定
$ResourceGroupName = $config_yaml.azure.resource_group
$ServerName = $config_yaml.azure.sqlserver[$num].host
$DatabaseName = $Args[1]

# バックアップ用ストレージ・コンテナ設定
$storageName = $config_yaml.azure.storage.backup.storage_name
$StorageKeyType = $config_yaml.azure.storage.backup.storage_key_type
$StorageKey = $config_yaml.azure.storage.backup.storage_key
$containerName = $config_yaml.azure.storage.backup.container

# バックアップファイル名設定
$BacpacFileName = $DatabaseName + '.bacpac'
$BacpacUri = ('https://' + $storageName + '.blob.core.windows.net/' + $containerName + '/export/dir/' + $ServerName + '/' + $BacpacFileName).Replace("-", "_")

# SQL Database へのログイン設定。パスワードは SecureString に変換。
$Login = $config_yaml.azure.sqlserver[$num].admin
$Password = $config_yaml.azure.sqlserver[$num].password | ConvertTo-SecureString -AsPlainText -Force

# エクスポート開始
$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri -AdministratorLogin $Login -AdministratorLoginPassword $Password

実行

config.ymlに定義した最初のSQL Serverにあるhogefuga_databaseをバックアップ

[user@centos7 ~]$ pwsh -File bacpac_export.ps1 0 hogefuga_database
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1