Using the AWS CLI, how can I get the most recent snapshot for a particular DB instance?
I can get them through the GUI easily but I'd like to automate it.
CodePudding user response:
You can use the aws rds describe-db-snapshots
CLI command to get a list of DB snapshots, and then run a local query using --query
to get the latest DB snapshot using the SnapshotCreateTime
field.
SnapshotCreateTime -> (timestamp)
Specifies when the snapshot was taken in Coordinated Universal Time (UTC). Changes for the copy when the snapshot is copied.
Something like this:
aws rds describe-db-snapshots \
--db-instance-identifier your-id \
--query "sort_by(DBSnapshots, &SnapshotCreateTime)[-1].{id:DBSnapshotIdentifier,time:SnapshotCreateTime}"
Note that this query sorts snapshots by their ascending SnapshotCreateTime and then simply takes the last one in the list (as dictated by [-1]), which will be the one that was last created.
[Added] if you're looking for snapshots of Aurora DB clusters then you'd have to use describe-db-cluster-snapshots
in place of describe-db-snapshots
, but otherwise the process is similar: use DBClusterSnapshots
and DBClusterSnapshotIdentifier
(in place of DBSnapshots
and DBSnapshotIdentifier
).