此脚本是获取MongoDB数据量大小脚本,有需要朋友可以参考,脚本内容如下:
环境准备:
操作系统:CentOS Linux release 7.8.2003文章源自小柒网-https://www.yangxingzhen.cn/7841.html
软件版本:
MongoDB server version: 3.4.24文章源自小柒网-https://www.yangxingzhen.cn/7841.html
[root@MongoDB ~]# vim auto_get_mongodb_data.sh文章源自小柒网-https://www.yangxingzhen.cn/7841.html
脚本内容如下文章源自小柒网-https://www.yangxingzhen.cn/7841.html
#!/bin/bash
#2020-7-10 16:24:11
#Author Blog:
# https://www.yangxingzhen.cn
# https://www.i7ti.cn
#Author WeChat:
# 微信公众号:小柒博客
#Author mirrors site:
# https://mirrors.yangxingzhen.com
#About the Author
# BY:YangXingZhen
# Mail:xingzhen.yang@yangxingzhen.com
# QQ:675583110
#Auto Get MongoDB status
function select_collections (){
stty erase '\^H'
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的IP地址:\\033\[0m")" IPADDR
if \[ -z ${IPADDR} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${IPADDR} == "q" -o ${IPADDR} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的端口号:\\033\[0m")" PORT
if \[ -z ${PORT} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${PORT} == "q" -o ${PORT} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入数据库名称:\\033\[0m")" DB_Name
if \[ -z ${DB_Name} \];then
echo -e "\\033\[31m输入错误,数据库名称不能为空...\\033\[0m"
elif \[ ${DB_Name} == "q" -o ${DB_Name} == "Q" \];then
Code=break
else
# 打印数据库名称
echo -e "\\033\[32m数据库名称:${DB_Name}\\033\[0m"
# 获取集合名
for i in $(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.getCollectionNames().join('\\n')" \|grep -vi "MongoDB")
do
echo "集合名:${i}"
done
Code=break
fi
${Code}
done
fi
${Code}
done
fi
${Code}
done
}
function select_data (){
stty erase '\^H'
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的IP地址:\\033\[0m")" IPADDR
if \[ -z ${IPADDR} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${IPADDR} == "q" -o ${IPADDR} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的端口号:\\033\[0m")" PORT
if \[ -z ${PORT} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${PORT} == "q" -o ${PORT} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入数据库名称:\\033\[0m")" DB_Name
if \[ -z ${DB_Name} \];then
echo -e "\\033\[31m输入错误,数据库名称不能为空...\\033\[0m"
elif \[ ${DB_Name} == "q" -o ${DB_Name} == "Q" \];then
Code=break
else
# 打印数据库名称
echo -e "\\033\[32m数据库名称:${DB_Name}\\033\[0m"
# 获取集合名
for i in $(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.getCollectionNames().join('\\n')" \|grep -vi "MongoDB")
do
# 输出集合名和集合的大小
echo -e "集合名:${i}\\t集合大小:$(($(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.${i}.dataSize();" \|grep -vi "MongoDB") / 1024 / 1024))M"
done
Code=break
fi
${Code}
done
fi
${Code}
done
fi
${Code}
done
}
function select_storage (){
stty erase '\^H'
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的IP地址:\\033\[0m")" IPADDR
if \[ -z ${IPADDR} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${IPADDR} == "q" -o ${IPADDR} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的端口号:\\033\[0m")" PORT
if \[ -z ${PORT} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${PORT} == "q" -o ${PORT} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入数据库名称:\\033\[0m")" DB_Name
if \[ -z ${DB_Name} \];then
echo -e "\\033\[31m输入错误,数据库名称不能为空...\\033\[0m"
elif \[ ${DB_Name} == "q" -o ${DB_Name} == "Q" \];then
Code=break
else
# 打印数据库名称
echo -e "\\033\[32m数据库名称:${DB_Name}\\033\[0m"
# 获取集合名
for i in $(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.getCollectionNames().join('\\n')" \|grep -vi "MongoDB")
do
# 输出集合名和集合已使用的空间大小
echo -e "集合名:${i}\\t集合已使用的空间大小:$(($(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.${i}.storageSize();" \|grep -vi "MongoDB") / 1024 / 1024))M"
done
Code=break
fi
${Code}
done
fi
${Code}
done
fi
${Code}
done
}
function select_index (){
stty erase '\^H'
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的IP地址:\\033\[0m")" IPADDR
if \[ -z ${IPADDR} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${IPADDR} == "q" -o ${IPADDR} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的端口号:\\033\[0m")" PORT
if \[ -z ${PORT} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${PORT} == "q" -o ${PORT} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入数据库名称:\\033\[0m")" DB_Name
if \[ -z ${DB_Name} \];then
echo -e "\\033\[31m输入错误,数据库名称不能为空...\\033\[0m"
elif \[ ${DB_Name} == "q" -o ${DB_Name} == "Q" \];then
Code=break
else
# 打印数据库名称
echo -e "\\033\[32m数据库名称:${DB_Name}\\033\[0m"
# 获取集合名
for i in $(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.getCollectionNames().join('\\n')" \|grep -vi "MongoDB")
do
# 输出集合名和集合所有的索引所占用的空间大小
echo -e "集合名:${i}\\t索引数据大小:$(($(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.${i}.totalIndexSize();" \|grep -vi "MongoDB") / 1024 / 1024))M"
done
Code=break
fi
${Code}
done
fi
${Code}
done
fi
${Code}
done
}
function select_total (){
stty erase '\^H'
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的IP地址:\\033\[0m")" IPADDR
if \[ -z ${IPADDR} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${IPADDR} == "q" -o ${IPADDR} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入需要查询MongoDB的端口号:\\033\[0m")" PORT
if \[ -z ${PORT} \];then
echo -e "\\033\[31m输入错误,IP地址不能为空...\\033\[0m"
elif \[ ${PORT} == "q" -o ${PORT} == "Q" \];then
Code=break
else
while true
do
read -p "$(echo -e "\\033\[32m请输入数据库名称:\\033\[0m")" DB_Name
if \[ -z ${DB_Name} \];then
echo -e "\\033\[31m输入错误,数据库名称不能为空...\\033\[0m"
elif \[ ${DB_Name} == "q" -o ${DB_Name} == "Q" \];then
Code=break
else
# 打印数据库名称
echo -e "\\033\[32m数据库名称:${DB_Name}\\033\[0m"
# 获取集合名
for i in $(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.getCollectionNames().join('\\n')" \|grep -vi "MongoDB")
do
# 输出集合名和集合的总占用空间,包括所有文件和所有索引
echo -e "集合名:${i}\\t集合的总占用空间大小:$(($(mongo ${IPADDR}:${PORT}/${DB_Name} --eval "db.${i}.totalSize();" \|grep -vi "MongoDB") / 1024 / 1024))M"
done
Code=break
fi
${Code}
done
fi
${Code}
done
fi
${Code}
done
}
read -p "$(echo -e "\\033\[32m请输入需要执行参数\[collections\|data\|storage\|index\|total\]:\\033\[0m")" Value
case ${Value} in
collections)
select_collections
;;
data)
select_data
;;
storage)
select_storage
;;
index)
select_index
;;
total)
select_total
;;
Q\|q)
exit 1
;;
\*)
echo "Uages:{collections\|data\|storage\|index\|total}"
;;
esac
脚本执行方式:文章源自小柒网-https://www.yangxingzhen.cn/7841.html
[root@MongoDB ~]# sh auto_get_mongodb_data.sh文章源自小柒网-https://www.yangxingzhen.cn/7841.html
脚本执行过程截图如下文章源自小柒网-https://www.yangxingzhen.cn/7841.html
文章源自小柒网-https://www.yangxingzhen.cn/7841.html 文章源自小柒网-https://www.yangxingzhen.cn/7841.html
继续阅读
历史上的今天
1 月
19
- 2018Docker构建Tomcat(Web服务器)
- 2018Docker启动Tomcat容器
- 2018Docker虚拟化技术 Shell最后更新:2022-11-25