Skip to content

Commit

Permalink
Use timestamp to deduplicate snapshots of same index
Browse files Browse the repository at this point in the history
  • Loading branch information
fllaca committed Jan 8, 2020
1 parent 03ec523 commit b557fcc
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions es-cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,20 @@ 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
while retries < int(self.cfg["es_max_retry"]):
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b557fcc

Please sign in to comment.