6.7MongoDB 实验——数据备份和恢复

    • 第一关 数据备份
    • 第二关 数据恢复

以下代码/命令可直接复制粘贴

第一关 数据备份

mongoimport -d test1 -c person --file /home/example/person.json --type json
mongoimport -d test2 -c student --type csv --headerline --ignoreBlanks --file /home/example/student.csv

mkdir /opt
mkdir /opt/mongodb
mkdir /opt/mongodb_1
mkdir /opt/collection_1
mkdir /opt/collection_2
mkdir /opt/mongodb_2
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -o /opt/mongodb
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test1 -o /opt/mongodb_1
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test1 -c person -o /opt/collection_1
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test2 -c student -o /opt/collection_2 --gzip
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test2 -o /opt/mongodb_2 --gzip

第二关 数据恢复

mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin  /opt/mongodb
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest1 /opt/mongodb_1/test1
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest2 -c person /opt/collection_1/test1/person.bson
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest3 -c student --drop /opt/collection_2/test2/student.bson.gz --gzip
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest4 --drop /opt/mongodb_2/test2 --gzip
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐