美文网首页javaWeb学习
mongodb定时备份与清理

mongodb定时备份与清理

作者: weylau | 来源:发表于2019-07-20 16:47 被阅读1次

备份脚本

#!/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

相关文章

网友评论

    本文标题:mongodb定时备份与清理

    本文链接:https://www.haomeiwen.com/subject/xieulctx.html