MongoDB 备份与恢复

MongoDB数据备份

在Mongodb中使用mongodump命令来备份MongoDB数据。该命令可以导出所有数据到指定目录中。

mongodump命令可以通过参数指定备份服务器

为什么要定期进行数据库的备份?

数据备份就是要保存数据的完整性,防止断电,病毒感染等等情况,使数据丢失。有必要的话,最好勤备份,防止数据丢失。

最主要的原因:要知道,在地球上网是很危险的,即使做好安全预防措施,也难免会发生不可预想的问题。

案例

携程旅行网2015-5-28日11时,携程旅行网瘫痪,网页版和手机APP均不能正常使用。经携程技术排查,确认此次事件是由于员工错误操作导致。由于携程涉及的业务、应用及服务繁多,验证应用与服务之间的功能是否正常运行,花了较长时间。携程官方网站及APP于28日23:29全面恢复正常。

MongoDB 备份与恢复 - 图1

MongoDB 备份与恢复 - 图2

MongoDB 备份与恢复 - 图3

2015-05-27下午6点05分,支付宝无线支付官方微博回应称,由于杭州市萧山区某地光纤被挖断,造成目前少部分用户无法使用支付宝,运营商正在抢修,支付宝工程师正在紧急将用户请求切换至其他机房,受影响的用户正在逐步恢复。用户的资金安全并不会因此受到任何影响。直到晚上7点半,支付宝无线支付官微再次发声,称支付宝目前已恢复正常使用。

阿里巴巴再牛逼,干不过蓝翔挖掘机!

MongoDB 备份与恢复 - 图4

事实证明,在这个世界里,再牛的互联网公司,都干不过蓝翔技校的挖掘机……

什么互联网+,什么4.0,什么大数据,都顶不住传统行业一铲子……

语法

mongodump命令脚本语法如下:

  1. >mongodump -h dbhost -d dbname -o dbdirectory
  • -h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
  • -d:需要备份的数据库实例,例如:test
  • -o:备份的数据存放位置,例如:/home/mongodump/,当然该目录需要提前建立,这个目录里面存放该数据库实例的备份数据。

实例

创建备份数据库存放位置,执行导出命令

  1. python@ubuntu:~$ sudo rm -rf /home/mongodump/
  2. python@ubuntu:~$ sudo mkdir -p /home/mongodump/
  3. python@ubuntu:~$ sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/

执行以上命令后,客户端会连接到ip为 192.168.17.129 端口号为 27017 的MongoDB服务上,并备份所有数据到 /home/mongodump/目录中。

命令输出结果如下:

  1. 2016-09-15T18:04:30.735+0800 writing itcast.articles to
  2. 2016-09-15T18:04:30.787+0800 writing itcast.inventory to
  3. 2016-09-15T18:04:30.788+0800 writing itcast.itcast to
  4. 2016-09-15T18:04:30.788+0800 writing itcast.sales to
  5. 2016-09-15T18:04:30.790+0800 done dumping itcast.articles (7 documents)
  6. 2016-09-15T18:04:30.790+0800 writing itcast.books to
  7. 2016-09-15T18:04:30.791+0800 done dumping itcast.inventory (6 documents)
  8. 2016-09-15T18:04:30.791+0800 writing itcast.orders to
  9. 2016-09-15T18:04:30.791+0800 done dumping itcast.itcast (5 documents)
  10. 2016-09-15T18:04:30.792+0800 writing itcast.mycol to
  11. 2016-09-15T18:04:30.792+0800 done dumping itcast.sales (5 documents)
  12. 2016-09-15T18:04:30.792+0800 writing itcast.factories to
  13. 2016-09-15T18:04:30.793+0800 done dumping itcast.books (5 documents)
  14. 2016-09-15T18:04:30.793+0800 writing itcast.myLimit to
  15. 2016-09-15T18:04:30.794+0800 done dumping itcast.orders (3 documents)
  16. 2016-09-15T18:04:30.794+0800 writing itcast.col to
  17. 2016-09-15T18:04:30.794+0800 done dumping itcast.mycol (3 documents)
  18. 2016-09-15T18:04:30.794+0800 done dumping itcast.factories (1 document)
  19. 2016-09-15T18:04:30.794+0800 done dumping itcast.myLimit (0 documents)
  20. 2016-09-15T18:04:30.795+0800 done dumping itcast.col (0 documents)
  21. python@ubuntu:~$

mongodump 命令可选参数列表如下所示:

  1. mongodump --help

导出一个集合:

  1. python@ubuntu:/home/mongodump$ sudo mongodump -h 192.168.17.129:27017 -d example -c itcast -o /home/mongodump/
  2. 2016-09-15T20:08:12.856+0800 writing example.itcast to
  3. 2016-09-15T20:08:12.862+0800 done dumping example.itcast (1 document)
  4. python@ubuntu:/home/mongodump$ tree
  5. .
  6. ├── example
  7. ├── itcast.bson
  8. └── itcast.metadata.json
  9. └── itcast
  10. ├── articles.bson
  11. ├── articles.metadata.json
  12. ├── books.bson
  13. ├── books.metadata.json
  14. ├── col.bson
  15. ├── col.metadata.json
  16. ├── factories.bson
  17. ├── factories.metadata.json
  18. ├── inventory.bson
  19. ├── inventory.metadata.json
  20. ├── itcast.bson
  21. ├── itcast.metadata.json
  22. ├── mycol.bson
  23. ├── mycol.metadata.json
  24. ├── myLimit.bson
  25. ├── myLimit.metadata.json
  26. ├── orders.bson
  27. ├── orders.metadata.json
  28. ├── sales.bson
  29. └── sales.metadata.json
  30. 2 directories, 22 files

MongoDB数据恢复

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

语法

mongorestore命令脚本语法如下:

  1. >mongorestore -h dbhost -d dbname --dir dbdirectory
  • -h:MongoDB所在服务器地址
  • -d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
  • —dir:备份数据所在位置,例如:/home/mongodump/itcast/
  • —drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用!

接下来我们执行以下命令:

  1. mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/

执行以上命令输出结果如下:

  1. python@ubuntu:~$ mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/
  2. 2016-09-15T18:16:14.543+0800 building a list of collections to restore from /home/mongodump/itcast dir
  3. 2016-09-15T18:16:14.594+0800 reading metadata for itcast_restore.mycol from /home/mongodump/itcast/mycol.metadata.json
  4. 2016-09-15T18:16:14.605+0800 restoring itcast_restore.mycol from /home/mongodump/itcast/mycol.bson
  5. 2016-09-15T18:16:14.615+0800 restoring indexes for collection itcast_restore.mycol from metadata
  6. 2016-09-15T18:16:14.615+0800 reading metadata for itcast_restore.articles from /home/mongodump/itcast/articles.metadata.json
  7. 2016-09-15T18:16:14.615+0800 reading metadata for itcast_restore.sales from /home/mongodump/itcast/sales.metadata.json
  8. 2016-09-15T18:16:14.616+0800 reading metadata for itcast_restore.inventory from /home/mongodump/itcast/inventory.metadata.json
  9. 2016-09-15T18:16:14.624+0800 finished restoring itcast_restore.mycol (3 documents)
  10. 2016-09-15T18:16:14.626+0800 reading metadata for itcast_restore.books from /home/mongodump/itcast/books.metadata.json
  11. 2016-09-15T18:16:14.633+0800 restoring itcast_restore.articles from /home/mongodump/itcast/articles.bson
  12. 2016-09-15T18:16:14.647+0800 restoring itcast_restore.sales from /home/mongodump/itcast/sales.bson
  13. 2016-09-15T18:16:14.678+0800 restoring itcast_restore.books from /home/mongodump/itcast/books.bson
  14. 2016-09-15T18:16:14.697+0800 restoring itcast_restore.inventory from /home/mongodump/itcast/inventory.bson
  15. 2016-09-15T18:16:14.704+0800 restoring indexes for collection itcast_restore.books from metadata
  16. 2016-09-15T18:16:14.705+0800 restoring indexes for collection itcast_restore.sales from metadata
  17. 2016-09-15T18:16:14.706+0800 restoring indexes for collection itcast_restore.articles from metadata
  18. 2016-09-15T18:16:14.706+0800 restoring indexes for collection itcast_restore.inventory from metadata
  19. 2016-09-15T18:16:14.707+0800 finished restoring itcast_restore.inventory (6 documents)
  20. 2016-09-15T18:16:14.707+0800 reading metadata for itcast_restore.itcast from /home/mongodump/itcast/itcast.metadata.json
  21. 2016-09-15T18:16:14.708+0800 finished restoring itcast_restore.articles (7 documents)
  22. 2016-09-15T18:16:14.708+0800 reading metadata for itcast_restore.orders from /home/mongodump/itcast/orders.metadata.json
  23. 2016-09-15T18:16:14.711+0800 finished restoring itcast_restore.sales (5 documents)
  24. 2016-09-15T18:16:14.718+0800 reading metadata for itcast_restore.factories from /home/mongodump/itcast/factories.metadata.json
  25. 2016-09-15T18:16:14.719+0800 finished restoring itcast_restore.books (5 documents)
  26. 2016-09-15T18:16:14.719+0800 reading metadata for itcast_restore.myLimit from /home/mongodump/itcast/myLimit.metadata.json
  27. 2016-09-15T18:16:14.733+0800 restoring itcast_restore.itcast from /home/mongodump/itcast/itcast.bson
  28. 2016-09-15T18:16:14.756+0800 restoring itcast_restore.orders from /home/mongodump/itcast/orders.bson
  29. 2016-09-15T18:16:14.783+0800 restoring itcast_restore.myLimit from /home/mongodump/itcast/myLimit.bson
  30. 2016-09-15T18:16:14.853+0800 restoring indexes for collection itcast_restore.itcast from metadata
  31. 2016-09-15T18:16:14.855+0800 restoring itcast_restore.factories from /home/mongodump/itcast/factories.bson
  32. 2016-09-15T18:16:14.859+0800 finished restoring itcast_restore.itcast (5 documents)
  33. 2016-09-15T18:16:14.859+0800 reading metadata for itcast_restore.col from /home/mongodump/itcast/col.metadata.json
  34. 2016-09-15T18:16:14.860+0800 restoring indexes for collection itcast_restore.myLimit from metadata
  35. 2016-09-15T18:16:14.875+0800 restoring itcast_restore.col from /home/mongodump/itcast/col.bson
  36. 2016-09-15T18:16:15.056+0800 finished restoring itcast_restore.myLimit (0 documents)
  37. 2016-09-15T18:16:15.056+0800 restoring indexes for collection itcast_restore.orders from metadata
  38. 2016-09-15T18:16:15.056+0800 restoring indexes for collection itcast_restore.factories from metadata
  39. 2016-09-15T18:16:15.057+0800 restoring indexes for collection itcast_restore.col from metadata
  40. 2016-09-15T18:16:15.057+0800 finished restoring itcast_restore.orders (3 documents)
  41. 2016-09-15T18:16:15.122+0800 finished restoring itcast_restore.col (0 documents)
  42. 2016-09-15T18:16:15.122+0800 finished restoring itcast_restore.factories (1 document)
  43. 2016-09-15T18:16:15.122+0800 done

mongorestore 命令可选参数列表如下所示:

  1. mongorestore --help

恢复数据库example下的集合itcast:

  1. python@ubuntu:/home/mongodump$ mongorestore -h 192.168.17.129:27017 -d example_restore --dir /home/mongodump/example/
  2. 2016-09-15T20:19:58.176+0800 building a list of collections to restore from /home/mongodump/example dir
  3. 2016-09-15T20:19:58.177+0800 reading metadata for example_restore.itcast from /home/mongodump/example/itcast.metadata.json
  4. 2016-09-15T20:19:58.206+0800 restoring example_restore.itcast from /home/mongodump/example/itcast.bson
  5. 2016-09-15T20:19:58.208+0800 restoring indexes for collection example_restore.itcast from metadata
  6. 2016-09-15T20:19:58.209+0800 finished restoring example_restore.itcast (1 document)
  7. 2016-09-15T20:19:58.209+0800 done

或者:

  1. python@ubuntu:/home/mongodump$ mongorestore -h 192.168.17.129:27017 -d example_restore -c itcast_restore --dir /home/mongodump/example/itcast.bson
  2. 2016-09-15T20:24:00.372+0800 checking for collection data in /home/mongodump/example/itcast.bson
  3. 2016-09-15T20:24:00.373+0800 reading metadata for example_restore.itcast_restore from /home/mongodump/example/itcast.metadata.json
  4. 2016-09-15T20:24:00.398+0800 restoring example_restore.itcast_restore from /home/mongodump/example/itcast.bson
  5. 2016-09-15T20:24:00.471+0800 restoring indexes for collection example_restore.itcast_restore from metadata
  6. 2016-09-15T20:24:00.471+0800 finished restoring example_restore.itcast_restore (1 document)
  7. 2016-09-15T20:24:00.471+0800 done
  8. python@ubuntu:/home/mongodump$