Skip to content

Instantly share code, notes, and snippets.

@sburlot
Last active April 23, 2024 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sburlot/803a0e1222b57f9959d9926a3c56e14c to your computer and use it in GitHub Desktop.
Save sburlot/803a0e1222b57f9959d9926a3c56e14c to your computer and use it in GitHub Desktop.
Report size of all Backblaze B3 folders in bucket with S3CMD
#!/usr/bin/perl
# I use updraft to backup WordPress sites, I need to monitor BackBlaze/b2 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 sburlot@coriolis.ch Apr 2024
#
use Data::Dumper;
use MIME::Lite;
use POSIX;
# change the config file here if needed
$s3cmd = "s3cmd --config=/home/stephan/.s3cfg_b2 ";
$b2_bucket = "s3://secret-updraft/";
$email_address = 'me@example.com';
################################################################################################
# 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