返回介绍

MongoDB 备份与恢复

发布于 2024-10-04 20:57:09 字数 17695 浏览 0 评论 0 收藏 0

MongoDB数据备份

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

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

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

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

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

案例

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

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

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

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

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

语法

mongodump命令脚本语法如下:

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

实例

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

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

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

命令输出结果如下:

2016-09-15T18:04:30.735+0800    writing itcast.articles to 
2016-09-15T18:04:30.787+0800    writing itcast.inventory to 
2016-09-15T18:04:30.788+0800    writing itcast.itcast to 
2016-09-15T18:04:30.788+0800    writing itcast.sales to 
2016-09-15T18:04:30.790+0800    done dumping itcast.articles (7 documents)
2016-09-15T18:04:30.790+0800    writing itcast.books to 
2016-09-15T18:04:30.791+0800    done dumping itcast.inventory (6 documents)
2016-09-15T18:04:30.791+0800    writing itcast.orders to 
2016-09-15T18:04:30.791+0800    done dumping itcast.itcast (5 documents)
2016-09-15T18:04:30.792+0800    writing itcast.mycol to 
2016-09-15T18:04:30.792+0800    done dumping itcast.sales (5 documents)
2016-09-15T18:04:30.792+0800    writing itcast.factories to 
2016-09-15T18:04:30.793+0800    done dumping itcast.books (5 documents)
2016-09-15T18:04:30.793+0800    writing itcast.myLimit to 
2016-09-15T18:04:30.794+0800    done dumping itcast.orders (3 documents)
2016-09-15T18:04:30.794+0800    writing itcast.col to 
2016-09-15T18:04:30.794+0800    done dumping itcast.mycol (3 documents)
2016-09-15T18:04:30.794+0800    done dumping itcast.factories (1 document)
2016-09-15T18:04:30.794+0800    done dumping itcast.myLimit (0 documents)
2016-09-15T18:04:30.795+0800    done dumping itcast.col (0 documents)
python@ubuntu:~$

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

mongodump --help

导出一个集合:

python@ubuntu:/home/mongodump$ sudo mongodump -h 192.168.17.129:27017 -d example -c itcast  -o /home/mongodump/
2016-09-15T20:08:12.856+0800    writing example.itcast to 
2016-09-15T20:08:12.862+0800    done dumping example.itcast (1 document)
python@ubuntu:/home/mongodump$ tree
.
├── example
│   ├── itcast.bson
│   └── itcast.metadata.json
└── itcast
    ├── articles.bson
    ├── articles.metadata.json
    ├── books.bson
    ├── books.metadata.json
    ├── col.bson
    ├── col.metadata.json
    ├── factories.bson
    ├── factories.metadata.json
    ├── inventory.bson
    ├── inventory.metadata.json
    ├── itcast.bson
    ├── itcast.metadata.json
    ├── mycol.bson
    ├── mycol.metadata.json
    ├── myLimit.bson
    ├── myLimit.metadata.json
    ├── orders.bson
    ├── orders.metadata.json
    ├── sales.bson
    └── sales.metadata.json

2 directories, 22 files

MongoDB数据恢复

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

语法

mongorestore命令脚本语法如下:

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

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

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

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

python@ubuntu:~$ mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/
2016-09-15T18:16:14.543+0800    building a list of collections to restore from /home/mongodump/itcast dir
2016-09-15T18:16:14.594+0800    reading metadata for itcast_restore.mycol from /home/mongodump/itcast/mycol.metadata.json
2016-09-15T18:16:14.605+0800    restoring itcast_restore.mycol from /home/mongodump/itcast/mycol.bson
2016-09-15T18:16:14.615+0800    restoring indexes for collection itcast_restore.mycol from metadata
2016-09-15T18:16:14.615+0800    reading metadata for itcast_restore.articles from /home/mongodump/itcast/articles.metadata.json
2016-09-15T18:16:14.615+0800    reading metadata for itcast_restore.sales from /home/mongodump/itcast/sales.metadata.json
2016-09-15T18:16:14.616+0800    reading metadata for itcast_restore.inventory from /home/mongodump/itcast/inventory.metadata.json
2016-09-15T18:16:14.624+0800    finished restoring itcast_restore.mycol (3 documents)
2016-09-15T18:16:14.626+0800    reading metadata for itcast_restore.books from /home/mongodump/itcast/books.metadata.json
2016-09-15T18:16:14.633+0800    restoring itcast_restore.articles from /home/mongodump/itcast/articles.bson
2016-09-15T18:16:14.647+0800    restoring itcast_restore.sales from /home/mongodump/itcast/sales.bson
2016-09-15T18:16:14.678+0800    restoring itcast_restore.books from /home/mongodump/itcast/books.bson
2016-09-15T18:16:14.697+0800    restoring itcast_restore.inventory from /home/mongodump/itcast/inventory.bson
2016-09-15T18:16:14.704+0800    restoring indexes for collection itcast_restore.books from metadata
2016-09-15T18:16:14.705+0800    restoring indexes for collection itcast_restore.sales from metadata
2016-09-15T18:16:14.706+0800    restoring indexes for collection itcast_restore.articles from metadata
2016-09-15T18:16:14.706+0800    restoring indexes for collection itcast_restore.inventory from metadata
2016-09-15T18:16:14.707+0800    finished restoring itcast_restore.inventory (6 documents)
2016-09-15T18:16:14.707+0800    reading metadata for itcast_restore.itcast from /home/mongodump/itcast/itcast.metadata.json
2016-09-15T18:16:14.708+0800    finished restoring itcast_restore.articles (7 documents)
2016-09-15T18:16:14.708+0800    reading metadata for itcast_restore.orders from /home/mongodump/itcast/orders.metadata.json
2016-09-15T18:16:14.711+0800    finished restoring itcast_restore.sales (5 documents)
2016-09-15T18:16:14.718+0800    reading metadata for itcast_restore.factories from /home/mongodump/itcast/factories.metadata.json
2016-09-15T18:16:14.719+0800    finished restoring itcast_restore.books (5 documents)
2016-09-15T18:16:14.719+0800    reading metadata for itcast_restore.myLimit from /home/mongodump/itcast/myLimit.metadata.json
2016-09-15T18:16:14.733+0800    restoring itcast_restore.itcast from /home/mongodump/itcast/itcast.bson
2016-09-15T18:16:14.756+0800    restoring itcast_restore.orders from /home/mongodump/itcast/orders.bson
2016-09-15T18:16:14.783+0800    restoring itcast_restore.myLimit from /home/mongodump/itcast/myLimit.bson
2016-09-15T18:16:14.853+0800    restoring indexes for collection itcast_restore.itcast from metadata
2016-09-15T18:16:14.855+0800    restoring itcast_restore.factories from /home/mongodump/itcast/factories.bson
2016-09-15T18:16:14.859+0800    finished restoring itcast_restore.itcast (5 documents)
2016-09-15T18:16:14.859+0800    reading metadata for itcast_restore.col from /home/mongodump/itcast/col.metadata.json
2016-09-15T18:16:14.860+0800    restoring indexes for collection itcast_restore.myLimit from metadata
2016-09-15T18:16:14.875+0800    restoring itcast_restore.col from /home/mongodump/itcast/col.bson
2016-09-15T18:16:15.056+0800    finished restoring itcast_restore.myLimit (0 documents)
2016-09-15T18:16:15.056+0800    restoring indexes for collection itcast_restore.orders from metadata
2016-09-15T18:16:15.056+0800    restoring indexes for collection itcast_restore.factories from metadata
2016-09-15T18:16:15.057+0800    restoring indexes for collection itcast_restore.col from metadata
2016-09-15T18:16:15.057+0800    finished restoring itcast_restore.orders (3 documents)
2016-09-15T18:16:15.122+0800    finished restoring itcast_restore.col (0 documents)
2016-09-15T18:16:15.122+0800    finished restoring itcast_restore.factories (1 document)
2016-09-15T18:16:15.122+0800    done

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

mongorestore --help

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

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

或者:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文