Mongodb备份

#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
. /etc/profile 

# webhook
db_type="MongoDB"
db_env="宜昌城发正式环境"

webhook_url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=****"


#备份相关
host=$(hostname)
BIN='/usr/bin/mongodump'
BASE_DIR="/data/backup/${db_type}"
BACK_DIR=${BASE_DIR}/$(date +%Y%m%d)
keep_time="7"
start_time=$(date "+%F %X")
yester_day=$(date -d"1 day ago" +"%Y%m%d")

# MongoDB设置
HOST='10.96.2.199'
USER='root'
PASSWD='starship@2022'

#备份所有库
#${BIN} -u "$USER" -host ${HOST} -p"$PASSWD" --port 27017 -o ${BACK_DIR}
#/root/whyw/mongodump -u "$USER" -host ${HOST} -p"$PASSWD" --port 27017 -o ${BACK_DIR}
/root/whyw/mongodump    -h 10.96.2.199 --port 27017  -u root  -pstarship2022 -o ${BACK_DIR}
#备份单个库
#$BIN -u"$USER" --host $HOST -p"$PASSWD" -d common-tool -o ${BACK_DIR}

# webhook 相关
bdir_size=$(du -sm ${BACK_DIR} 2>/dev/null |awk '{print $1}')
last_bdir_size=$(du -sm ${BASE_DIR}/${yester_day} 2>/dev/null |awk '{print $1}')

end_time=$(date "+%F %X")

if [ ! ${last_bdir_size} ]; then
  last_bdir_size=0
fi

size_diff=$(expr ${bdir_size} - ${last_bdir_size})

if [ ${size_diff} -lt 0 ];then
  bak_status_col='warning'
  bk_status="Failed"
else
  bak_status_col='info'
  bk_status="Succes"
fi
contents='{
  "msgtype": "markdown",
  "markdown": {
      "content": "
          # '${db_env}' '${db_type}' 备份监控 \\n
          >备份状态: <font color=\"'${bak_status_col}'\">'${bk_status}'</font> \\n
          >备份主机: <font color=\"comment\">'${HOST}'</font> \\n
          >备份开始时间: <font color=\"comment\">'${start_time}'</font>\\n
          >备份结束时间: <font color=\"comment\">'${end_time}'</font>\\n
          >备份目录: <font color=\"comment\">'${BACK_DIR}'</font> \\n
          >备份目录大小: <font color=\"comment\">'${bdir_size}'M</font> \\n
          >上次备份目录: <font color=\"comment\">'${BASE_DIR}/${yester_day}'</font> \\n
          >上次备份目录大小: <font color=\"comment\">'${last_bdir_size}'M</font> \\n
          >与上次备份差: <font color=\"comment\">'${size_diff}'M</font> \\n"
      }
}'

echo -e ${contents} > ${BACK_DIR}/webhook.json

curl -X POST -i -H 'Content-Type: application/json' -d @${BACK_DIR}/webhook.json ${webhook_url}

#删除15天前的文件目录
find ${BACK_DIR} -type d -mtime  +${keep_time} |xargs rm -rf {};

posted @ 2023-01-28 09:33  小星奕的快乐  阅读(5)  评论(0)    收藏  举报