#!/bin/bash
BAK_DIR=/data/backup/mongo_bak
BAK_TIME=$(date +%F)
mongodump=/usr/local/mongodb/bin/mongodump
host=127.0.0.1
prot=27017
#backup mongodb
if [ ! -e $BAK_DIR/$BAK_TIME ]
then
mkdir $BAK_DIR/$BAK_TIME
fi
$mongodump -h $host -p $prot -o $BAK_DIR/$BAK_TIME
cd $BAK_DIR
tar -zcvf $BAK_TIME.tar.gz $BAK_DIR/$BAK_TIME
rm -rf $BAK_TIME
if [ -e $BAK_DIR$BAK_TIME.tar.gz ] && [ $? -eq 0 ]
then
echo $BAK_TIME mongodb $DB backup is successlly >> $BAK_DIR/mongo_bak.log
else
echo $BAK_TIME mongodb backup is fail >> $BAK_DIR/mongo_bak.log
fi
#保存30天的数据
find $BAK_DIR -mtime +30 -exec rm -rf {} \;