0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RDS(SQL SERVER)のネイティブバックアップ手順[備忘録]

Posted at

はじめに

実務でRDS (SQL Server) のバックアップをS3にアップロード・リストアしたい、という要望が出たので調査、実装しました。

以下手順です。


1. IAMロールの準備

RDSがS3にアクセスするための権限が必要です。

1. IAMポリシーの作成
S3バケットへのアクセスを許可するポリシーを作成します。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:ListBucket",
        "s3:GetBucketLocation"
      ],
      "Resource": [
        "arn:aws:s3:::<your-bucket-name>"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:DeleteObject",
        "s3:ListMultipartUploadParts",
        "s3:AbortMultipartUpload"
      ],
      "Resource": [
        "arn:aws:s3:::<your-bucket-name>/*"
      ]
    }
  ]
}

2. IAMロールの作成
RDSサービス (rds.amazonaws.com) を信頼するIAMロールを作成し、上記で作成したポリシーをアタッチします。

  • 信頼されたエンティティタイプ: 「AWS のサービス」
  • ユースケース: 「RDS」 > 「RDS - Add feature to database」

信頼ポリシーが以下のようになっていることを確認してください。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "rds.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

2. RDSオプショングループの設定

対象のRDSインスタンスが使用するオプショングループに、以下の設定を追加します。

  1. 「オプションの追加」を選択します。
  2. SQLSERVER_BACKUP_RESTORE オプションを選択します。
  3. 「IAM ロール」に、ステップ1で作成したIAMロールを指定します。
  4. 設定を適用します(インスタンスの再起動が伴う場合があります)。

3. 実行コマンド (T-SQL)

SSMSなどからRDSに接続し、専用のストアドプロシージャを実行します。

バックアップ (S3へ)

EXEC msdb.dbo.rds_backup_database 
    @source_db_name = 'testdb', 
    @s3_arn_to_backup_to = 'arn:aws:s3:::your-bucket-name/backups/testdb.bak',
    @type = 'FULL'; -- FULL または DIFFERENTIAL

リストア (S3から)

EXEC msdb.dbo.rds_restore_database 
    @restore_db_name = 'testdb_restored', 
    @s3_arn_to_restore_from = 'arn:aws:s3:::your-bucket-name/backups/testdb.bak';

4. リストア時の注意点

  • 上書き不可: @restore_db_name に指定するDB名は、インスタンス内に 存在しない新しい名前 である必要があります。既存DBへの上書き(WITH REPLACE)はサポートされていません。
  • 回避策:
    1. DROP & RESTORE: 既存DBを DROP DATABASE してから、同じ名前でリストアする。
    2. RENAME: 別名(例: testdb_new)でリストアした後、古いDBをリネーム(ALTER DATABASE testdb MODIFY NAME = testdb_old)し、新しいDBを元の名前にリネーム(ALTER DATABASE testdb_new MODIFY NAME = testdb)する。

5. タスクの進捗確認

バックアップ・リストアは非同期で実行されます。進捗は以下のコマンドで確認します。

-- タスクのステータスを確認
EXEC msdb.dbo.rds_task_status;

-- タスクIDで絞り込み
EXEC msdb.dbo.rds_task_status @task_id = 123;

参考先:https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/UserGuide/SQLServer.Procedural.Importing.Native.Enabling.html

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?