Last active
April 24, 2020 08:46
-
-
Save rafaelfelix/45ada15a4e77311743da15df7e394841 to your computer and use it in GitHub Desktop.
Amazon ES index rotation script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import datetime | |
from requests_aws4auth import AWS4Auth | |
from elasticsearch import Elasticsearch, RequestsHttpConnection | |
import boto3 | |
host = '' # For example, search-my-domain.region.es.amazonaws.com | |
region = '' # 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) | |
days = 7 # keep only one week | |
es = Elasticsearch( | |
hosts = [{'host': host, 'port': 443}], | |
http_auth = awsauth, | |
use_ssl = True, | |
verify_certs = True, | |
connection_class = RequestsHttpConnection | |
) | |
to_be_deleted = [] | |
for i in es.indices.get('*'): | |
try: | |
i_d = datetime.datetime.strptime(i.split('-')[-1], '%Y.%m.%d') | |
if (datetime.datetime.now() - i_d) > datetime.timedelta(days=days) and not (i.startswith('keep-this-') or i.startswith('also-keep-this-')): | |
to_be_deleted.append(i) | |
except ValueError: | |
print("ERROR: ", i) | |
continue | |
for i in to_be_deleted: | |
es.indices.delete(i) | |
print('deleted ', i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment