您的位置:首页 > 数据库 > Mongodb

mongodb全量备份,简单的实现方式

2016-06-30 16:38 691 查看
主要是通过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 定时调用上述两个脚本
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb