AWS RDS自動バックアップの復元

  • 2019.11.20
  • AWS
AWS RDS自動バックアップの復元

はじめに

前回までに作成したDBを使用して進めていきます。

RDSはデフォルトで自動バックアップが有効になるようで、デフォルトの設定では7日分保存されます。さらに毎日1回完全なスナップショットが作成され、直近5分前まで取り続けています。その間は、どのタイミングにでもリストアできるのです。

今回はこの自動バックアップによって作成されたバックアップよりDBを復元してみたいと思います。

バックアップ期間の確認

次のコマンドで確認することができます。

$ aws rds describe-db-instance-automated-backups --db-instance-identifier testdb-1
{
    "DBInstanceAutomatedBackups": [
        {
            "DBInstanceArn": "arn:aws:rds::281230433728:db:testdb-1",
            "DbiResourceId": "db-PSTE7IM2ADXWR3IHIKVLWOXTOQ",
            "Region": "apne-1",
            "DBInstanceIdentifier": "testdb-1",
            "RestoreWindow": {
                "EarliestTime": "2019-11-19T05:44:55.150Z",
                "LatestTime": "2019-11-20T08:10:00Z"
            },
            "AllocatedStorage": 20,
            "Status": "active",
            "Port": 3306,
            "AvailabilityZone": "ap-northeast-1a",
            "VpcId": "vpc-04e72e6413b624d29",
            "InstanceCreateTime": "2019-11-19T05:43:25Z",
            "MasterUsername": "admin",
            "Engine": "mariadb",
            "EngineVersion": "10.2.21",
            "LicenseModel": "general-public-license",
            "OptionGroupName": "default:mariadb-10-2",
            "Encrypted": false,
            "StorageType": "gp2",
            "IAMDatabaseAuthenticationEnabled": false
        }
    ]
}

RestoreWindowに表示されている期間はバックアップがあるということです。UTCタイムで表示されているため9時間プラスしてください。

この例の場合でいくと2019-11-19 14:44:55〜2019-11-20 17:10:00までの期間はバックアップが取られているということです。

バックアップの復元

まずは現在のDBの中身を見ておきます。

MariaDB [TestDB]> select * from WordList;
+---------------------+--------------+
| Timestamp           | Word         |
+---------------------+--------------+
| 2019-11-20 06:51:14 | thereupon    |
| 2019-11-20 06:51:19 | topocentric  |
(省略)
| 2019-11-20 06:56:40 | fact         |
| 2019-11-20 06:56:45 | juju         |
| 2019-11-20 06:56:50 | manganese    |
| 2019-11-20 06:56:55 | laden        |
+---------------------+--------------+
69 rows in set (0.00 sec)

このデータを一度消してしまい、バックアップ復元後に戻せるかどうかを見ておきます。

MariaDB [TestDB]> delete from WordList;
Query OK, 69 rows affected (0.01 sec)

MariaDB [TestDB]> exit;
Bye

それでは復元してみます。

$ aws rds restore-db-instance-to-point-in-time --source-db-instance-identifier testdb-1 --target-db-instance-identifier restore-testdb1 --db-subnet-group-name default-vpc-04e72e6413b624d29 --vpc-security-group-ids sg-0fc1435eb1b8b4cc1 --restore-time 2019-11-20T08:00:00Z

$ aws rds describe-db-instances --db-instance-identifier restore-testdb1 --query DBInstances[].DBInstanceStatus
[
    "creating"
]

$ aws rds describe-db-instances --db-instance-identifier restore-testdb1 --query DBInstances[].DBInstanceStatus
[
    "available"
]

$ aws rds describe-db-instances --db-instance-identifier restore-testdb1 --query DBInstances[].Endpoint
[
    {
        "Address": "restore-testdb1.hoge.ap-northeast-1.rds.amazonaws.com",
        "Port": 3306,
        "HostedZoneId": "Z24O6O9L7SGTNB"
    }
]

復元にはしばらく時間がかかりました。

$ mysql -h restore-testdb-1.hoge.ap-northeast-1.rds.amazonaws.com -u admin -p TestDB
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.2.21-MariaDB-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [TestDB]> select * from WordList;
+---------------------+--------------+
| Timestamp           | Word         |
+---------------------+--------------+
| 2019-11-20 06:51:14 | thereupon    |
| 2019-11-20 06:51:19 | topocentric  |
| 2019-11-20 06:51:24 | Schuyler     |
(省略)
| 2019-11-20 06:56:30 | epitaph      |
| 2019-11-20 06:56:35 | toast        |
| 2019-11-20 06:56:40 | fact         |
| 2019-11-20 06:56:45 | juju         |
| 2019-11-20 06:56:50 | manganese    |
| 2019-11-20 06:56:55 | laden        |
+---------------------+--------------+
69 rows in set (0.00 sec)

正常に復元できました。

まとめ

復元が簡単にできるのは嬉しいです。ただ、気をつけなくてはいけないのは、復元後は別DBができるということと、復元中のデータが消えてしまうということです。この辺りはマルチAZなどを利用して対応するしかなさそうです。