Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding last snapshot using boto

I have read the explanation about "describe_cluster_snapshots" from ...

http://docs.pythonboto.org/en/latest/ref/redshift.html#boto.redshift.layer1.RedshiftConnection.create_cluster

It has an option start_time and end_time but there is no way to sort it. How do I get the id of the latest snapshot using boto?

Here is what I have tried but it does not seem to return the last snapshot.

mysnap=conn.describe_cluster_snapshots()

mysnapidentifier=mysnap['DescribeClusterSnapshotsResponse']['DescribeClusterSnapshotsResult']['Snapshots'][-1]['SnapshotIdentifier'] 
like image 770
shantanuo Avatar asked Feb 03 '26 05:02

shantanuo


1 Answers

There is no way to have the sorting done on the server-side so you will have to sort on the client-side. First, extract the list of snapshot data from the response data:

response = conn.describe_cluster_snapshots()
snapshots = response['DescribeClusterSnapshotsResponse']['DescribeClusterSnapshotsResult']['Snapshots']

The snapshots variable should now be a list of dictionaries where each dictionary represents one snapshot. Each of those dictionaries will have a key called SnapshotCreateTime and you want to sort your list by that value. You can do that like this:

snapshots.sort(key=lambda d: d['SnapshotCreateTime'])

This does an in-place sort so your list will now be modified to have all snapshots in ascending order of that key. You can now just grab the last item in the list:

mysnapidentifier = snapshots[-1]['SnapshotIdentifier']

I don't use Redshift so I don't have live data to test this with but I believe that will give you what you want.

like image 54
garnaat Avatar answered Feb 04 '26 21:02

garnaat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!