Itamar Bitton
Itamar Bitton

Reputation: 786

How to find latest or most recent AWS RDS snapshot?

I can call aws rds describe-db-snapshots --db-instance-identifier {my_db_instance} and sort all automated snapshots to find the most recently created one but I was hoping someone has a better idea out there.

Upvotes: 27

Views: 22828

Answers (8)

kalyani chaudhari
kalyani chaudhari

Reputation: 7849

If it is RDS cluster then you can use below command:

aws rds describe-db-cluster-snapshots --db-cluster-identifier <DBClusterIdentifier> --region <region> --query="max_by(DBClusterSnapshots, &SnapshotCreateTime)"

you can use below command to fetch specific snapshot ARN:

aws rds describe-db-cluster-snapshots --db-cluster-identifier <DBClusterIdentifier> --region <region> --query="max_by(DBClusterSnapshots, &SnapshotCreateTime).DBClusterSnapshotArn" 

Upvotes: 0

Jack Salvador
Jack Salvador

Reputation: 15

aws rds describe-db-cluster-snapshots --snapshot-type=automated --query="max_by(DBClusterSnapshots,&SnapshotCreateTime)"

This works in 2022.08

Upvotes: 0

hey
hey

Reputation: 3242

For me, this one works:

aws rds describe-db-snapshots \
  --query="max_by(DBSnapshots, &SnapshotCreateTime)"

The query parameter returns only the most recent one.

If only the Arn is needed, this one might help:

aws rds describe-db-snapshots \
  --query="max_by(DBSnapshots, &SnapshotCreateTime).DBSnapshotArn" \
  --output text

And all that for a specific database instance:

aws rds describe-db-snapshots \
  --db-instance-identifier={instance identifier} \
  --query="max_by(DBSnapshots, &SnapshotCreateTime).DBSnapshotArn" \
  --output text

Upvotes: 56

FacePalm
FacePalm

Reputation: 11738

If someone is looking for cluster command:

aws rds describe-db-cluster-snapshots --db-cluster-identifier prod --snapshot-type automated --query "DBClusterSnapshots[?SnapshotCreateTime>='2017-06-05'].DBClusterSnapshotIdentifier"

Upvotes: 3

Arbab Nazar
Arbab Nazar

Reputation: 23791

I am getting this error while restoring the db from snapshot with the id that I get with the command from the above methods:

An error occurred (InvalidParameterValue) when calling the RestoreDBInstanceFromDBSnapshot operation: Invalid snapshot identifier:  "rds:dev-mysql-rds1-2018-10-06-01-09"

So, I have modified the above query to make it work for me, here is a query that worked for me to get the latest snapshot that worked with restore-db-instance-from-db-snapshot

aws rds describe-db-snapshots --query "DBSnapshots[?DBInstanceIdentifier=='MASTER_INSTANCE_IDENTIFIER']" | jq -r 'max_by(.SnapshotCreateTime).DBSnapshotIdentifier'

Upvotes: 0

Nick E
Nick E

Reputation: 129

I know this is old, but I was needing to know the same information and was able to construct the following which will then just give me the snapshot name. It doesn't totally answer your question about emphatically finding the latest snapshot but in this example might give you some better direction.

aws rds describe-db-snapshots --db-instance-identifier prd --snapshot-type automated --query "DBSnapshots[?SnapshotCreateTime>='2017-06-05'].DBSnapshotIdentifier"

To break it down with the options

--db-instance-identifier (put in your instance name your are looking for)

--snapshot-type (I put in automated to find the automated backups)

--query "DBSnapshots[?SnapshotCreateTime>='2017-06-05'].DBSnapshotIdentifier"

(This is what I used to refine my search as we do daily backups, I just look for the snapshot create time to be greater than today and by giving the .DBSnapshotIdentifier gives me back just the name.

Hopefully this will help somebody else out.

Upvotes: 12

user1767316
user1767316

Reputation: 3641

My way:

> aws rds describe-db-snapshots --db-instance-identifier ${yourDbIdentifier} --query="reverse(sort_by(DBSnapshots, &SnapshotCreateTime))[0]|DBSnapshotIdentifier"
> "rds:dbName-2018-06-20-00-07"

Upvotes: 4

Piyush Patil
Piyush Patil

Reputation: 14533

As at 31th October 2014, it looks like you can use the --t flag to list only automated backups.

http://docs.aws.amazon.com/AmazonRDS/latest/CommandLineReference/CLIReference-cmd-DescribeDBSnapshots.html

From there, you should be able to parse the output to determine your latest snapshots.

rds-describe-db-snapshots --t automated

DBSNAPSHOT  rds:<NAME>-2016-08-09-17-12  

There is no any other more simple way around for this.

Upvotes: 1

Related Questions