missing-cn 阅读(35) 评论(0)

输出mongodb所有库中各表的大小的shell

环境

  • mongodb 3.4.4
  • centos 7

shell

[root@db01 ~]# cat get_table_size.sh
#!/bin/sh
MONGO=/usr/bin/mongo
function get_dbs() {
  $MONGO << EOF
show dbs
EOF
}
function get_tables() {
    # $1 is dbname
    DB=$1
    $MONGO  << EOF
use $DB ;
show tables;
EOF
}
function get_a_table_size() {
  DB=$1
  TABLE=$2
  $MONGO  << EOF
use $DB ;
db.$TABLE.totalSize()
EOF
}

function get_size() {
  for dbname in `get_dbs |head -n -1 |tail -n +5|awk '{print $1}'`
  do
    for i in `get_tables $dbname |head -n -1 |tail -n +5`
    do
      SIZE=`get_a_table_size $dbname $i |head -n -1 |tail -n +5`
      echo "$dbname  $i    $SIZE"
    done
  done
}

get_size $1 |sort  -k3nr