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

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


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


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 


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