Amazon RDS for MySQLをCLIで一発でリストアする方法

aws_rds_for_mysql_restore

RDS for MySQLを自動バックアップから特定時点への復元をする際に、CLI操作で一発で実行する方法を備忘としてまとめます。

MySQLをCLIでリストア

コマンドは以下の通りです。

aws rds restore-db-instance-to-point-in-time \
    --source-db-instance-automated-backups-arn arn:aws:rds:ap-northeast-1:${AccountId}:auto-backup:ab-xxxxxxxxxxxxxxxxxxxxxxxx \
    --target-db-instance-identifier rds-mysql \
    --restore-time 2025-xx-xxTxx:xx:00.000Z \
    --multi-az \
    --db-instance-class db.m6i.xlarge \
    --storage-type gp3 \
    --allocated-storage 5400 \
    --iops 12000 \
    --port 3306 \
    --engine mysql \
    --region ap-northeast-1 \
    --vpc-security-group-ids sg-xxxxxxxxxxxx \
    --db-subnet-group-name rds-mysql-subnet \
    --copy-tags-to-snapshot \
    --enable-cloudwatch-logs-exports audit error general slowquery

上記のコマンドについて解説します。

1. 特定時点の復元をする際の指定コマンドです。

aws rds restore-db-instance-to-point-in-time

2. 自動バックアップのARNを指定します。

自動バックアップの概要からバックアップのARNが確認できます。

--source-db-instance-automated-backups-arn arn:aws:rds:ap-northeast-1:${AccountId}:auto-backup:ab-xxxxxxxxxxxxxxxxxxxxxxxx

3. DBインスタンスの識別子を指定します。

--target-db-instance-identifier rds-mysql

4. 復元可能な最新の時刻を指定します。

UTC時間で指定する必要があるため、実際に復元する時間-9時間を指定します。

本項目は対象RDSの概要から確認できる、「最も遅い復元可能な時刻」から確認できるため、その時刻からー9時間して指定すると良い。

--restore-time 2025-xx-xxTxx:xx:00.000Z

5. 以下まとめて記載します。

マルチAZであるかどうかの指定 -> マルチAZでない場合は本項目は指定しません。

DBインスタンスクラスの指定

ストレージタイプの指定

ストレージの割り当ての指定

プロビジョンドIOPSの指定

ポートの指定

    --multi-az \
    --db-instance-class db.m6i.xlarge \
    --storage-type gp3 \
    --allocated-storage 5400 \
    --iops 12000 \
    --port 3306

6. DBエンジンを指定します。

以下はMySQLの場合

--engine mysql

7. どこのリージョンに復元するかを指定します。

--region ap-northeast-1

8. セキュリティグループを指定します。

--vpc-security-group-ids sg-xxxxxxxxxxxx

9. DBサブネットグループを指定します。

--db-subnet-group-name rds-mysql-subnet

10. スナップショットにタグをコピーを有効化する。

--copy-tags-to-snapshot

11. CloudWatch Logsに出力するログを指定します。

以下の場合、

audit = 監査ログ
error = エラーログ
general = 全般ログ
slowquery = スロークエリログ

上記を出力します。

--enable-cloudwatch-logs-exports audit error general slowquery

CLIコマンドを実行

CloudShellを使用してコマンドを実行し、成功すると以下のようなメッセージが表示されプロンプトが返ります。

{
    "DBInstance": {
        "DBInstanceIdentifier": "rds-mysql",
        "DBInstanceClass": "db.m6i.xlarge",
        "Engine": "mysql",
        "DBInstanceStatus": "creating",
        "MasterUsername": "admin",
        "AllocatedStorage": 5400,
        "PreferredBackupWindow": "04:00-04:30",
        "PreferredMaintenanceWindow": "sun:18:00-sun:18:30",
        "MultiAZ": true,
        "StorageType": "gp3",
        "Iops": 12000,
        "VpcSecurityGroups": [
            {
                "VpcSecurityGroupId": "sg-xxxxxxxxxxxx",
                "Status": "active"
            }
        ],
        "DBSubnetGroup": {
            "DBSubnetGroupName": "rds-mysql-subnet",
            "VpcId": "vpc-xxxxxxxxxxxx",
            "SubnetGroupStatus": "Complete"
        },
        "Endpoint": null,
        "PendingModifiedValues": {},
        "LatestRestorableTime": "2025-xx-xxTxx:xx:00Z",
        "EngineVersion": "8.0.xx",
        "CopyTagsToSnapshot": true,
        "EnabledCloudwatchLogsExports": [
            "audit",
            "error",
            "general",
            "slowquery"
        ]
    }
}

失敗時には以下のようなメッセージが表示されますので、コマンドの内容を見返してみましょう。

An error occurred (InvalidParameterCombination) when calling the RestoreDBInstanceToPointInTime operation: 
The source automated backup ARN is invalid or not found.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です