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 d464fb5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 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 @@ -255,7 +259,10 @@ def lambda_handler(event, context):
snapshot_earliest_to_keep = datetime.date.today() - datetime.timedelta(
days=int(es.cfg["snapshot_delete_after"]))
for snapshot in es.get_snapshots(es.cfg["snapshot_repository"])["snapshots"]:
snapshot_split = snapshot["snapshot"].rsplit("-",
# split by "-", ignoring the timestamp part of the snapshot name:
timestamp_pos=snapshot["snapshot"].rfind("_")
timestamp_pos=timestamp_pos+1 if timestamp_pos >= 0 else len(snapshot["snapshot"])
snapshot_split = snapshot["snapshot"][:timestamp_pos].rsplit("-",
1 + es.cfg["index_format"].count("-"))
snapshot_name = snapshot_split[0]
snapshot_date = '-'.join(word for word in snapshot_split[1:])
Expand Down

0 comments on commit d464fb5

Please sign in to comment.