mongodb数据库备份

mongodb 备份恢复
1. 备份数据库
脚本如下
#!/bin/bash
source /etc/profile
export PATH=/bin:/usr/bin:/usr/local/bin

#backup MongoDB
# Check necessary commands are available
while getopts "I:P:U:C:" opt
do
  case "$opt" in
  I) IP=${OPTARG};;
  P) PORT=${OPTARG};;
  U) USER=${OPTARG};;
  C) PWD=${OPTARG};;
  *) echo "Unknown option: $opt" ;;
  esac
done
if [[ -z $IP ]]; then 
  echo "Error: ip not found"; exit 1
else
  DB_IP=$IP
fi

if [[ -z $PORT ]]; then 
  echo "Error: port not found"; exit 1
else
  DB_PORT=$PORT
fi

if [[ -z $USER ]]; then 
  echo "Error: USER not found"; exit 1
else
  USERNAME=$USER
fi

if [[ -z $PWD ]]; then 
  echo "Error: PWD not found"; exit 1
else
  PASSWD=$PWD
fi

echo ${DB_IP}
echo ${DB_PORT}
echo ${MONGO_USER}
echo ${MONGO_PASSWD}


#mongodump path
MONGO_BASE=/usr/local/mongodb/bin/mongo
MONGO_DUMP=/usr/local/mongodb/bin/mongodump
TODAY=`date +"%Y%m%d%H%M%S"`
MONGO_HOST=${DB_IP}
MONGO_PORT=${DB_PORT}
#get current datetime
DATE=`date +%Y%m%d`
MONGO_USER=${USERNAME}
DATABASE_NAMES='ALL'
MONGO_PASSWD=${PASSWD}
## Number of days to keep local backup copy
BACKUP_RETAIN_DAYS=15
#backup temp dir
BASE_DIR=/data/db_backup/${DB_IP}_Mongodb_${MONGO_PORT}
BACKUP_DIR=${BASE_DIR}/${MONGO_HOST}_${MONGO_PORT}${DATE}
AUTH_PARAM=" --username ${MONGO_USER} --authenticationDatabase "admin" --password ${MONGO_PASSWD} "

echo "**********************************************************************"
echo "Database ip address is :${DB_IP}"
echo "Database connect port is :${DB_PORT}"
echo "**********************************************************************"
echo "1. Check mogodump Whether the execution file exists: "
if [ ! -f "${MONGO_DUMP}" ];then
  echo "mongodump the command does not exist, check the correct path." 
  exit 1
fi

[[ $? -eq 0 ]] && echo -e "\033[37;32;5m[The execution file ${MONGO_DUMP} is exists]\033[39;49;0m"
sleep 0.5
 
#Check the operation status of mongodb
echo -e "\n2.Check the operation status of mongodb"
AUTH_PARAM=" --host ${MONGO_HOST} --port ${MONGO_PORT} --username ${MONGO_USER} --password ${MONGO_PASSWD} --authenticationDatabase "admin"  "
MONGO_STATUS=`echo "db.serverStatus().connections" | ${MONGO_BASE} ${AUTH_PARAM} | grep current| awk -F ':' '{ print $2 }' |awk -F ',' '{ print $1 }' `
if [ -z ${MONGO_STATUS} ]; then
    echo -e "HALTED: Mongodb does not appear to be running or incorrect username and password"
    exit 1
fi
[[ $? -eq 0 ]] && echo -e "\033[37;32;5m[Mongodb operates normally]\033[39;49;0m"
sleep 0.5


# Create backup directory
echo -e "\n3.Check whether the backup directory exists: "
if ! mkdir -p ${BACKUP_DIR}; then
  echo "Can't create backup directory in ${BACKUP_DIR}. Go and fix it!" 1>&2
  exit 1;
fi
[[ $? -eq 0 ]] && echo -e "\033[37;32;5m[The backup directory ${BACKUP_DIR} was created successfully]\033[39;49;0m"
sleep 0.5


#Start the backup of mongodb
echo -e "\n4.Start the backup of mongodb:"
if [ ${DATABASE_NAMES} = "ALL" ]; then
    echo "Running backup for all databases at $(date +"%Y-%m-%d:%H-%M-%S"). "
    echo "--------------------------------------------"
    ${MONGO_DUMP} --host ${MONGO_HOST}  ${AUTH_PARAM}  --out ${BACKUP_DIR}/
    if [ $? -eq 0 ]; then
                echo -e "Daabase dump is successfully! \n"
        else
                echo -e "Daabase dump is failed!\n"
                exit 1;
        fi
else
    echo "Running backup for selected databases"
    for DB_NAME in ${DATABASE_NAMES}
    do
        ${MONGO_DUMP} --host ${MONGO_HOST} --db ${DB_NAME} ${AUTH_PARAM}  --out $${BACKUP_DIR}/
                if [ $? -eq 0 ]; then
                        echo -e "Daabase dump is successfully! \n"
                else
                        echo -e "Daabase dump is failed!\n"
                exit 1;
                fi
    done
fi
[[ $? -eq 0 ]] && echo -e "\033[37;32;5m[All database of Mongodb successfully backed up]\033[39;49;0m"
sleep 1

#Start compressing backup files
#echo -e "\n5.Start compressing backup files"
#cd ${BASE_DIR}
#tar -Pzcvf tmp.tar.gz ${BACKUP_DIR}
#if [ $? -eq 0 ]; then
#         echo -e "Compressing backup files is successfully! \n"
#else
#        echo -e " Compressing backup files is failed!\n"
#exit 1;
#fi
#echo -e "\n6.Rename compressing backup files"
#MD5=`md5sum tmp.tar.gz | awk '{print $1}'`
#TAR_BAK="mongodbbak_${DATE}_${MD5}.tar.gz"
#mv tmp.tar.gz  ${TAR_BAK}
#echo -e "The backup file name is ${BASE_DIR}/${TAR_BAK}"
#rm -rf ${BACKUP_DIR}
#[[ $? -eq 0 ]] && echo -e "\033[37;32;5m[Compressing and rename backup files is successfully]\033[39;49;0m"
#sleep 1


######## Remove backups older than {BACKUP_RETAIN_DAYS} days  ########
#Delete expired backup file!
   
echo -e "\n7.Clean up expired backup files"   
echo -e "Find expire backup file"
for efile in $(/usr/bin/find $BASE_DIR/ -mtime +1)   
do   
    if [ -d ${efile} ]; then   
    rm -rf "${efile}"   
    echo -e "delete expire full backup file:${efile}" 
    elif [ -f ${efile} ]; then   
    rm -rf "${efile}"   
    echo -e "delete expire full backup file:${efile}" 
    fi;   
done   
[[ $? -eq 0 ]] && echo -e "\033[37;32;5m[Not find expire backup file]\033[39;49;0m"
   
echo   
echo -e "\033[37;32;5m[Complete datetime: `date +%F' '%T' '%w`]\033[39;49;0m"
echo "**********************************************************************"
exit 0 
######################### End of script ##############################


2 执行备份脚本
/bin/bash /data/scripts/mongodb.sh -I 10.0.204.218 -P 27017 -U admin -C aa123456

3 拷贝备份到目标环境

4. 开始恢复
创建用户
Mongodb默认是不需要用户密码就可以连接的,如果使用命令报错"not authorized on admin to execute command ",则表示当前登陆用户不具备相应权限;
解决办法:通过创建一个用户,赋予用户root权限
注意:在createUser之前先use admin切换一下(感谢评论区大佬提醒)。
db.createUser(
    {
        user:"root",
        pwd:"123456",
        roles:[{role:"root",db:"admin"}]
    }
);
添加用户权限成功之后,使用root用户登陆,再次使用命令即可成功!!!
附:添加用户时各个角色对应权限
1.数据库用户角色:read、readWrite;
2.数据库管理角色:dbAdmin、dbOwner、userAdmin;
3.集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4.备份恢复角色:backup、restore
5.所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6.超级用户角色:root

5. 执行恢复
使用mongodump导出时,每一个集合都会对应到以下两个文件:
集合名.metadata.json
集合名.bson
使用mongorestore时,导入的文件直接指定到.bson后缀的文件,如使用:
./mongorestore -h 192.168.1.150:27017
    --authenticationDatabase admin
    -u mongo_user
    -p mongo_pass
    -d my_data
    /Users/xq/Desktop/mongo_wind_data/wind_data/stock_k_line.bson
问题解析
通常来讲是可以直接把文件夹传进去的,有时候会成功,但是如果失败,会报一个编码的报错,且在谷歌上是人类未知领域,几乎找不到解决方案,因此只好曲线救国,循环导出的文件夹里的每一个bson文件
采用如下脚本
for bson_file in `ls 恢复数据的文件夹`: # 循环想要恢复数据的那个文件夹,拿到文件名
do
    cd ~/mongodb-database-tools-macos-x86_64-100.6.1/bin # 进入到包含mongorestore的工具包目录
    if [[ "$bson_file" == *".bson"* ]]; then
        ./mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -u user -p password -d cover_data ~/my_data/$bson_file
        # 使用用户名user,密码password,来运行mongorestore,直接恢复具体的 ~/my_data/xxx.bson文件
    fi
done

示例:
for bson_file in `ls /data/software/10.012313_2701720240308/assets_monitor`:
do
    cd /data/myapp/mongodb/bin/
    if [[ "$bson_file" == *".bson"* ]]; then
        ./mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -u root -p 123456 -d dbtest01 /data/software/12313_2701720240308/assets_monitor/$bson_file
    fi
done
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值