主要是通过shell脚本调用mongodb命令实现,编写sh的脚本,然后结合linux crontab 定时调用
脚本分为备份脚本和清理脚本
3、如下是关于自动备份的脚本
#!/bin/bash
sourcepath='/home/mongodb/mongodb-linux-x86_64-3.0.5'/bin
targetpath='/home/mongo_bak'
nowtime=$(date "+%Y%m%d%H:%M:%S" | cut -d ":" -f 1)
dbname='glgd'
logdatetime=$(date "+%Y%m%d%H:%M:%S")
start()
{
echo "[${logdatetime}]===========bak start=================" >> mongodb_bak_clear.log
${sourcepath}/mongodump --host 127.0.0.1 --port 27017 -d ${dbname} --out ${targetpath}/${nowtime}
}
execute()
{
start
if [ $? -eq 0 ]
then
echo "bak successfully!" >> mongodb_bak_clear.log
else
echo "bak failure!" >> mongodb_bak_clear.log
fi
}
if [ ! -d "${targetpath}/${nowtime}/" ]
then
mkdir ${targetpath}/${nowtime}
fi
execute
echo "[${logdatetime}]===========bak end --> bak to the path: ${targetpath}/${nowtime} ==============" >> mongodb_bak_clear.log
2、清理脚本
#!/bin/bash
targetpath='/home/mongo_bak'
dbname='glgd'
nowtime=$(date +%Y%m%d%H --date="-5 hour")
logdatetime=$(date "+%Y%m%d %H:%M:%S")
echo "[${logdatetime}]===clear start===" >> mongodb_bak_clear.log
if [ -d "${targetpath}/${nowtime}/" ]
then
rm -rf "${targetpath}/${nowtime}/"
echo "===success:delete ${targetpath}/${nowtime}/ completed!=====" >> mongodb_bak_clear.log
else
echo "===fail:${targetpath}/${nowtime}/ does not exists!=====" >> mongodb_bak_clear.log
fi
echo "[${logdatetime}]===clear end ! ===" >> mongodb_bak_clear.log
3、编写linux crontab 定时调用上述两个脚本