Last active
April 23, 2024 07:47
-
-
Save sburlot/f4624154837b49a45a6950703619164f to your computer and use it in GitHub Desktop.
Report size of all Amazon S3 buckets with S3CMD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# I use updraft to backup WordPress sites, I need to monitor S3 bucket size because money | |
# This script will report the size used by all buckets, used to monitor the | |
# size of all buckets. | |
# It uses s3cmd https://s3tools.org/s3cmd | |
# Outputs and send via email the size of all buckets, sorted by size, with | |
# total size of all buckets | |
# | |
# Stephan Burlot [email protected] Apr 2024 | |
# | |
use Data::Dumper; | |
use MIME::Lite; | |
use POSIX; | |
# change the config file here if needed | |
$s3cmd = "s3cmd --config=/home/stephan/.s3cfg "; | |
$email_address = '[email protected]'; | |
# I use the same script to check backblaze/b2 buckets, so for now this is empty | |
$b2_bucket = ""; | |
################################################################################################ | |
# general routine to send results via email | |
sub send_email($) { | |
my $message = shift @_; | |
my $msg = MIME::Lite->new( | |
From => $email_address, | |
To => $email_address, | |
Subject => 'Backups S3 Buckets Sizes', | |
Data => "Amazon S3 usage:\n\n$message\n\n" | |
); | |
$msg->send; | |
} | |
################################################################################################ | |
## get list of buckets/folders | |
$cmd = $s3cmd . "ls " . $b2_bucket; | |
$response = qx/$cmd/; | |
my $status = ""; | |
# extract name of buckets/folders from s3cmd response | |
# bucket/folder name is column 3 | |
my @BUCKETS = (); | |
for $line (split("\n", $response)) { | |
$bucket = (split(/\s+/, $line))[2]; | |
push @BUCKETS, $bucket; | |
} | |
# enumerate all buckets/folders and get the size | |
for $bucket_url (@BUCKETS) { | |
$cmd = $s3cmd . " du $bucket_url"; | |
$response = qx/$cmd/; | |
chomp($response); | |
$response =~ s/^\s+//g; | |
($bucket = $bucket_url) =~ s/$b2_bucket//; | |
$bucket =~ s/\/$//; | |
$size = (split(/\s+/, $response))[0]; | |
# we get the size in bytes and transform in M | |
# there's no way to directly get the size in M | |
$size = ceil($size/1048576) . "M"; | |
# that's not what I would call error checking | |
if ($response eq "") { | |
print( "Bucket: $bucket is empty\n" ); | |
next; | |
} | |
printf("Bucket: %-10s %s\n", $size, $bucket); | |
$status .= sprintf("%s\t%s\n", $size, $bucket); | |
} | |
## get the total size of the bucket/folder. We could add all folder size, but I prefer to be sure | |
$total_cmd = $s3cmd . "du -H " . $b2_bucket; | |
$response = qx/$total_cmd/; | |
$response = (split("\n", $response))[-1]; | |
$response =~ s/^\s+//g; | |
$size = (split(/\s+/, $response))[0]; | |
# sort the bucket list by size | |
$cmd = "echo \"$status\" | sort -n"; | |
$sorted = qx/$cmd/; | |
$sorted .= "--------------\n$size\tTotal\n"; | |
print "\n\nSorted:\n$sorted\n"; | |
# send the result via email | |
send_email($sorted); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment