用于源代码控制的 MongoDB 备份为文本而不是二进制 [英] MongoDB backup as text rather than binary for source control

查看:20
本文介绍了用于源代码控制的 MongoDB 备份为文本而不是二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以这样做吗?

我问的原因是将备份作为预提交钩子执行似乎很有用,并以某种方式在提交的代码中包含基于文本的备份,这样源代码管理 (git) 中的每个修订都有与之关联的必要备份.

The reason I ask is that it would seem useful to perform a backup as a pre-commit hook, and include that text-based backup somehow with the code being committed, such that every revision in source control (git) has the necessary backups associated with it.

我意识到有可能让 MongoDB 将它的所有集合作为 JSON 输出,并以这种方式备份它们,但是这样就不会包含数据库和集合元数据.

I realise that it may be possible to get MongoDB to spit out all it's collections as JSON, and back them up that way, but then this would not contain DB and collection metadata.

核心问题是,当分支以对服务器端代码进行更改,然后恢复该分支时,我们很容易忘记进行必要的备份以允许有效的恢复(代码将不再匹配架构).有人可能会声称这是程序员错误,但我认为应该有一些自动化,以确保我们拥有一个适用于每个代码版本的数据库版本.这就是项目早期无定形模式的问题……动态模式是 Mongo 的主要祝福和诅咒.

The central concern is that when branching to make changes to server side code, then reverting that branch, it is easy for us to forget to make the necessary backups that will allow an effective revert (code will no longer match schema). One could claim this is programmer error, but I think there should be some automation in place to ensure that we have a version of the database that is current for every version of the code. This is the problem with schemas that are amorphous during the early days of a project... dynamic schemas being Mongo's main blessing and curse.

推荐答案

MongoDB 以 BSON(二进制 JSON)格式存储其数据 因此,如果您要转换为纯文本格式,您应该注意潜在的 数据类型保真度问题.如果您想要对 MongoDB 数据库进行一致的备份,推荐的格式将是 mongodump.不过,我不建议使用二进制数据库备份来膨胀您的 git 存储库.

MongoDB stores its data in a BSON (Binary JSON) format so if you are converting to a text-only format you should be aware of potential data type fidelity issues. If you want a consistent backup of your MongoDB database, the recommended format would be the binary output of mongodump. I wouldn't recommend bloating your git repo with binary DB backups, though.

通常,具有架构灵活性的数据库的解放方面之一是不必在开发过程中担心架构迁移.这确实需要考虑您的应用程序将如何处理不同的架构版本,因此采用更可控的方法也很好.

Typically one of the liberating aspects of a database with schema flexibility is not having to worry about schema migrations during development. This does require some consideration on how your application will handle varying schema versions, so having a more controlled approach is fine, too.

由于您的主要目标是使数据库架构与代码更改保持同步,因此更好的方法是研究架构迁移工具.这些通常允许您创建具有定义的应用程序顺序到数据库的升级/降级代码片段,并确保您的应用程序代码未处理的任何数据库架构更改被捕获在迁移脚本.

Since your main goal is to keep your database schema in sync with code changes, a much better approach would be to investigate schema migration tools. These typically allow you to create upgrade/downgrade code snippets with a defined order of application to a database, and ensure that any changes to the database schema that aren't handled by your application code are captured in the migration scripts.

一些示例工具:

这篇关于用于源代码控制的 MongoDB 备份为文本而不是二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆