如何在嵌入式H2数据库引擎运行时对其进行备份?
我想用 H2 数据库引擎构建一个 Web 应用程序。但是,读完本教程后,我仍然不知道如何在数据库运行时备份数据:
http://www.h2database.com/html/tutorial.html#upgrade_backup_restore
H2 是否将其存储的文件输出到文件系统中的某个位置?我可以只备份输出的文件吗?
I would like to build up an web application with H2 database engine. However, I still don't know how to back up the data while the database is running after reading this tutorial:
http://www.h2database.com/html/tutorial.html#upgrade_backup_restore
Does H2 output its stored file to somewhere in the file system? Can I just back up the outputted files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您链接的教程,不建议通过复制来备份数据库运行时的文件。这是在数据库运行时备份数据库的正确方法(Scala 代码,但可以轻松转换为 Java)(来源):
As per the tutorial you linked, it is not recommended to backup the database by copying the files while it is running. Here is the right way to backup the database while it is running (Scala code, but can be easily converted to Java) (Source):
感谢 Jus12 的好回答。我将其改编为 Spring Data 中的 JPARepositories,并想在这里分享它,因为我在网上找不到类似的答案:
Thx to Jus12 for the nice answer. I adapted it for JPARepositories in Spring Data and would like to share it here as I couldn't find a similar answer online:
H2 存储在文件系统上,但最好使用您引用的备份工具,因为文件格式可能会在 H2 版本之间发生变化。如果您升级 H2,它可能无法再读取在先前版本中创建的文件。另外,如果你复制它使用的文件,我建议先关闭数据库,否则H2可能无法读取复制的文件。
文件的位置取决于您指定的 jdbc url。请参阅常见问题解答:
http://www.h2database.com/html/faq.html
H2 is stored on the file system, but it would be better to use the backup tools that you reference, because the file format can change between versions of H2. If you upgrade H2, it may not any longer be able to read the files it created in a previous version. Also, if you copy the files it uses, I would recommend shutting the database down first, otherwise the copied files may be unreadable by H2.
The location of the file depends on the jdbc url you specify. See the FAQ:
http://www.h2database.com/html/faq.html