#!/bin/bash# export mongoDB tables to json files# created by Gabriel(Xu Qiao) in 11-AUG-2020# edit config below# put all table names into "target.txt" which is in the same directory with this shell script# !!notice : 1 table name per line
mongo_dir=/home/hadoop/mongodb-linux-x86_64-3.2.3
target_dir=/home/hadoop/data
host=xxxxxxxxx.mongodb.rds.aliyuncs.com
port=3717
database=targetDB
user=dba
passwd=helloworld
#
tables=$(cat target.txt);for table in$tables;doecho"exporting table ----- $database.$table"${mongo_dir}/bin/mongoexport --host "$host" \
--port "$port" -u "$user" -p "$passwd" -d "$database" -c "$table" \
-o ${target_dir}/mongoDB_Backup/"$table"/table.json
done
说明: 只是一个简易脚本mongodb的目录为mongo_dir,从官网下载解压即可,主要是用其自带的bin/mongoexport工具在脚本同目录中编辑target.txt,把要导出的表名放入其中,一行一个.导出格式为json每张表最终输出文件为${target_dir}/mongoDB_Backup/表名/table.json#!/bin/bash# export mongoDB tables to json files# created by Gabriel(Xu Qiao)