#!/usr/bin/bashsql
#define:Mongodb data backup regularly, delete data before the specified timemongodb
#date:2016-12-20shell
curr_dir=`pwd`
back_dir="/data/bak/mongodb_bak"
mongo_comm="/usr/local/mongodb/bin/mongo"
mongo_dump="/usr/local/mongodb/bin/mongodump"
mongo_data="
robot
engine
edi"bash
####rsync同步信息
rsync_user=***
rsync_passwd=****
echo "$rsync_passwd" > $curr_dir/rsync_passwd
passwd="$curr_dir/rsync_passwd"
rsync_server=****
rsync_module=mongo_com大數據
#####mongodb各庫須要保存的天數
#robot_delete_day=`date -d "25 days ago" +"%Y-%m-%d %H:%M:%S"`
robot_delete_day=`date -d "24 days ago" +"%Y-%m-%d"`
#engine_delete_day=`date -d "60 days ago" +"%Y-%m-%d %H:%M:%S"`
engine_delete_day=`date -d "13 days ago" +"%Y-%m-%d"`
#edi_delete_day=`date -d "16 days ago" +"%Y-%m-%d %H:%M:%S"`
edi_delete_day=`date -d "52 days ago" +"%Y-%m-%d"`
today=`date +"%Y%m%d"`
transferr_data_dir="/data/bak/mongo_data_back/$today"日誌
id_dir="/data/bak/mongo_id/$today"
[[ -d $id_dir ]]|| mkdir -p $id_dirserver
logdir="/data/log/mongodb_delete_log" #日誌路徑
log=$logdir/log"$today".log #日誌文件
is_font=1 #終端是否打印日誌: 1打印 0不打印
is_log=1 #是否記錄日誌: 1記錄 0不記錄
[[ -d $logdir ]]||mkdir -p $logdir #確保日誌目錄存在ci
datef(){
date "+%Y-%m-%d %H:%M:%S"
}rem
print_log(){
if [[ $is_log -eq 1 ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo -e "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1 ]];then
echo -e "[ $(datef) ] $1"
fi
}get
####mongodb數據按期備份、壓縮、同步至大數據存儲
mongodb_back_regualy() {
for data in $mongo_data
do
print_log "判斷備份目錄是否存在"
[[ -d $back_dir/$today/$data ]]|| mkdir -p $back_dir/$today/$data
while true
do
print_log "$data 開始備份"
$mongo_dump -d $data -o $back_dir/$today/$data
if [[ $? -eq 0 ]];then
print_log "$data 備份成功"
break
else
print_log "$data 備份失敗,繼續"
fi
done
cd $back_dir/$today/$data
while true
do
print_log "$data 開始壓縮"
[[ -d $transferr_data_dir ]]|| mkdir -p $transferr_data_dir
tar -zcf $transferr_data_dir/"$data"_"$today".tar.gz "$data"
if [[ $? -eq 0 ]];then
print_log "'$data'_'$today' 壓縮成功"
break
else
print_log "'$data'_'$today' 壓縮失敗,繼續壓縮"
fi
done
while true
do
print_log "'$data'_'$today'.tar.gz 開始傳輸"
#####rsync傳輸,傳輸目錄變量待修改
#rsync -avzr --password-file=$passwd $transferr_data_dir $rsync_user@$rsync_server::$rsync_module
if [[ $? -eq 0 ]];then
print_log "'$data'_'$today'.tar.gz 傳輸成功"
break
else
print_log "'$data'_'$today'.tar.gz 傳輸失敗"
fi
done
done
}
mongo_robot_delete() {
print_log "開始統計robot庫總條數"
mongo_sql_sum='db.robotTask.find().count()'
mongo_sql_sum_cal=`echo "$mongo_sql_sum"|$mongo_comm $data --shell|grep ^[0-9]`
print_log "robot庫總條數爲------$mongo_sql_sum_cal-------"
#print_log "開始統計robot庫要刪除的條數"
while true
do
mongo_sql_count='db.robotTask.find({"startTime":{$lt:ISODate("'$robot_delete_day'")}}).count()'
mongo_sql_count_cal=`echo "$mongo_sql_count"|$mongo_comm $data --shell`
need_delete_total=`echo "$mongo_sql_count_cal"|grep ^[0-9]`
if [[ $need_delete_total -le 0 ]];then
print_log "robot 刪除完畢"
break
else
print_log "robot庫須要刪除的條數爲 $need_delete_total --------------------------"
mongo_data_delete='db.robotTask.remove({"startTime":{$lt:ISODate("'$robot_delete_day'")}})'
echo "$mongo_data_delete" | $mongo_comm $data --shell
fi
done
}
mongo_engine_delete() {
print_log "開始統計engine庫總條數"
mongo_sql_sum='db.rulemessage.find().count()'
mongo_sql_sum_cal=`echo "$mongo_sql_sum"|$mongo_comm $data --shell|grep ^[0-9]`
print_log "engine庫總條數爲--------$mongo_sql_sum_cal---------"
#print_log "開始統計engine庫要刪除的條數"
while true
do
mongo_sql_count='db.getCollection("rulemessage").find({"lastUpdated":{$lte:new Date("'$engine_delete_day'").getTime()}}).count()'
mongo_sql_count_cal=`echo "$mongo_sql_count"|$mongo_comm $data --shell`
need_delete_total=`echo "$mongo_sql_count_cal"|grep ^[0-9]`
if [[ $need_delete_total -le 0 ]];then
print_log "engine 刪除完畢"
break
else
print_log "engine庫須要刪除的條數爲 $need_delete_total --------------------------"
mongo_data_delete='db.rulemessage.remove({"lastUpdated":{$lte:new Date("'$engine_delete_day'").getTime()}})'
echo "$mongo_data_delete" | $mongo_comm $data --shell
fi
done
}
mongo_edi_delete() {
print_log "開始統計edi庫總條數"
mongo_sql_sum='db.action_log.find().count()'
mongo_sql_sum_cal=`echo "$mongo_sql_sum"|$mongo_comm $data --shell |grep ^[0-9]`
print_log "edi庫總條數爲-------$mongo_sql_sum_cal---------"
#print_log "開始統計edi庫要刪除的條數"
while true
do
mongo_sql_count='db.action_log.find({"sendTime":{$lt:ISODate("'$edi_delete_day'")}}).count()'
mongo_sql_count_cal=`echo "$mongo_sql_count"|$mongo_comm $data --shell`
need_delete_total=`echo "$mongo_sql_count_cal"|grep ^[0-9]`
if [[ $need_delete_total -le 0 ]];then
print_log "edi 刪除完畢"
break
else
print_log "edi庫須要刪除的條數爲 $need_delete_total --------------------------"
mongo_data_delete='db.action_log.remove({"sendTime":{$lt:ISODate("'$edi_delete_day'")}})'
echo "$mongo_data_delete" | $mongo_comm $data --shell
fi
done
}
data_delete(){
cd $id_dir
print_log "刪除各庫10天之前的id數據"
find ./ -mtime +10 |xargs rm -rf
print_log "刪除各庫備份兩天之前的數據"
cd $back_dir
find ./ -mtime +1 |xargs rm -rf
print_log "刪除各庫壓縮4天之前的數據"
cd $transferr_data_dir
find ./ -mtime +3 |xargs rm -rf
}
mongodb_back_regualy
for data in $mongo_data
do
case $data in
robot)
mongo_robot_delete
;;
engine)
mongo_engine_delete
;;
edi)
mongo_edi_delete
;;
*)
echo "$1"
echo "Usage:集合不存在"
;;
esac
done
data_delete