MongoDB数据库日志备份压缩脚本

脚本1小时执行一次,对mongod,mongos,arbiter,config的日志进行logRotate,对生成的日志进行压缩。

定期删除job脚本

#bo1.li
0 1 * * * /usr/bin/find /home/cipcache/var/log/config/config*log.* -ctime +14 -exec rm {} \;
0 1 * * * /usr/bin/find /home/cipcache/var/log/arbiter/arbiter*log.* -ctime +14 -exec rm {} \;
0 1 * * * /usr/bin/find /home/cipcache/var/log/mongos/mongos.log.* -ctime +14 -exec rm {} \;
0 1 * * * /usr/bin/find /home/cipcache/var/log/mongod/shard.log.* -ctime +14 -exec rm {} \;

压缩备份脚本

#!/bin/bash
cd "$(dirname $0)";

curDate=`date ‘+%Y%m%d_%H%M%S‘`
logpath="/home/cipcache/var/log"
mongodlogpath="$logpath/mongod"
mongoslogpath="$logpath/mongos"
arbiterlogpath="$logpath/arbiter"
configlogpath="$logpath/config"

mongodlogfile="$logpath/mongod/shard.log"
mongoslogfile="$logpath/mongos/mongos.log"


C_DC_PORT=(24001 24002 24003 24004)
A_DC_PORT=(21001 21002 21003 21004)


if [ ! -d "$newmongoslogpath" ];then
mkdir -p $newmongoslogpath
fi

if [ ! -d "$newmongodlogpath" ];then
mkdir -p $newmongodlogpath
fi

#if [ ! -d "$newarbiterlogpath" ];then
# mkdir -p $newarbiterlogpath
#fi

#if [ ! -d "$newconfiglogpath" ];then
# mkdir -p $newconfiglogpath
#fi


if [ -f $mongodlogfile ];then
/home/cipcache/sbin/mongodb/mongo admin --eval "db.runCommand( { logRotate : 1 } )"
newmongodlogName=`ls -t $mongodlogpath | grep "shard.log.\/*" | awk ‘NR==1‘`
if [ -n ${newmongodlogName} ] && [ -f "$mongodlogpath/$newmongodlogName" ] ;then
#mv $mongodlogpath/$newmongodlogName $newmongodlogpath/shard.log.$curDate
#cd $newmongodlogpath;/usr/bin/gzip shard.log.$curDate
/usr/bin/gzip "$mongodlogpath/$newmongodlogName"
fi
fi

if [ -f $mongoslogfile ];then
/home/cipcache/sbin/mongodb/mongo --port 30000 admin --eval "db.runCommand( { logRotate : 1 } )"
newmongoslogName=`ls -t $mongoslogpath | grep "mongos.log.\/*" | awk ‘NR==1‘`
if [ -n ${newmongoslogName} ] && [ -f "$mongoslogpath/$newmongoslogName" ] ;then
#mv $mongodlogpath/$newmongodlogName $newmongodlogpath/shard.log.$curDate
#cd $newmongodlogpath;/usr/bin/gzip shard.log.$curDate
/usr/bin/gzip "$mongoslogpath/$newmongoslogName"
fi
fi

for value in ${C_DC_PORT[*]}
do
/home/cipcache/sbin/mongodb/mongo --port $value admin --eval "db.runCommand( { logRotate : 1 } )"
newconfiglogName=`ls -t $configlogpath/config*log.* | awk ‘NR==1‘`
#mv $newconfiglogName $newconfiglogpath/config$value.log.$curDate
if [ -n ${newconfiglogName} ] ;then
/usr/bin/gzip "$newconfiglogName"
fi
done

for value in ${A_DC_PORT[*]}
do
/home/cipcache/sbin/mongodb/mongo --port $value admin --eval "db.runCommand( { logRotate : 1 } )"
newarbiterlogName=`ls -t $arbiterlogpath/arbiter*log.* | awk ‘NR==1‘`
#mv $newarbiterlogName $newarbiterlogpath/arbiter$value.log.$curDate
if [ -n ${newarbiterlogName} ];then
/usr/bin/gzip "$newarbiterlogName"
fi
done

exit 0

MongoDB数据库日志备份压缩脚本

上一篇:Delphi流的操作


下一篇:mongodb数据库磁盘碎片整理。