点击(此处)折叠或打开
- #!/bin/bash
- set -e
- BACKUP_DIR="/goluk/backup/mongo"
- MONGO_HOST="db03"
- MONGO_DB="trip"
- MONGO_DUMP_CMD="mongodump "
- NUM=0
- MONGO_EXCLUDE="trip_track"
- BACK_FILE="$BACKUP_DIR/$MONGO_DB-$(date +%Y%m%d-%H%M).gz"
- CMD="$BACKUP_DIR/$MONGO_DB-$(date +%Y%m%d-%H%M).CMD"
- ALERTING="[email protected]"
- error_trap() {
- echo " [LINE:$1] Error." |mail -s "mongodb backup failed" $ALERTING
- rm -fr $BACK_FILE $CMD
- }
- get_max_track_table() {
- id2=`mongo <<EOF
- use trip
- rs.slaveOk()
- db.trip_track_table.find({},{"id" : 1 , "_id" : 0}).sort({"id" : -1}).limit(1)
- EOF`
- echo $id2
- }
- set_exclude() {
- NUM=`echo $id2 |awk -F':| ' '{print $18}'`
- NUM2=`expr $NUM - 1 `
- MONGO_EXCLUDE="--excludeCollection=trip_track --excludeCollection=trip_track_{0..$NUM2}"
- echo $MONGO_EXCLUDE
- }
- main() {
- trap 'error_trap $LINENO' ERR
- get_max_track_table
- set_exclude
- echo "$MONGO_DUMP_CMD -h $MONGO_HOST:27017 -d $MONGO_DB $MONGO_EXCLUDE --gzip --archive=$BACK_FILE" >$CMD
- sh < $CMD
- rm -f $CMD
- }
- main