1. 介绍

在Mongodb中我们使用mongodump命令来备份MongoDB数据。该命令可以导出所有数据到指定目录中。mongodump命令可以通过参数指定导出的数据量级转存的服务器。

2. mongodump

MongoDB中提供了mongostat 和 mongotop 两个命令来监控MongoDB的运行情况。
语法如下:

  1. >mongodump -h dbhost -d dbname -o dbdirectory

参数解释

  1. -h 指明数据库宿主机的IP
  2. --port 指明数据库的端口
  3. -u 指明数据库的用户名
  4. -p 指明数据库的密码
  5. -d 指明数据库的名字
  6. -c 指明collection的名字
  7. -o 指明到要导出的文件名
  8. -q 指明导出数据的过滤条件
  9. --authenticationDatabase 验证数据的名称
  10. --gzip 备份时压缩
  11. --oplog use oplog for taking a point-in-time snapshot

快速命令

  1. mkdir full # 全量备份
  2. mkdir db_cmz # 单库备份
  3. mkdir db_cmz_col # 库中表备份
  4. mkdir db_cmz_gzip # 库备份压缩
  5. mkdir db_cmz_gzip_col # 库中表备份压缩
  6. mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -o ./full
  7. mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -o ./db_cmz/
  8. mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col -o ./db_cmz_col
  9. mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -o ./db_cmz_gzip/ --gzip
  10. ongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col -o ./db_cmz_gzip_col/ --gzip

2.0 准备

  1. [root@leco app]# mkdir full # 全量备份
  2. [root@leco app]# mkdir db_cmz # 单库备份
  3. [root@leco app]# mkdir db_cmz_col # 库中表备份
  4. [root@leco app]# mkdir db_cmz_gzip # 库备份压缩
  5. [root@leco app]# mkdir db_cmz_gzip_col # 库中表备份压缩
  6. > use cmz
  7. switched to db cmz
  8. > show tables;
  9. col
  10. col2
  11. col3
  12. test
  13. > db.col.find()
  14. { "_id" : ObjectId("5c76214dfec6d367f28a65db"), "title" : "Books 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "cmz", "url" : "https://caimengzhi.github.io/books", "tags" : [ "Java", "database", "web" ], "likes" : 200 }
  15. { "_id" : ObjectId("5c76214dfec6d367f28a65dc"), "title" : "Books 教程", "description" : "Python 语言是最近比较流行的语言", "by" : "cmz", "url" : "https://caimengzhi.github.io/books", "tags" : [ "Python", "database", "web" ], "likes" : 150 }
  16. { "_id" : ObjectId("5c76214efec6d367f28a65dd"), "title" : "Books 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "cmz", "url" : "https://caimengzhi.github.io/books", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
  17. { "_id" : ObjectId("5c7632efeb4ea775e2693743"), "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "url" : "https://caimengzhi.github.io/books" }
  18. { "_id" : ObjectId("5c7632efeb4ea775e2693744"), "description" : "Python 语言是最近比较流行的语言", "url" : "https://caimengzhi.github.io/books" }
  19. { "_id" : ObjectId("5c7632efeb4ea775e2693745"), "description" : "MongoDB 是一个 Nosql 数据库", "url" : "https://caimengzhi.github.io/books" }

2.1 全库备份

  1. [root@leco app]# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -o ./full
  2. 2019-07-27T15:11:23.080+0800 writing admin.system.version to
  3. 2019-07-27T15:11:23.095+0800 done dumping admin.system.version (1 document)
  4. 2019-07-27T15:11:23.095+0800 writing cmz.col to
  5. 2019-07-27T15:11:23.095+0800 writing cmz.col2 to
  6. 2019-07-27T15:11:23.095+0800 writing cmz.test to
  7. 2019-07-27T15:11:23.095+0800 writing cmz.col3 to
  8. 2019-07-27T15:11:23.099+0800 done dumping cmz.col (6 documents)
  9. 2019-07-27T15:11:23.099+0800 done dumping cmz.col2 (5 documents)
  10. 2019-07-27T15:11:23.100+0800 done dumping cmz.test (1 document)
  11. 2019-07-27T15:11:23.100+0800 done dumping cmz.col3 (0 documents)
  12. [root@leco app]# ls ./full/
  13. admin cmz

2.2 备份cmz库

  1. [root@leco app]# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -o ./db_cmz/
  2. 2019-07-27T15:12:44.375+0800 writing cmz.col to
  3. 2019-07-27T15:12:44.377+0800 writing cmz.col2 to
  4. 2019-07-27T15:12:44.377+0800 writing cmz.test to
  5. 2019-07-27T15:12:44.378+0800 writing cmz.col3 to
  6. 2019-07-27T15:12:44.379+0800 done dumping cmz.col (6 documents)
  7. 2019-07-27T15:12:44.380+0800 done dumping cmz.col2 (5 documents)
  8. 2019-07-27T15:12:44.380+0800 done dumping cmz.test (1 document)
  9. 2019-07-27T15:12:44.381+0800 done dumping cmz.col3 (0 documents)
  10. [root@leco app]# ls db_cmz
  11. cmz
  12. [root@leco app]# ls db_cmz/cmz/
  13. col2.bson col2.metadata.json col3.bson col3.metadata.json col.bson col.metadata.json test.bson test.metadata.json

2.3 备份cmz库下的col集合

  1. [root@leco app]# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col -o ./db_cmz_col
  2. 2019-07-27T15:13:15.706+0800 writing cmz.col to
  3. 2019-07-27T15:13:15.707+0800 done dumping cmz.col (6 documents)
  4. [root@leco app]# ls db_cmz
  5. db_cmz/ db_cmz_col/ db_cmz_gzip/ db_cmz_gzip_col/
  6. [root@leco app]# ls db_cmz_col/
  7. cmz
  8. [root@leco app]# ls db_cmz_col/cmz/
  9. col.bson col.metadata.json

2.4 压缩备份库

  1. [root@leco app]# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -o ./db_cmz_gzip/ --gzip
  2. 2019-07-27T15:13:53.005+0800 writing cmz.col to
  3. 2019-07-27T15:13:53.005+0800 writing cmz.col2 to
  4. 2019-07-27T15:13:53.005+0800 writing cmz.test to
  5. 2019-07-27T15:13:53.005+0800 writing cmz.col3 to
  6. 2019-07-27T15:13:53.008+0800 done dumping cmz.col (6 documents)
  7. 2019-07-27T15:13:53.009+0800 done dumping cmz.col2 (5 documents)
  8. 2019-07-27T15:13:53.011+0800 done dumping cmz.test (1 document)
  9. 2019-07-27T15:13:53.012+0800 done dumping cmz.col3 (0 documents)
  10. [root@leco app]# ls db_cmz_gzip
  11. db_cmz_gzip/ db_cmz_gzip_col/
  12. [root@leco app]# ls db_cmz_gzip/cmz/
  13. col2.bson.gz col2.metadata.json.gz col3.bson.gz col3.metadata.json.gz col.bson.gz col.metadata.json.gz test.bson.gz test.metadata.json.gz

2.5 压缩备份单表

  1. [root@leco app]# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col -o ./db_cmz_gzip_col/ --gzip
  2. 2019-07-27T15:14:26.475+0800 writing cmz.col to
  3. 2019-07-27T15:14:26.476+0800 done dumping cmz.col (6 documents)
  4. [root@leco app]# ls db_cmz_gzip_col/cmz/
  5. col.bson.gz col.metadata.json.gz

3. mongorestore

mongodb使用 mongorestore 命令来恢复备份的数据。

  1. mongorestore -h <hostname><:port> -d dbname <path>

参数解释

  1. --host <:port>, -h <:port>:ongoDB所在服务器地址,默认为: localhost:27017
  2. -h 指明数据库宿主机的IP
  3. -u 指明数据库的用户名
  4. -p 指明数据库的密码
  5. -d 指明数据库的名字
  6. -c 指明collection的名字
  7. -o 指明到要导出的文件名
  8. -q 指明导出数据的过滤条件
  9. --authenticationDatabase 验证数据的名称
  10. --gzip 备份时压缩
  11. --oplog use oplog for taking a point-in-time snapshot
  12. --drop 恢复的时候把之前的集合drop

3.1 整个恢复

3.3.1 全库备份中恢复单库

  1. [root@leco app]# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin --drop ./full
  2. 2019-07-27T15:16:24.608+0800 preparing collections to restore from
  3. 2019-07-27T15:16:24.656+0800 reading metadata for cmz.col from full/cmz/col.metadata.json
  4. 2019-07-27T15:16:24.661+0800 reading metadata for cmz.col3 from full/cmz/col3.metadata.json
  5. 2019-07-27T15:16:24.670+0800 reading metadata for cmz.test from full/cmz/test.metadata.json
  6. 2019-07-27T15:16:24.681+0800 reading metadata for cmz.col2 from full/cmz/col2.metadata.json
  7. 2019-07-27T15:16:24.707+0800 restoring cmz.col from full/cmz/col.bson
  8. 2019-07-27T15:16:24.724+0800 restoring cmz.col3 from full/cmz/col3.bson
  9. 2019-07-27T15:16:24.725+0800 no indexes to restore
  10. 2019-07-27T15:16:24.725+0800 finished restoring cmz.col3 (0 documents)
  11. 2019-07-27T15:16:24.741+0800 restoring cmz.test from full/cmz/test.bson
  12. 2019-07-27T15:16:24.756+0800 restoring cmz.col2 from full/cmz/col2.bson
  13. 2019-07-27T15:16:24.768+0800 no indexes to restore
  14. 2019-07-27T15:16:24.768+0800 finished restoring cmz.test (1 document)
  15. 2019-07-27T15:16:24.768+0800 no indexes to restore
  16. 2019-07-27T15:16:24.768+0800 finished restoring cmz.col (6 documents)
  17. 2019-07-27T15:16:24.768+0800 no indexes to restore
  18. 2019-07-27T15:16:24.768+0800 finished restoring cmz.col2 (5 documents)
  19. 2019-07-27T15:16:24.768+0800 done

4. mongoexport

Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。

  1. mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv -o 文件名

参数解释

  1. -h 指明数据库宿主机的IP
  2. -u 指明数据库的用户名
  3. -p 指明数据库的密码
  4. -d 指明数据库的名字
  5. -c 指明collection的名字
  6. -f 指明要导出那些列,以逗号分割,-f uid,name,age导出uid,name,age这三个字段
  7. -o 指明到要导出的文件名
  8. -q 指明导出数据的过滤条件,-q '{ "uid" : "100" }' 导出uid100的数据
  9. --type 指定文件类型
  10. --authenticationDatabase 验证数据的名称

4.1 导出整张表

  1. [root@leco bak]# mongoexport -d cmz -c col -o ./col.dat
  2. 2019-07-27T14:26:12.934+0800 connected to: localhost
  3. 2019-07-27T14:26:12.952+0800 exported 3 records
  4. [root@leco bak]# ls
  5. admin cmz col.dat
  6. [root@leco bak]# cat col.dat
  7. {"_id":{"$oid":"5c76214dfec6d367f28a65db"},"title":"Books 教程","description":"Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。","by":"cmz","url":"https://caimengzhi.github.io/books","tags":["Java","database","web"],"likes":200.0}
  8. {"_id":{"$oid":"5c76214dfec6d367f28a65dc"},"title":"Books 教程","description":"Python 语言是最近比较流行的语言","by":"cmz","url":"https://caimengzhi.github.io/books","tags":["Python","database","web"],"likes":150.0}
  9. {"_id":{"$oid":"5c76214efec6d367f28a65dd"},"title":"Books 教程","description":"MongoDB 是一个 Nosql数据库","by":"cmz","url":"https://caimengzhi.github.io/books","tags":["mongodb","database","NoSQL"],"likes":100.0}

4.2 导出表中部分字段

  1. [root@leco bak]# mongoexport -d cmz -c col --csv -f description,tags,likes -o ./col.csv
  2. 2019-07-27T14:28:16.132+0800 csv flag is deprecated; please use --type=csv instead
  3. 2019-07-27T14:28:16.134+0800 connected to: localhost
  4. 2019-07-27T14:28:16.145+0800 exported 3 records
  5. [root@leco bak]# cat col.csv
  6. description,tags,likes
  7. Java 是由Sun Microsystems公司于19955月推出的高级程序设计语言。,"[""Java"",""database"",""web""]",200
  8. Python 语言是最近比较流行的语言,"[""Python"",""database"",""web""]",150
  9. MongoDB 是一个 Nosql 数据库,"[""mongodb"",""database"",""NoSQL""]",100

4.3 根据条件导出数据

  1. [root@leco bak]# mongoexport -d cmz -c col -q '{likes:{$gt:150}}' -o ./col.json
  2. 2019-07-27T14:29:32.399+0800 connected to: localhost
  3. 2019-07-27T14:29:32.407+0800 exported 1 record
  4. [root@leco bak]# cat col.json
  5. {"_id":{"$oid":"5c76214dfec6d367f28a65db"},"title":"Books 教程","description":"Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。","by":"cmz","url":"https://caimengzhi.github.io/books","tags":["Java","database","web"],"likes":200.0}

4.4 other

  1. [root@leco back]# mongoexport -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col -o ./col.dat
  2. 2019-07-27T14:45:13.623+0800 connected to: 127.0.0.1:27017
  3. 2019-07-27T14:45:13.643+0800 exported 3 records
  4. [root@leco back]# ls
  5. col.dat
  6. [root@leco back]# cat col.dat
  7. {"_id":{"$oid":"5c76214dfec6d367f28a65db"},"title":"Books 教程","description":"Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。","by":"cmz","url":"https://caimengzhi.github.io/books","tags":["Java","database","web"],"likes":200.0}
  8. {"_id":{"$oid":"5c76214dfec6d367f28a65dc"},"title":"Books 教程","description":"Python 语言是最近比较流行的语言","by":"cmz","url":"https://caimengzhi.github.io/books","tags":["Python","database","web"],"likes":150.0}
  9. {"_id":{"$oid":"5c76214efec6d367f28a65dd"},"title":"Books 教程","description":"MongoDB 是一个 Nosql数据库","by":"cmz","url":"https://caimengzhi.github.io/books","tags":["mongodb","database","NoSQL"],"likes":100.0}
  10. [root@leco back]# mongoexport -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col --type=csv -f description,url -o ./col_csv.dat
  11. 2019-07-27T14:47:01.048+0800 connected to: 127.0.0.1:27017
  12. 2019-07-27T14:47:01.059+0800 exported 3 records
  13. [root@leco back]# cat col_csv.dat
  14. description,url
  15. Java 是由Sun Microsystems公司于19955月推出的高级程序设计语言。,https://caimengzhi.github.io/books
  16. Python 语言是最近比较流行的语言,https://caimengzhi.github.io/books
  17. MongoDB 是一个 Nosql 数据库,https://caimengzhi.github.io/books

5. mongoimport

Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。
参数解释

  1. -h 指明数据库宿主机的IP
  2. -u 指明数据库的用户名
  3. -p 指明数据库的密码
  4. -d 指明数据库的名字
  5. -c 指明collection的名字
  6. -f 指明要导出那些列
  7. -o 指明到要导出的文件名
  8. -q 指明导出数据的过滤条件
  9. --drop 插入之前先删除原有的
  10. --headerline 指明第一行是列名,不需要导入。
  11. -j 同时运行的插入操作数(默认为1),并行
  12. --authenticationDatabase 验证数据的名称

5.1 恢复导出的表数据

  1. [root@leco back]# mongoimport -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col --drop ./col.dat
  2. 2019-07-27T14:48:38.415+0800 connected to: 127.0.0.1:27017
  3. 2019-07-27T14:48:38.416+0800 dropping: cmz.col
  4. 2019-07-27T14:48:38.497+0800 imported 3 documents

5.2 部分字段的表数据导入

  1. mongoimport -d cmz -c col --upsertFields description,tags,likes ./col.dat

5.3 恢复csv文件

  1. [root@leco back]# mongoimport -h 127.0.0.1:27017 --authenticationDatabase admin -d cmz -c col --type=csv --headerline --file ./col_csv.dat
  2. 2019-07-27T14:49:19.561+0800 connected to: 127.0.0.1:27017
  3. 2019-07-27T14:49:19.563+0800 imported 3 documents