Forked from kiantabrizi/getTotalSizeofDatabases.js
Last active
September 9, 2015 16:08
-
-
Save braz/495c35b05595f18c3713 to your computer and use it in GitHub Desktop.
MongoDB Cloud Manager Backup: calculating how much you will be charged
This file contains hidden or 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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment