0
点赞
收藏
分享

微信扫一扫

mongodb定时备份与清理

沪钢木子 2021-09-28 阅读 54

备份脚本

#!/bin/bash
sourcepath='/usr/local/mongodb'/bin
targetpath='/data/mongodb_bk'
nowtime=$(date +%Y%m%d)
 
start()
{
  ${sourcepath}/mongodump --host 127.0.0.1 --port 27017 --out ${targetpath}/${nowtime}
}
execute()
{
  start
  if [ $? -eq 0 ]
  then
    tar -czvf ${targetpath}/${nowtime}.tar.gz  -C ${targetpath} ${nowtime} --remove-files
    echo "back successfully!"

  else
    echo "back failure!"
  fi
}
 
if [ ! -d "${targetpath}/${nowtime}/" ]
then
 mkdir ${targetpath}/${nowtime}
fi
execute
echo "============== back end ${nowtime} =============="

清理脚本

#!/bin/bash
targetpath='/data/mongodb_bk'
nowtime=$(date -d '-3 days' "+%Y%m%d")
if [ -f "${targetpath}/${nowtime}.tar.gz" ]
then
  rm -rf "${targetpath}/${nowtime}.tar.gz"
  echo "=======${targetpath}/${nowtime}.tar.gz===删除完毕=="
fi
echo "===$nowtime ==="

定时任务

10 02 * * * /bin/bash /data/shell/cron_mongo_back.sh 1>>/data/shell/log/cron_mongo_back.log
10 03 * * * /bin/bash /data/shell/cron_mongo_rm.sh 1>>/data/shell/log/cron_mongo_rm.log
举报

相关推荐

0 条评论