Skip to content

Instantly share code, notes, and snippets.

@okiwan
Created June 18, 2020 15:26
Show Gist options
  • Select an option

  • Save okiwan/11ea94c8d215774ba93b7bf84a58dc2a to your computer and use it in GitHub Desktop.

Select an option

Save okiwan/11ea94c8d215774ba93b7bf84a58dc2a to your computer and use it in GitHub Desktop.
import boto3
import requests
from requests_aws4auth import AWS4Auth
# Origin: host = 'https://my-elasticsearch-endpoint.com/'
# Destination:
host = 'https://my-elasticsearch-enpoint-destination.com/'
region = 'eu-west-1' # 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)
r = requests.delete(host + "restored-servicelogs", auth=awsauth)
print(r.status_code)
print(r.text)
exit()
# Delete index
#
#index_reference = [
# "restored-things",
# "restored-abc",
# "restored-logstash"
#]
#
#for index in index_reference:
# r = requests.delete(host + index, auth=awsauth)
# print(r.status_code)
# print(r.text)
# Check if there is any task running for a specific action (check if it is still running after a 504)
# path = "_tasks?actions=*reindex&detailed"
# url = host + path
# r = requests.get(url, auth=awsauth)
# print(r.status_code)
# print(r.text)
# Copy missing documents from one index to another
#
#path = "_reindex"
#url = host + path
#index_reference = [
# ("servicelogs-test-tda", "2020.24"),
# ("servicelogs-lalala-tda", "2020.24"),
#]
#for index_name, index_suffix in index_reference:
# r = requests.head(host + "restored-" + index_name, auth=awsauth)
#
# if r.status_code == 200:
# payload = {
# "conflicts": "proceed",
# "source": {
# "index": "restored-" + index_name
# },
# "dest": {
# "index": index_name + "-" + index_suffix,
# "op_type": "create"
# }
# }
# headers = {"Content-Type": "application/json"}
# s = requests.post(url, auth=awsauth, json=payload, headers=headers)
# print(s.status_code)
# print(s.text)
#
# if s.status_code != 200:
# exit()
# Restore snapshot (all indices except Kibana and fine-grained access control)
#
#
#path = '_snapshot/my-snapshot-repo/full-snapshot-3/_restore'
#url = host + path
#payload = {
# "indices": "-.kibana*,-.opendistro_security",
# "include_global_state": False,
# "rename_pattern": "(.+)-(2020\.24|2020\.06\.09)",
# "rename_replacement": "restored-$1"
#}
#headers = {"Content-Type": "application/json"}
#r = requests.post(url, auth=awsauth, json=payload, headers=headers)
#print(r.status_code)
#print(r.text)
# Delete latest indexes
#
#latest_indexes = [
# "servicelogs-test-tda-2020.24",
# "servicelogs-lalala-tda-2020.24",
#]
#for index in latest_indexes:
# url = host + index
# r = requests.delete(url, auth=awsauth)
# print(r.status_code)
# print(r.text)
# Check details snapshot status
#
#path = '_snapshot/my-snapshot-repo/_all?pretty'
#url = host + path
#r = requests.get(url, auth=awsauth)
#print(r.status_code)
#print(r.text)
# Check snapshot status
#
#path = '_snapshot/_status'
#url = host + path
#r = requests.get(url, auth=awsauth)
#print(r.status_code)
#print(r.text)
# Take snapshot
#
#path = '_snapshot/my-snapshot-repo/full-snapshot'
#url = host + path
#r = requests.put(url, auth=awsauth)
#print(r.text)
# Register repository
#
#path = '_snapshot/my-snapshot-repo' # the Elasticsearch API endpoint
#url = host + path
#payload = {
# "type": "s3",
# "settings": {
# "bucket": "test-snapshot-es-sync-socana",
# "region": "us-west-2",
# "role_arn": "arn:aws:iam::264159233651:role/SnapshotRole"
# }
#}
#headers = {"Content-Type": "application/json"}
#r = requests.put(url, auth=awsauth, json=payload, headers=headers)
# Restore snapshot (all indices except Kibana and fine-grained access control)
#
#
# path = '_snapshot/my-snapshot-repo/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)
#
# Restore snapshot (one index)
#
# path = '_snapshot/my-snapshot-repo/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)
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment