DEV Community

Cover image for Monitoring and Managing AWS RDS Snapshot Storage Across All Regions
Dmitry Romanoff
Dmitry Romanoff

Posted on

Monitoring and Managing AWS RDS Snapshot Storage Across All Regions

In the dynamic world of cloud computing, efficient management of your resources is crucial. For AWS users, particularly those utilizing Amazon RDS (Relational Database Service), monitoring database snapshot storage is an essential task. These snapshots, whether manual or automated, can accumulate and impact your storage costs. In this article, we’ll walk through a shell script designed to calculate and report the total size of RDS snapshots across all AWS regions.

The Challenge: Managing RDS Snapshot Storage
AWS RDS snapshots are backups of your databases, allowing you to restore data in case of failures or other issues. Snapshots can be created manually or automatically, and while they provide essential backup capabilities, they also consume storage. Over time, the accumulated size of these snapshots can lead to significant costs if not managed properly.

The Solution: A Shell Script for Snapshot Storage Analysis
The following shell script helps you track and analyze the total storage used by RDS snapshots across all AWS regions. It breaks down the snapshot sizes into manual and automated categories, providing a comprehensive view of your storage utilization.

The Script

#!/bin/bash

now=$(date)
echo ".................................................................. "
echo "Check DBs Snapshots Size Total GB"
echo "Snapshots types: public, shared, manual, awsbackup, automated"
echo "Date: $now "
echo ".................................................................. "
all_regions_total_size_gb=0
# Iterate through each AWS region
for region in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text)
do
    manual_size_gb=0
    manual_num_of_snapshots=0
    # Calculate the total size of manual snapshots in the current region
    for db_snapshot_size in $(aws rds describe-db-snapshots --region ${region} --snapshot-type manual --query 'DBSnapshots[*].[AllocatedStorage]' --output text)
    do
        manual_size_gb=$((manual_size_gb + db_snapshot_size))
        manual_num_of_snapshots=$((manual_num_of_snapshots + 1))
    done
    automated_size_gb=0
    automated_num_of_snapshots=0
    # Calculate the total size of automated snapshots in the current region
    for db_snapshot_size in $(aws rds describe-db-snapshots --region ${region} --snapshot-type automated --query 'DBSnapshots[*].[AllocatedStorage]' --output text)
    do
        automated_size_gb=$((automated_size_gb + db_snapshot_size))
        automated_num_of_snapshots=$((automated_num_of_snapshots + 1))
    done
    # Aggregate sizes for all regions
    all_regions_total_size_gb=$((all_regions_total_size_gb + manual_size_gb + automated_size_gb))
    # Output the snapshot statistics for the current region
    printf "Region: %-15s | Num of Manual Snapshots: %-15s | Size of Manual Snapshots GB: %-15s | Num of Automatic Snapshots: %-15s | Size of Automatic Snapshots GB: %-15s | Total Size Of the Snapshots GB: %-15s  \n" ${region} ${manual_num_of_snapshots} ${manual_size_gb} ${automated_num_of_snapshots} ${automated_size_gb} $((manual_size_gb + automated_size_gb))
done
# Output the total snapshot size across all regions
printf "All the regions total size of snapshots in GB: %-15s \n" ${all_regions_total_size_gb}
Enter fullscreen mode Exit fullscreen mode

Script Breakdown
Initialization: The script begins by capturing the current date and time, providing context for when the snapshot analysis is performed.

Region Iteration: It retrieves a list of all AWS regions and iterates through each one. This ensures that the snapshot analysis covers every region where your RDS instances might be running.

Snapshot Size Calculation: For each region, the script calculates the total size of manual and automated snapshots separately. It uses the AWS CLI to retrieve snapshot data and sum up the allocated storage sizes.

Aggregating and Reporting: The script aggregates the total size of snapshots across all regions and prints detailed information, including the number of snapshots and their sizes. This provides a clear picture of your snapshot storage usage.

Summary: It concludes with a summary of the total snapshot size across all regions, giving you a comprehensive overview of your snapshot storage consumption.

Benefits of This Script
Cost Awareness: By regularly running this script, you can gain insights into your snapshot storage usage, helping you manage and potentially reduce costs.
Efficient Management: Automated reporting simplifies tracking and managing snapshot storage across multiple regions.
Data-Driven Decisions: Detailed breakdowns enable you to make informed decisions about snapshot retention and management policies.

Conclusion
Managing AWS RDS snapshot storage is a crucial aspect of maintaining an efficient and cost-effective cloud environment. This shell script provides a straightforward way to monitor and report on snapshot sizes across all AWS regions, helping you keep a close eye on your storage usage. By integrating such scripts into your regular cloud management practices, you can ensure better resource optimization and cost control.

Feel free to adapt this script to fit your specific needs and environment.

Top comments (0)