#!/bin/bash max_day="2021-01-01" year= while getopts ":y:m:" opt; do case $opt in y) year=$OPTARG ;; m) month=$OPTARG ;; ?) echo "usage: dumpdata -y=2019 -m=2" exit 1 ;; esac done if [ ! -n "$year" ]; then echo "no year paramerter" exit 1 fi if [ ! -n "$month" ]; then echo "no month parameter" exit 1 fi start_month=$(printf "%02d" ${month}) start_year=${year} next_month=$(expr ${month} + 1) if [ $next_month -gt 12 ]; then end_month="01" end_year=$(expr ${year} + 1) else end_month=$(printf "%02d" ${next_month}) end_year=${year} fi start_day="${start_year}-${start_month}-01" start_time="${start_year}-${start_month}-01 00:00:00" start_ts=$(date -u -d ${start_day} +%s)000 echo $start_ts echo $start_time end_day="${end_year}-${end_month}-01" end_ts=$(date -u -d ${end_day} +%s)000 end_time="${end_year}-${end_month}-01 00:00:00" echo $end_time echo $end_ts if [ "$end_day" \> "$max_day" ]; then echo "invalid day." exit 1 fi make_js() { _db_name=$1 _col_name=$2 _start_day=$3 _end_day=$4 _output_file=$5 echo "use ${_db_name};" >${_output_file} echo 'var bulk = db.getCollection("'${_col_name}'").initializeUnorderedBulkOp();' >>${_output_file} echo 'bulk.find({"$and": [{"date": {"$gte": "'${_start_day}'"}}, {"date": {"$lt": "'${_end_day}'"}}]}).remove()' >>${_output_file} echo "bulk.execute();" >>${_output_file} cat ${_output_file} } db_name="report_his" echo "====================== device_daily_stats begin =============================" make_js ${db_name} "device_daily_stats" ${start_day} ${end_day} temp.js #mongo --authenticationMechanism=SCRAM-SHA-1 --authenticationDatabase=admin --host 172.16.145.62 --port 20000 -u guanli -p 67374030857714990597992655453345