AWS

Shell Script to get the versioning status of S3 buckets

Today I received a request to get the versioning status of the buckets used by the engineering team. To check the bucket versioning status, normally you can go the S3 page, select the bucket and see the status of versioning in the properties box.

In the below screenshot, we can see versioning is disabled for this bucket.

Versioning
S3 bucket properties

One can follow this approach for 10, 20, maybe 50 buckets. What if you’ve to check for 100 buckets, 200, 500 buckets?

We can make use of s3api cli command for this requirement.

Note: s3api is installed in almost every ec2 instance. If not, please get it installed before proceeding with this.

root@ip:~# aws s3api get-bucket-versioning --bucket random.bucket.name --region us-east-1
{
"Status": "Enabled"
}

If versioning is enabled, it will give the output as “Status”:”Enabled”; if versioning is suspended, output as “Status”:”Suspended”. If versioning is not enabled, the command returns no output.

 

To print in text format:

root@ip:~# aws s3api get-bucket-versioning --bucket random.bucket.name --region us-east-1 --output text

Enabled

You can configure both the region and the output format in ~/.aws/configure

To check for the list of buckets:

We can use shell script for this requirement.

Save the buckets name in a file and run the below for loop over them.

#!/bin/bash
for buck in $(cat bucketlist.txt)
do
echo "Checking $buck"
aws s3api get-bucket-versioning --bucket $buck --region us-east-1 --output text
echo "------"
done

Output would look like this:

Checking bucket1
Enabled
——
Checking bucket2
Suspended
—–
Checking bucket3
—–
Checking bucket4
Enabled
—–

CSV style output:
The above script helps to find the list of buckets with versioning enabled, but it’s difficult to copy them to a spreadsheet as the output is in two lines and not excel cells friendly.

To get in csv format,

#!/bin/bash
for buck in $(cat bucketlist.txt)
do
printf "$(echo $buck),$(aws s3api get-bucket-versioning --bucket $buck --region us-east-1 --output text) \n"
done

Now the output would look like this.
Bucket1,Enabled
Bucket2,Suspended
Bucket3,
Bucket4,Enabled

It’s easier to copy this to excel and split it into columns.

Further optimisation of the script:

Instead of manually writing the buckets in a file and run a for loop over them, we can get the list of buckets using s3 ls command and pass it to the loop.


#!/bin/bash
read -p "Enter the region name: " REG
aws s3 ls --region $REG | awk '{print $3}' > ~/bucketlist.txt
for buck in $(cat ~/bucketlist.txt)
do
printf "$(echo $buck),$(aws s3api get-bucket-versioning --bucket $buck --region $REG) \n"
done

 

 

One thought on “Shell Script to get the versioning status of S3 buckets

Leave a Reply

Your email address will not be published. Required fields are marked *