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

来源:互联网 发布:深圳淘宝培训班免费 编辑:程序博客网 时间:2024/05/20 11:37


主要是通过shell脚本调用mongodb命令实现,编写sh的脚本,然后结合linux crontab 定时调用

脚本分为备份脚本和清理脚本


3、如下是关于自动备份的脚本

#!/bin/bashsourcepath='/home/mongodb/mongodb-linux-x86_64-3.0.5'/bintargetpath='/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}fiexecuteecho "[${logdatetime}]===========bak end --> bak to the path: ${targetpath}/${nowtime} ==============" >> mongodb_bak_clear.log


2、清理脚本

#!/bin/bashtargetpath='/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.logif [ -d "${targetpath}/${nowtime}/" ]then  rm -rf "${targetpath}/${nowtime}/"  echo "===success:delete ${targetpath}/${nowtime}/  completed!====="  >> mongodb_bak_clear.logelse  echo "===fail:${targetpath}/${nowtime}/ does not exists!====="  >> mongodb_bak_clear.logfiecho "[${logdatetime}]===clear end ! ===" >> mongodb_bak_clear.log

3、编写linux crontab 定时调用上述两个脚本




0 0
原创粉丝点击