java 嵌入式 H2 数据库引擎如何在运行时进行备份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2036117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:03:23  来源:igfitidea点击:

How to back up the embedded H2 database engine while it is running?

javadatabasebackuph2

提问by Winston Chen

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:

我想用 H2 数据库引擎构建一个 Web 应用程序。但是,读了这篇教程后,我还是不知道如何在数据库运行时备份数据:

http://www.h2database.com/html/tutorial.html#upgrade_backup_restore

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?

H2 是否将其存储的文件输出到文件系统中的某个位置?我可以只备份输出的文件吗?

采纳答案by Jay Askren

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.

H2 存储在文件系统上,但最好使用您引用的备份工具,因为文件格式可能会在 H2 版本之间发生变化。如果升级 H2,它可能不再能够读取它在以前版本中创建的文件。此外,如果您复制它使用的文件,我建议先关闭数据库,否则 H2 可能无法读取复制的文件。

The location of the file depends on the jdbc url you specify. See the FAQ: http://www.h2database.com/html/faq.html

文件的位置取决于您指定的 jdbc url。请参阅常见问题解答:http: //www.h2database.com/html/faq.html

回答by Jus12

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):

根据您链接教程,不建议在数据库运行时通过复制文件来备份数据库。这是在数据库运行时备份数据库的正确方法(Scala 代码,但可以轻松转换为 Java)(源代码):

val connection:java.sql.Connection = ??? // get a database connection 
connection.prepareStatement("BACKUP TO 'myFile.zip'").executeUpdate