
もくじ
関連
- AWS RDS PITR+ロールフォワードによるリカバリ 【下準備編】
- AWS RDS PITR+ロールフォワードによるリカバリ 【汎用 RDSのディスク障害からの復旧】
- AWS RDS PITR+ロールフォワードによるリカバリ 【誤ったクエリ発行からの復旧】
RDS側作業
binlog保存期限設定
24時間の保持に設定
mysql > call mysql.rds_set_configuration('binlog retention hours', 24);
MySQL [testdb]> call mysql.rds_show_configuration;
+------------------------+-------+-----------------------------------------------------------------------------------------------------------+
| name | value | description |
+------------------------+-------+-----------------------------------------------------------------------------------------------------------+
| binlog retention hours | 24 | binlog retention hours specifies the duration in hours before binary logs are automatically deleted. |
| source delay | 0 | source delay specifies replication delay in seconds between current instance and its master. |
| target delay | 0 | target delay specifies replication delay in seconds between current instance and its future read-replica. |
+------------------------+-------+-----------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
タイムゾーン設定
RDSのコンソールでパラメータグループから設定
キー: time_zone 値: Asia/Tokyo
これをやらないとbinlogの時間がUTCになって、リストアする時の時間指定の考慮が発生してしまう^^;
これも変更する
キー: log_bin_trust_function_creators 値: 1
binlogのリストア時のエラーを回避する為
設定反映の確認
mysql> SELECT CURTIME(); +-----------+ | CURTIME() | +-----------+ | 12:27:58 | +-----------+ 1 row in set (0.01 sec)
binlogバックアップ側サーバ側作業
S3へのFullAccessな IAMがEC2に付与されていること
EC2にあらかじめロールを付与しておいてください
RDSの種類とメジャーバージョンとクライアントの種類とメジャーバージョンは合わせましょう
タイムゾーン変更
# sudo cp -p /usr/share/zoneinfo/Japan /etc/localtime
# sudo vi /etc/sysconfig/clock - ZONE="UTC" - UTC=true + ZONE="Asia/Tokyo" + UTC=False
設定反映の確認
# date Mon Nov 15 12:27:24 JST 2021
binlogバックアップサーバでのアプリによるRDS to S3へのbinlogのバックアップ
# aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************HRPZ shared-credentials-file
secret_key ****************RQS/ shared-credentials-file
region <not set> None None
# export AWS_DEFAULT_REGION='ap-northeast-1'
# aws configure IAMの設定してください
EC2にロール設定してたらいらないかも^^;
# aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************HRPZ shared-credentials-file
secret_key ****************RQS/ shared-credentials-file
region ap-northeast-1 env AWS_DEFAULT_REGION
アカウントidの取得
$ AWS_ID=$( \
aws sts get-caller-identity \
--query 'Account' \
--output text \
) && echo ${AWS_ID}
$ S3_BUCKET_PREFIX='sample-db-binlog'
バケット名の決定
$ S3_BUCKET_NAME="${S3_BUCKET_PREFIX}-${AWS_ID}" \
&& echo ${S3_BUCKET_NAME}
バケット「binlog-bucket1」の作成 // 任意で変更してください。
$ aws s3api create-bucket \
--bucket ${S3_BUCKET_NAME} \
--create-bucket-configuration "LocationConstraint=${AWS_DEFAULT_REGION}"
アプリをクローン
$ git clone https://github.com/yuukanehiro/ManageBinlog.git $ cd ManageBinlog
バケットにライフサイクル設定
ManageBinlog $ aws s3api put-bucket-lifecycle-configuration \
--bucket ${S3_BUCKET_NAME} \
--lifecycle-configuration file://binlog_expiration_lifecycle_rules.json
ex. binlog_expiration_lifecycle_rules.json
{
"Rules": [
{
"Filter": {
"Prefix": "testdb/binlog/"
},
"Status": "Enabled",
"Transitions": [
{
"Days": 3,
"StorageClass": "GLACIER"
}
],
"Expiration": {
"Days": 7
},
"ID": "testdb"
}
]
}
・GLACIERに3日経過したbinlogを送信
・30日後に削除
設定したルールの確認
ManageBinlog # aws s3api get-bucket-lifecycle-configuration --bucket ${S3_BUCKET_NAME}
{
"Rules": [
{
"Filter": {
"Prefix": "testdb/binlog/"
},
"Status": "Enabled",
"Transitions": [
{
"Days": 3,
"StorageClass": "GLACIER"
}
],
"Expiration": {
"Days": 7
},
"ID": "testdb"
}
]
}
$ vi /ManageBinlog/backupBinlogFromRdsToS3.sh
・・・
# ----------------------------------------------------------------------
S3_BUCKET_NAME={S3バケット名}
DB_HOST={RDS ホスト名}
DB_PASSWORD={RDS パスワード}
DB_USER={RDS ユーザ}
DB_NAME={RDS DB名}
MAX_DOWNLOAD_BINLOG_COUNT=5 ... binlogファイル取得
TEMP_DIR=./tmp_binlog/${DB_NAME}/ ... 一時的に保存するbinlog
S3_DIR=s3://${S3_BUCKET_NAME}/${DB_NAME}/ ... binlogの保存場所
# ----------------------------------------------------------------------
・・・
$ chmod +x /ManageBinlog/backupBinlogFromRdsToS3.sh $ sh /ManageBinlog/backupBinlogFromRdsToS3.sh
Cron設定
# vi /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed * * * * * root sh /ManageBinlog/backupBinlogFromRdsToS3.sh
反映
$ sudo systemctl restart crond
S3のバケットにファイルがアップロードされることを確認してください。
$ vi /ManageBinlog/downloadBinlogFromS3.sh
・・・
# ----------------------------------------------
S3_BUCKET_NAME={S3バケット名}
DB_NAME={DB名}
STORAGE_DIR=./download/${DB_NAME}/
S3_DIR=s3://${S3_BUCKET_NAME}/${DB_NAME}/
# ---------------------------------------------
・・・
$ chmod +x /downloadBinlogFromS3.sh
$ sh /downloadBinlogFromS3.sh
ダウンロードできるか確認
# ls -laht /ManageBinlog/download/{DB名}/
total 48K
drwxr-xr-x 2 root root 4.0K Nov 15 19:27 .
-rw-r--r-- 1 root root 568 Nov 15 19:27 mysql-bin-changelog.001197
-rw-r--r-- 1 root root 568 Nov 15 19:27 mysql-bin-changelog.001198
-rw-r--r-- 1 root root 568 Nov 15 19:27 mysql-bin-changelog.001199
-rw-r--r-- 1 root root 568 Nov 15 19:27 mysql-bin-changelog.001200
-rw-r--r-- 1 root root 511 Nov 15 19:27 mysql-bin-changelog.001201
-rw-r--r-- 1 root root 568 Nov 15 19:24 mysql-bin-changelog.001196
-rw-r--r-- 1 root root 211 Nov 15 19:19 mysql-bin-changelog.001195
-rw-r--r-- 1 root root 568 Nov 15 19:14 mysql-bin-changelog.001194
-rw-r--r-- 1 root root 568 Nov 15 19:09 mysql-bin-changelog.001193
-rw-r--r-- 1 root root 568 Nov 15 19:04 mysql-bin-changelog.001192
-rw-r--r-- 1 root root 568 Nov 15 18:59 mysql-bin-changelog.001191


