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


主要是通过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 定时调用上述两个脚本




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值