Skip to content

Instantly share code, notes, and snippets.

@kiantabrizi
Created June 30, 2015 14:54
Show Gist options
  • Save kiantabrizi/9dc1dc5cfb1e655a1841 to your computer and use it in GitHub Desktop.
Save kiantabrizi/9dc1dc5cfb1e655a1841 to your computer and use it in GitHub Desktop.
MongoDB Cloud Manager Backup: calculating how much you will be charged
var dbNames = [];
var sum = 0;
var dblist = db.getMongo().getDBs();
for (var key in dblist) {
if(key === "databases") {
for (var i = 0; i < dblist.databases.length; i++){
if (dblist.databases[i].name !== "local") {
dbNames.push(dblist.databases[i].name);
}
}
}
}
function formatSizeUnits(bytes){
if (bytes>=1000000000) {bytes=(bytes/1000000000).toFixed(2)+' GB';}
else if (bytes>=1000000) {bytes=(bytes/1000000).toFixed(2)+' MB';}
else if (bytes>=1000) {bytes=(bytes/1000).toFixed(2)+' KB';}
else if (bytes>1) {bytes=bytes+' bytes';}
else {bytes='0 byte';}
return bytes;
}
for (var i = 0; i < dbNames.length; i++) {
var indexSize = db.getMongo().getDB(dbNames[i]).stats().indexSize;
var dataSize = db.getMongo().getDB(dbNames[i]).stats().dataSize;
var total = indexSize + dataSize;
sum += total;
print("db name: " + dbNames[i] + " | indexSize: " + formatSizeUnits(indexSize) + " | dataSize: " + formatSizeUnits(dataSize) + " | total: " + formatSizeUnits(total));
}
print("total size of all dbs: " + formatSizeUnits(sum));
@kiantabrizi
Copy link
Author

For every primary in the deployment (including every primary for every shard in a sharded cluster), sum the total dataSize and indexSize. Here’s a little script that does this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment