Shell脚本获取MongoDB数据量大小

小辉博客
小辉博客
小辉博客
394
文章
3
评论
2021-01-2013:47:42
评论
22.8K 5296字阅读17分39秒

此脚本是获取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.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

脚本执行方式:

[root@MongoDB ~]# sh auto_get_mongodb_data.sh

脚本执行过程截图如下

Shell脚本获取MongoDB数据量大小

继续阅读
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如果喜欢本站,请打赏支持本站,谢谢!
  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
小辉博客
  • 本文由 发表于 2021-01-2013:47:42
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Centos7.X防SSH暴力破解脚本 Shell

Centos7.X防SSH暴力破解脚本

什么是暴力破解,简单来说就是对一个服务器进行无数次尝试登陆,并用不同的密码进行登陆直到可以登陆成功。暴力破解的基本步骤可以分为以下几步: 1)找到对应的Linux服务器ip地址 2)扫描端口号:2...
CentOS系统初始化脚本(适合CentOS 6.X和CentOS 7.X系列) Shell

CentOS系统初始化脚本(适合CentOS 6.X和CentOS 7.X系列)

作为运维人员,经常会初始化系统,系统在安装过程中基本都会选择最小化安装,这样安装好的系统里会缺少很多环境。系统初始化脚本是刚安装完操作系统之后运行的脚本,主要功能是将系统环境进行优化,并更改常用设置,...
匿名

发表评论

匿名网友 填写信息

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