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

发布于 2024-08-18 01:42:41 字数 289 浏览 11 评论 0原文

我想用 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

压抑⊿情绪 2024-08-25 01:42:42

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

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

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

val connection:java.sql.Connection = ??? // get a database connection 
connection.prepareStatement("BACKUP TO 'myFile.zip'").executeUpdate 
我不吻晚风 2024-08-25 01:42:42

感谢 Jus12 的好回答。我将其改编为 Spring Data 中的 JPARepositories,并想在这里分享它,因为我在网上找不到类似的答案:

@Modifying
@Transactional
@Query(value = "BACKUP TO ?1", nativeQuery = true)
int backupDB(String path);

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:

@Modifying
@Transactional
@Query(value = "BACKUP TO ?1", nativeQuery = true)
int backupDB(String path);
如何视而不见 2024-08-25 01:42:42
try {
    Class.forName("org.h2.Driver");
    Connection con = DriverManager.getConnection("jdbc:h2:"+"./Dbfolder/dbname", "username", "password" );
    Statement stmt = con.createStatement();
    con.prepareStatement("BACKUP TO 'backup.zip'").executeUpdate();

} catch(Exception ex) {
    JOptionPane.showMessageDialog(null, ex.toString());
}
try {
    Class.forName("org.h2.Driver");
    Connection con = DriverManager.getConnection("jdbc:h2:"+"./Dbfolder/dbname", "username", "password" );
    Statement stmt = con.createStatement();
    con.prepareStatement("BACKUP TO 'backup.zip'").executeUpdate();

} catch(Exception ex) {
    JOptionPane.showMessageDialog(null, ex.toString());
}
Hello爱情风 2024-08-25 01:42:41

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文