Last active
February 21, 2018 04:48
-
-
Save bidiu/7b1fae658e9bb0f43d1a9ef978602526 to your computer and use it in GitHub Desktop.
Back up Elasticsearch indices.
This file contains hidden or 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
import time | |
import datetime | |
from elasticsearch import Elasticsearch, helpers | |
# the underlying Elasticsearch client | |
es = Elasticsearch(['localhost:9200'], | |
sniff_onz_start=True, | |
sniff_on_connection_fail=True, | |
sniffer_timeout=60) | |
def backup_index(self, index_name): | |
'''Utility for backing up any index. | |
''' | |
def timestamp_to_date(timestamp): | |
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d') | |
def gen_backup_index_name(): | |
while True: | |
timestamp = int(time.time()) | |
date_str = timestamp_to_date(timestamp) | |
backup_index = '{}_backup_{}_{}'.format(index_name, date_str, timestamp) | |
if not self._index_exists(backup_index): | |
return backup_index | |
def action_generator(): | |
doc_generator = helpers.scan(es, | |
preserve_order=True, | |
index=index_name, | |
doc_type=DOC_TYPE_NAME, | |
query={'query': {'match_all': {}}}, | |
sort='order:asc') | |
backup_index = gen_backup_index_name() | |
for doc in doc_generator: | |
# set meta data | |
doc['_op_type'] = 'index' | |
doc['_index'] = backup_index | |
yield doc | |
# start backup | |
helpers.bulk(es, action_generator()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment