Backup and Restore of the Elasticsearch Service in AWS S3 bucket

Introduction

This article will help you in taking the manual snapshot of your domain in Amazon Elasticsearch Service. We can easily back up our entire domain this way. You can also take snapshot and restore a single index, or multiple indexes. This blog post walks you through backing up and restoring a single index by using an Amazon S3 bucket.

Steps for Backup of the Elasticsearch Service

  1. Create the s3 bucket
  2. Create the role and attached inline policy
{
    "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/*"
            ]
        }
    ]

}

3. Update the trust relationship for role.
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "es.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

4. Create the AWS user with secret access key and attached this inline policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "iam:PassRole",
            "Resource": "arn:aws:iam::<AWS_ACCOUNT_NUMBER>:role/ROLE-NAME"
        },
        {
            "Effect": "Allow",
            "Action": "es:ESHttpPut",
            "Resource": "arn:aws:es:us-west-2:
<AWS_ACCOUNT_NUMBER>:domain/ES-CLUSTER-NAME/*"
        }
    ]

}

5. Install some prerequisites packages in the instance from where you want to run the backup script and update the AWS secret key also from where you running this backup script.
  • yum -y install python-pip
  • pip install requests-aws4auth

6. Update the ES host which you want to take the backup and bucket info.
  • vim /tmp/register-repo.py
import boto3
import requests
from requests_aws4auth import AWS4Auth

host = 'https://search-mb-production-app-us-west-2.es.amazonaws.com/'
region = 'us-west-2' # For example, 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' # the Elasticsearch API endpoint
url = host + path

payload = {
  "type": "s3",
  "settings": {
    "bucket": "elasticsearch-backup-indices",
    "region": "us-west-2",
    "role_arn": "arn:aws:iam::YOUR-ACCOUNT-ID:role/es-s3-backup"
  }
}

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

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

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


7. Execute the file to register repository.
  • chmod 700 /tmp/register-repo.py
  • python /tmp/register-repo.py
Output should be ,
200
{"acknowledged":true}


8. To list all the snapshot from the ES Cluster this is automated snapshot
  • curl -XGET '<Elasticsearch_Endpoint>/_snapshot/cs-automated/_all?pretty'
9. Run this command for taking manual backup.
  • curl -XPUT 'elasticsearch-domain-endpoint/_snapshot/repository/snapshot-name'
10. Use the following command to verify the state of snapshots of your domain:
  • curl -XGET ‘<Elasticsearch_Endpoint>/_snapshot/my-snapshot-repo/_all?pretty'

Steps for Restore the backup data in New ES Cluster(AWS Managed)

1. Create the ES Cluster : List down this all details from this Cluster Info, ES endpoint, Domain ARN, Kibana

2. Data Saved in S3 Bucket : List S3 bucket ARN in which data is stored.
Bucket Info : arn:aws:s3:::<Bucket-name>

3. Create IAM role, Attached one inline policy, Update s3 ARN
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:ListBucket"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::<Bucket-name>"
            ]
        },
        {
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::<Bucket-name>/*"
            ]
        }
    ]
}


4. Create one User and attached one inline policy, Update role ARN and ES ARN
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "iam:PassRole",
            "Resource": "arn:aws:iam::
<AWS_ACCOUNT_NUMBER>:role/<ROLE_NAME>"
        },
        {
            "Effect": "Allow",
            "Action": "es:ESHttpPut",
            "Resource": "arn:aws:es:us-west-2:
<AWS_ACCOUNT_NUMBER>:domain/<ES_DOMAIN_NAME>/*"
        }
    ]
}

5. Now Take the AWS Secret access key and update in "aws configure" instance from Where you have to run the script. and update the S3 and ES Cluster info in register-repo.py script.
import boto3
import requests
from requests_aws4auth import AWS4Auth

host = '<ES Endpoint e.g.https://vpc-es-testing-backup-data-g6yblp2q64t3ruenxgyqyztjra.us-west-2.es.amazonaws.com/>'
region = 'us-west-2' # For example, 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' # the Elasticsearch API endpoint
url = host + path

payload = {
  "type": "s3",
  "settings": {
    "bucket": "<BUCKET-NAME>",
    "region": "us-west-2",
    "role_arn": "arn:aws:iam::<AWS_ACCOUNT_NUMBER>:role/<ROLE_NAME"
  }
}

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

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

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


6. Login in elasticsearch head and delete the .kibana indices
7. For restore the data run this command
  • curl -XPOST "<ES ENDOPINT>/_snapshot/my-snapshot-repo/<SNAPSHOT_NAME/_restore?pretty"
for e.g.
  • curl -XPOST "https://vpc-anshu-k-soni-12dsynuqyigf7birwuzortpvcq.us-west-2.es.amazonaws.com/_snapshot/my-snapshot-repo/2019-01-28t06-14-24.35f95d80-ebd7-47d4-b808-6a0ecd587y608/_restore?pretty"









Comments

Popular posts from this blog

What is the difference between the Roles and Policy in AWS.

How will you use the ec2 instances if you lost .pem key when you installed first time? How to login now to that ec2 instance.

Overview of SSL/TLS and encryption