AWS/OpenSearch迁移数据至自建Elasticsearch

Friday, June 17, 2022

TOC

准备

OpenSearch准备

export ES_DOMAIN=https://your.opensearch.domain

# 查看现有snapshot
curl -X GET ${ES_DOMAIN}/_snapshot/_all

在OpenSearch创建新的S3-repository

这篇文章有多种方式的完整教程,这里以python为例

import boto3
import requests
from requests_aws4auth import AWS4Auth

host = '' # 替换为host, 包括/结尾
region = '' # e.g. us-west-1
service = 'es'
credentials = boto3.Session( ##### 这里尤其注意!!! 官方给的demo貌似没法使用,亲测换成这种方案可行
        aws_access_key_id='', # 替换你的access_key
        aws_secret_access_key='' # 替换你的secret_key
        ).get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

# Register repository

path = '_snapshot/bucket_name' # bucket_name可以换成你的名称, 但是下面需要一并修改
url = host + path

payload = {
        "type": "s3",
        "settings": {
            "bucket": "bucket_name", # bucket_name
            "region": "", # region
            "role_arn": "" # role_arn
            }
        }

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

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

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

自建ES集群准备

export ES_DOMAIN=https://your.es.domain

# 查看现有snapshot
curl -X GET ${ES_DOMAIN}/_snapshot/_all

在ES创建S3-repository(指向相同的s3地址)

  • Step0. 启动repository-s3插件

    如果你是非snapshot的ES版本,通过${ES_HOME}bin/elasticsearch-plugin install repository-s3安装即可

    如果你是snapshot版本,则需先获取一个对应ES版本的snapshot版repository-s3插件(简单方法是从源码编译一个)放在${ES_HOME}/plugins目录下

  • Step1. 为所有ES节点授权访问S3

    $ES_HOME/bin/elasticsearch-keystore add s3.client.default.access_key
    <enter your access key>
    
    $ES_HOME/bin/elasticsearch-keystore add s3.client.default.secret_key
    <enter your secret key>
    
  • step2. 创建repository

    curl -XPUT "${ES_DOMAIN}/_snapshot/bucket_name" -H 'Content-Type: application/json' -d'
    {
      "type": "s3",
      "settings": {
        "bucket": "bucket_name", # 替换
        "region": "",            # 替换
        "role_arn": "arn:aws:iam::039043842901:role/bucket_name" # 替换
      }
    }'
    

    创建成功之后,可以通过GET _snapshot/_all 看到你刚创建的名叫bucket_name的快照仓库。接下来就是创建/恢复快照了。

快照创建/恢复

创建快照

curl -XPUT "${ES_DOMAIN}/_snapshot/bucket_name/snap_name?wait_for_completion=true" -H 'Content-Type: application/json' -d'
{
  "indices": "index_name1,index_name2,...",
  "ignore_unavailable": false,
  "include_global_state": true,
  "metadata": {
    "taken_by": "creator name",
    "taken_because": "backup before upgrading"
  }
}'

恢复快照

curl -XPOST "${ES_DOMAIN}/_snapshot/bucket_name/snap_name/_restore"

查看恢复进度

curl -XGET "${ES_DOMAIN}/_snapshot/bucket_name/snap_name"