脚本说明
-
该脚本用于批量修改mongodb字段,根据时间范围循环执行.
-
增加工作时间范围判断和mongodb 认证判断
-
增加日志输出
-
脚本代码
#!/bin/bash
MongoHostAndPort="127.0.0.1:27017"
# MongoBin="/home/mongodb/mongodb_3.2.12/bin/mongo"
MongoBin="/root/mongodb/mongo"
logfile="./log/remove_detail.log"
start_date="2022-01-01"
end_date="2022-12-31"
mongodb_user=ccod
mongodb_pwd=TKBXqnsoft2019
mopngodb_auth=true
#日志输出
logmod(){
echo "`date +%Y-%m-%d" "%H:%M:%S` "$*"" >> $logfile
}
#检查脚本执行工作时间
function check_for_hour() {
function check_hour() {
current_hour=$(date +%H)
if [ $current_hour -ge 8 ] && [ $current_hour -le 22 ]; then
logmod INFO $current_hour not In job Time 08 hour ==== 22 hour sleep 3600s
sleep 3600
else
logmod INFO $current_hour In job Time 08 hour ==== 22 hour Break For
break
fi
}
while true; do
check_hour
done
}
#自动获取需要执行企业id
function get_ent_id_list(){
if [ "$mopngodb_auth" == true ];then
ent_id_list=$(echo -e "show dbs"|$MongoBin $MongoHostAndPort|awk '{print $1}'|egrep '^[0-9]')
else
ent_id_list=$(echo -e "show dbs"|$MongoBin $MongoHostAndPort --authenticationDatabase admin -u$mongodb_user -p$mongodb_pwd |awk '{print $1}'|egrep '^[0-9]' )
fi
echo $ent_id_list
}
#删除执行函数
for Entid in $(get_ent_id_list)
do
# 计算开始和结束时间戳
start_timestamp=$(date -d "$start_date" +%s)
end_timestamp=$(date -d "$end_date" +%s)
# 输出开始和结束日期
logmod INFO "remove test1集合中 子集 开始日期: $(date -d @$start_timestamp +%Y-%m-%d)" "结束日期: $(date -d @$end_timestamp +%Y-%m-%d)"
# 循环输出每一天的日期
current_timestamp=$start_timestamp
while [ $current_timestamp -le $end_timestamp ]; do
start_time_timestamp=$((current_timestamp))
current_timestamp=$((current_timestamp + 86400)) # 每天加86400秒
end_time_timestamp=$current_timestamp
new_start_time_timestamp=$start_time_timestamp"000"
new_end_time_timestamp=$end_time_timestamp"000"
# echo $new_start_time_timestamp,$new_end_time_timestamp
logmod INFO Run mongodb Command $Entid -e "db.test1.updateMany({\"start_time\" : {\$gte : \"$new_start_time_timestamp\",\$lte : \"$new_end_time_timestamp\"}},{\$set : {session_detail : null}})"
if [ "$mopngodb_auth" == true ];then
echo -e "db.test1.updateMany({\"start_time\" : {\$gte : \"$new_start_time_timestamp\",\$lte : \"$new_end_time_timestamp\"}},{\$set : {session_detail : null}})" |$MongoBin $MongoHostAndPort/$Entid --authenticationDatabase admin -u$mongodb_user -p$mongodb_pwd
else
echo -e "db.test1.updateMany({\"start_time\" : {\$gte : \"$new_start_time_timestamp\",\$lte : \"$new_end_time_timestamp\"}},{\$set : {session_detail : null}})" |$MongoBin $MongoHostAndPort/$Entid
fi
check_for_hour
sleep 1
done
done