mongodb清理大量历史缓存数据.md

2022-09-06  本文已影响0人  平凡的运维之路

清理缓存

image.png

清理脚本

#!/bin/bash
MongoHostAndPort="xxxx.xxx.xxx:27017"
MongoBinDir="/home/mongodb/mongodb-3.2.12/bin/"
call_tables_1="call_tables_1"
tables_3="bx_table"
call_detail_back="call_detail_back"
tables_1="record_file"
January=$(date -d "`date  -d "1 month ago" +"%Y-%m-%d"` 00:00:00"  +%s) 
March=$(date -d "`date  -d "3 month ago" +"%Y-%m-%d"` 00:00:00"  +%s)
#是否mongodb认证登录
mongodbauth=no
#mongodb用户
mongodbuser=xxx
#mongodb密码
mongodbpasswd=xxxxx
#mognodb认证数据库
authdb=admin

RemoveR_agsData(){
    cd $MongoBinDir
    if [ "$mongodbauth" == "yes" ];then
        echo "Delete entid $1 $tables_3 data"
        echo -e "db.$tables_3.deleteMany({'start_time':{'\$lt':'$March'}}) " | ./mongo  $MongoHostAndPort/$1 -u$mongodbuser  -p$mongodbpasswd --authenticationDatabase $authdb
        echo "Delete entid $1 $call_tables_1 all data"
        echo -e "db.$call_tables_1.deleteMany({})"|./mongo  $MongoHostAndPort/$1 -u$mongodbuser  -p$mongodbpasswd --authenticationDatabase $authdb  || echo "Remove $Tables_name Fail "
        echo "Delete entid $1 $tables_1 all data"
        echo -e "db.$tables_1.deleteMany({})"|./mongo  $MongoHostAndPort/$1 -u$mongodbuser  -p$mongodbpasswd --authenticationDatabase $authdb   || echo "Remove $Tables_name Fail "
    else
        echo "Delete entid $1 $tables_3 data"
        echo -e "db.$tables_3.deleteMany({'start_time':{'\$lt':'$March'}}) " | ./mongo  $MongoHostAndPort/$1
        echo "Delete entid $1 $call_tables_1 all data"
        echo -e "db.$call_tables_1.deleteMany({})"|./mongo  $MongoHostAndPort/$1   || echo "Remove $Tables_name Fail "
        echo "Delete entid $1 $tables_1 all data"
        echo -e "db.$tables_1.deleteMany({})"|./mongo  $MongoHostAndPort/$1   || echo "Remove $Tables_name Fail "
    fi
}

RemoveCall_detailData(){
    cd $MongoBinDir
    if [ "$mongodbauth" == "yes" ];then
        echo -e "db.$call_detail_back.deleteMany({'start_time':{'\$lt':'$January'}}) " | ./mongo  $MongoHostAndPort/$1  -u$mongodbuser  -p$mongodbpasswd --authenticationDatabase $authdb
        echo "Delete entid $1 $call_detail_back data"
    else
        echo -e "db.$call_detail_back.deleteMany({'start_time':{'\$lt':'$January'}}) " | ./mongo  $MongoHostAndPort/$1
        echo "Delete entid $1 $call_detail_back data"
    fi
}

Main() {
    printf "Main function" 
    cd $MongoBinDir
    if [ $? == 0 ];then
       #for Entid in `echo -e "show dbs"|./mongo  $MongoHostAndPort|awk '{print $1}'|egrep '^[0-9]' `
       for Entid in `echo -e "show dbs"|./mongo  $MongoHostAndPort|awk '{print $1}'`
        do
           RemoveR_agsData  $Entid
           sleep 1
           RemoveCall_detailData $Entid
        done
    else
        usleep
    fi
}

Main
上一篇下一篇

猜你喜欢

热点阅读