#!/bin/sh

# This script is run on every mongo node. However, it checks to see if this node is the primary mongo node.
# If it is, mongo db is backed up and deleted the user session collection files.
# If this node is not primary mong node, nothing is done. MONGO_BACKUP_FOLDER=/backup/mongo-prod HOST_IP=`/sbin/ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p' | head -n1`
echo "HOST_IP: " $HOST_IP HOST_NAME=`hostname`
echo "HOST_NAME: " $HOST_NAME IS_PRIMARY=`mongo --quiet $HOST_IP --eval "print(db.isMaster().ismaster);"`
echo "IS_PRIMARY: " $IS_PRIMARY PRIMARY_HOST=`mongo --quiet $HOST_IP --eval " var primary = db.isMaster().primary;
if(primary && primary.indexOf(':') > ) primary = primary.substring(, primary.indexOf(':'));
print(primary);"`
if [ $PRIMARY_HOST == "undefined" ]
then
PRIMARY_HOST=`hostname`
fi
echo "PRIMARY_HOST: " $PRIMARY_HOST if [ "${IS_PRIMARY}" == "false" ]
then
echo "$PRIMARY_HOST is primary mongo node. Backing up data...."
BACKUP_FOLDER=`date +'%Y-%m-%d_%H%M%S'`
BACKUP_DIR=$MONGO_BACKUP_FOLDER/$BACKUP_FOLDER/$PRIMARY_HOST mkdir -p $BACKUP_DIR
cd $BACKUP_DIR
echo "PWD:" `pwd` EXPORT_RESULT=`mongodump -h $HOST_IP`
echo "Backed up data in $BACKUP_DIR" #delete user session collection
if [ -d ${BACKUP_DIR}/dump/sxa ]
then
if [ -f ${BACKUP_DIR}/dump/sxa/userSessionCollection.bson ]
then
rm -f ${BACKUP_DIR}/dump/sxa/userSessionCollection.bson
echo "Deleted user session data bson"
fi
if [ -f ${BACKUP_DIR}/dump/sxa/userSessionCollection.metadata.json ]
then
rm -f ${BACKUP_DIR}/dump/sxa/userSessionCollection.metadata.json
echo "Deleted user session metadata json"
fi
fi KEEP_DAYS=
#Delete back-up folder olders than KEEP_DAYS
echo "Removing backup data older than $KEEP_DAYS days....."
find $MONGO_BACKUP_FOLDER -mtime +$KEEP_DAYS -exec rm {} \;
echo "Done removing backup data older than $KEEP_DAYS days"
else
echo "$HOST_NAME is not primary mongo server ($PRIMARY_HOST is primary). Back-ups are run only on the primary."
fi echo "Complete!!!"

Mongo Backup的更多相关文章

  1. docker mongo backup 不用找啦,就在这里。

    rm -rf /tmp/mongodump && mkdir /tmp/mongodumpdocker run -it --rm --link lps-mongodb:mongo -v ...

  2. MongoDB Connector for Hadoop

    MongoDB Connector for Hadoop https://github.com/mongodb/mongo-hadoop Purpose The MongoDB Connector f ...

  3. Docker搭建MongoDB

    1. Docker搭建Mongodb 1.1 获取docker镜像 docker pull mongo 1.2 创建mongodb容器 docker run --name my-mongo -p 27 ...

  4. MongoDB With Spark遇到的2个错误,不能初始化和sample重复的key

    1.$sample stage could not find a non-duplicate document while using a random cursor 这个问题比较难解决,因为我用mo ...

  5. Mongodb基于oplog恢复至任意时间

    背景: 最近后端基于mongo的项目越来越多,MySQL基于冷备份+binlog可以恢复至任意时间点,那么mongo是否有同样的功能呢?经过调研发现可以通过dump+oplog可以实现粒度更细致的恢复 ...

  6. Istio VirtualService 虚拟服务

    概念及示例 VirtualService 描述了一个或多个用户可寻址目标到网格内实际工作负载之间的映射 . 虚拟服务让您配置如何在服务网格内将请求路由到服务,这基于 Istio 和平台提供的基本的连通 ...

  7. centos7 在docker下安装mongodb

    第一步:安装 1.1 查找(查看)mongo相应的版本 [root@localhost ~]# docker search mongo INDEX NAME DESCRIPTION STARS OFF ...

  8. Adnc如何本地调试 - 一个轻量级的.Net Core微服务开发框架

    前言     Adnc是一个轻量级的.Net Core微服务开发框架,同样适用于单体架构系统的开发.     如果只是想本地调试,只需要安装必备软件,必备软件除开发工具外,其它软件建议大家都使用`do ...

  9. mongo创建用户

    use dwb db.createUser( { "user" : "username",                         "pwd& ...

随机推荐

  1. 1、6 登陆 通过name 查到一个对象,如果没有。提示用户名不存在,再将查到的pwd 加传过来的pwd(需要加密)比较

    @RequestMapping("/login") public Object login(Student student) { Student s=studentService. ...

  2. RocketMQ之六:RocketMQ消息存储

    一.RocketMQ的消息存储基本介绍 先看一张图: 1.Commit log存储消息实体.顺序写,随机读.2.Message queue存储消息的偏移量.读消息先读message queue,根据偏 ...

  3. 正则表达式(Regular Expression, RegEx)学习入门

    1. 概述 正则表达式(Regular Expression, RegEx)是一种匹配模式,描述的是一串文本的特征. 正如自然语言中高大.坚固等词语抽象出来描述事物特征一样,正则表达式就是字符的高度抽 ...

  4. 1267: 展开字符串(Java)

    WUSTOJ 1267: 展开字符串 参考 jamesMusk的博客--Java 判断字符是大写小写或者数字 Description   给三个参数a1,a2,a3和一个字符串,请按以下要求展开该字符 ...

  5. pandas之分组计算笔记

    分组计算三部曲:拆分-->应用-->合并 分组:就是按照行或列把相同索引的部分分到一起 分组的关键词为groupby,分组后我们就可以对每组数据进行同一操作,返回的是每组数据分别计算后的结 ...

  6. spring cloud 停止服务

    shutdown的默认url为host:port/shutdown,当需要停止服务时,向服务器post该请求即可,如:curl -X POST host:port/shutdown将得到形如{&quo ...

  7. 在一台服务器上启动多个Broker

    1:把整个conf文件夹复制一份,比如叫做conf22:修改里面的activemq.xml文件(1)里面的brokerName 不能跟原来的重复(2)数据存放的文件名称不能重复,比如:<kaha ...

  8. Unity性能优化-对象池

    1.对象池Object Pool的原理: 有些GameObject是在游戏中需要频繁生成并销毁的(比如射击游戏中的子弹),以前的常规做法是:Instantiate不断生成预设件Prefab,然后采用碰 ...

  9. (一)CXF之发布第一个WebService服务

    一.CXF入门 1.1 前提 Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS .这些 ...

  10. “org/apache/commons/logging/LogFactory”错误的解决方式

    用spring-framework-4.2.6.RELEASE-dist时,发生了如下的错误: [java] view plain copy Exception in thread "mai ...