批量修改mongodb字段.md

脚本说明

  • 该脚本用于批量修改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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平凡的运维之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值