From b557fcc9636e29c7996c68f29bfce4dbe842829b Mon Sep 17 00:00:00 2001 From: Fernando Llaca Date: Wed, 8 Jan 2020 12:29:51 +0100 Subject: [PATCH] Use timestamp to deduplicate snapshots of same index --- es-cleanup.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/es-cleanup.py b/es-cleanup.py index 09240ae..c37d352 100644 --- a/es-cleanup.py +++ b/es-cleanup.py @@ -163,8 +163,12 @@ def snapshot_index(self, index_name, snapshot_repository): "include_global_state": False } + # Append a timestamp to deduplicate multiple snapshots for same index + now = str(datetime.datetime.now()) + snapshot_name = format("{}-{}".format(index_name, now) + # create snapshot - snapshot = self.send_to_es("_snapshot/{}/{}".format(snapshot_repository, index_name), method="PUT", payload=snapshot_payload) + snapshot = self.send_to_es("_snapshot/{}/{}".format(snapshot_repository, snapshot_name), method="PUT", payload=snapshot_payload) # Wait for snapshot to be sucessful retries = 0 @@ -172,7 +176,7 @@ def snapshot_index(self, index_name, snapshot_repository): if retries > 0: seconds = (5**retries) * .1 time.sleep(seconds) - snapshots = self.get_snapshot(snapshot_repository, index_name) + snapshots = self.get_snapshot(snapshot_repository, snapshot_name) if snapshots["snapshots"][0]["state"] == "SUCCESS": break return snapshot @@ -258,7 +262,7 @@ def lambda_handler(event, context): snapshot_split = snapshot["snapshot"].rsplit("-", 1 + es.cfg["index_format"].count("-")) snapshot_name = snapshot_split[0] - snapshot_date = '-'.join(word for word in snapshot_split[1:]) + snapshot_date = '-'.join(word for word in snapshot_split[1]) if snapshot_date <= snapshot_earliest_to_keep.strftime(es.cfg["index_format"]): # Delete snapshot