I am trying to list all EBS volume snapshot which is taken on a particular date so that i can automate a copy via bash script across region for better disaster recovery
I have an another bash script that creates snapshots of all in use EBS volumes and delete all which is older then 30 days. I need to copy all which is taken on a previous date across an another region.
I tried many jmespath switches (which is not giving any output) some of them are as:-
$aws ec2 describe-snapshots --query 'Sanpshots[?StartTime >= `2018-06-25`]|[?StartTime <= `2018-06-27`]'
$aws ec2 describe-snapshots --query 'Sanpshots[?StartTime == `2018-06-25`]
I looked many pages but not able to find for a particular date listing.
Please suggest some switches,sorting methods,links or anything.
Thanks.
Given that you'll need some programmatic way of calculating "30 days ago", you're better-off doing this in a programming language, such as:
import boto3
import pytz
from datetime import datetime, timedelta
# Get my AWS Account ID
myAccount = boto3.client('sts').get_caller_identity()['Account']
# Connect to EC2
client = boto3.client('ec2', region_name = 'ap-southeast-2')
# Get a list of snapshots for my AWS account (not all public ones)
snapshots = client.describe_snapshots(OwnerIds=[myAccount])['Snapshots']
# Find snapshots more than 30 days old
oldest_date = datetime.now(pytz.utc) - timedelta(days=30)
old_snapshots = [s for s in snapshots if s['StartTime'] < oldest_date]
# Delete the old snapshots
for s in old_snapshots:
client.delete_snapshot(SnapshotId = s['SnapshotId'])
I Figured the JMESpath switch from doc right here.
So in order to search for a particular date i applied a switch which search between two dates. for example:-
'Snapshots[?(StartTime >= `2018-06-27`) && (StartTime <= `2018-06-28`)]
Reason why "==" is not working in the switch as it used for exact match string.
So the complete string is :-
aws ec2 describe-snapshots --query 'Snapshots[?(StartTime >= `2018-06-27`) && (StartTime <= `2018-06-28`)].{ID:SnapshotId,ST:StartTime}' --output text --region $regionname