Shell脚本获取MongoDB数据量大小

小柒博客 264,4404字数 1447阅读4分49秒阅读模式

此脚本是获取MongoDB数据量大小脚本,有需要朋友可以参考,脚本内容如下:

一、环境准备

操作系统:CentOS Linux release 7.8.2003

二、软件版本

MongoDB server version: 3.4.24

[root@MongoDB ~]# vim auto_get_mongodb_data.sh

# 脚本内容如下

#!/bin/bash
#2020-7-10 16:24:11
#Author Blog:
#	https://www.yangxingzhen.com
#	https://www.yangxingzhen.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

脚本执行方式:

[root@MongoDB ~]# sh auto_get_mongodb_data.sh

脚本执行过程截图如下

Shell脚本获取MongoDB数据量大小

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
微信号已复制
微信公众号
微信扫一扫,关注我!
weinxin
公众号已复制
Shell最后更新:2024-1-24
小柒博客
  • 本文由 小柒博客 发表于 2021年1月19日 14:13:40
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.com/7841.html
评论  2  访客  2
    • 妖怪
      妖怪 4

      如果最后再加一个汇总所有集合的总大小就更美了

    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    拖动滑块以完成验证