2023-10-02|閱讀時間 ‧ 約 8 分鐘

Amazon OpenSearch snapshots to S3 (工作日常)


Prerequisites

  1. 建立 S3 bucket
  2. 設定 IAM role for OpenSearch (TheSnapshotRole)
{
"Version": "2012-10-17",
"Statement": [{
"Action": [
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::s3-bucket-name"
]
},
{
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::s3-bucket-name/*"
]
}
]
}

trust relationship

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

Iam User Permissions(或加到TheSnapshotRole)

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::123456789012:role/TheSnapshotRole"
},
{
"Effect": "Allow",
"Action": "es:ESHttpPut",
"Resource": "arn:aws:es:region:123456789012:domain/domain-name/*"
}
]
}


Registering a manual snapshot repository


OpenSearch Dashboards

 選擇 Security

選擇 Roles (mange_snapshots)

點選 Mange mapping

填入對應的權限

使用 Dev tools



使用python 呼叫api方式備份

import boto3
import requests
from requests_aws4auth import AWS4Auth

host = 'https://search-mingtestopensearch11-4xnbpzmrxv7m7ldifottug6icm.us-east-1.es.amazonaws.com/' # include https:// and trailing /
region = 'us-east-1' # e.g. us-west-1
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

# Register repository

path = '_snapshot/my-snapshot-repo-ming' # the OpenSearch API endpoint
url = host + path

payload = {
"type": "s3",
"settings": {
"bucket": "mingtestopensearch11",
"region": "us-east-1",
"role_arn": "arn:aws:iam::XXXX:role/TheOpensearchSnapshotRole"
}
}

headers = {"Content-Type": "application/json"}

r = requests.put(url, auth=awsauth, json=payload, headers=headers)

print(r.status_code)
print(r.text)

# # Take snapshot
#
# path = '_snapshot/my-snapshot-repo-name/my-snapshot'
# url = host + path
#
# r = requests.put(url, auth=awsauth)
#
# print(r.text)
#
# # Delete index
#
# path = 'my-index'
# url = host + path
#
# r = requests.delete(url, auth=awsauth)
#
# print(r.text)
#
# # Restore snapshot (all indexes except Dashboards and fine-grained access control)
#
# path = '_snapshot/my-snapshot-repo-name/my-snapshot/_restore'
# url = host + path
#
# payload = {
# "indices": "-.kibana*,-.opendistro_security",
# "include_global_state": False
# }
#
# headers = {"Content-Type": "application/json"}
#
# r = requests.post(url, auth=awsauth, json=payload, headers=headers)
#
# print(r.text)
#
# # Restore snapshot (one index)
#
# path = '_snapshot/my-snapshot-repo-name/my-snapshot/_restore'
# url = host + path
#
# payload = {"indices": "my-index"}
#
# headers = {"Content-Type": "application/json"}
#
# r = requests.post(url, auth=awsauth, json=payload, headers=headers)
#
# print(r.text)


參考

https://docs.aws.amazon.com/opensearch-service/latest/developerguide/managedomains-snapshots.html

https://www.youtube.com/watch?v=A_eFCpyl_Fg

分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.