mongodump对mongodb的备份

1.1 mongodb的部署情况

#### 软件部署的位置
/data/mongodb/apps/mongodb    
    # 是个软链接文件,指向/data/mongodb/apps/mongodb-linux-x86_64-rhel70-4.0.2

#### mongodb实例部署情况(是用操作系统下app用户来启动的)
[root@vm7-200 ~]# tree -Lp 1 /data/mongodb/27017/
/data/mongodb/27017/
├── [drwxr-xr-x]  conf
├── [drwxr-xr-x]  data
├── [drwxr-xr-x]  logs
├── [drwxr-xr-x]  run
└── [drwxr-xr-x]  scripts

5 directories, 0 files

#### mongodb实例开启了认证的
用户:root
密码:root123456
角色:root
认证库:admin

1.2 备份脚本

创建相关的目录

mkdir -p /data/bakmongodb/{data,script}
    # /data/bakmongodb/data目录下存放备份数据(每天一个目录)
    # /data/bakmongodb/script目录下存放以下的脚本

脚本内容

脚本只能root用户来执行(脚本中进行了限制);
这样我的脚本就会放在root用户下(定时任务)

#!/bin/bash
#
# explain: mongodump tool backup MongoDB(mongod)
# author:chenliang   tel:18382024220
# 
#### Define variables
RETVAL=0
CommPath="/data/mongodb/apps/mongodb/bin"
Host="127.0.0.1"
Port="27017"
User="root"
Password="root123456"
AuthenDb="admin"
Time1=$(date +%F-%H:%M)
SaveDir1="/data/bakmongodb/data"
SaveDir2="$SaveDir1/$Time1"

BusinesLibrary=( $(echo "show dbs;" | $CommPath/mongo --host=$Host --port=$Port --username=$User --password=$Password \
--authenticationDatabase=$AuthenDb --shell |sed '1,4'd|sed '$'d|cut -d " " -f1|grep -Ev "admin|config|local" ) 
)

#### Determine the user to execute
if [ $UID -ne $RETVAL ];then
   echo "Must be root to run scripts"
   exit 1
fi

#### Determine $SaveDir1 dir is exists.
#### if it does exists,create a directory.
#### Failed to create directory,exit script.
if [ ! -d $SaveDir1  ];then
   mkdir -p $SaveDir1
   RETVAL=$?
   if [ $RETVAL -eq 0   ];then
      echo "Create \"$SaveDir1\" directory successful"
     else
      echo "Create \"$SaveDir1\" directory failure"
      exit 1
   fi
fi

#### Determine $SaveDir2 dir is exists.
#### if it does exists,create a directory.
#### Failed to create directory,exit script.
if [ ! -d $SaveDir2  ];then
   mkdir -p $SaveDir2
   RETVAL=$?
   if [ $RETVAL -eq 0   ];then
      echo "Create \"$SaveDir2\" directory successful"
     else
      echo "Create \"$SaveDir2\" directory failure"
      exit 1
   fi
fi

### For loop depots backup
for((i=0;i<${#BusinesLibrary[*]};i++))
do
  $CommPath/mongodump --host=$Host --port=$Port --username=$User --password=$Password --authenticationDatabase=$AuthenDb \
  -d ${BusinesLibrary[i]} -o $SaveDir2 --gzip
done

### Delete data in the last 2 days
find $SaveDir1 -maxdepth 1 -type d -mtime +2 |xargs rm -fr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值