Skip to content

Instantly share code, notes, and snippets.

@cwdesautels
Last active January 17, 2022 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwdesautels/11188409 to your computer and use it in GitHub Desktop.
Save cwdesautels/11188409 to your computer and use it in GitHub Desktop.
Sample using H2's Backup and Restore tools
package h2;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.jdbcx.JdbcDataSource;
import org.h2.tools.Backup;
public class H2BackupTool
{
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
try
{
JdbcDataSource datasource = new JdbcDataSource();
datasource.setUrl("jdbc:h2:memFS:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
conn = datasource.getConnection("sa", "sa");
stmt = conn.createStatement();
stmt.execute("create schema test authorization sa");
stmt.execute("create table test.a(id int primary key)");
stmt.execute("insert into test.a (id) values (0)");
stmt.execute("insert into test.a (id) values (1)");
ResultSet rs = stmt.executeQuery("select * from test.a");
System.out.print("Expected 2: ");
if (rs.next())
{
System.out.print(rs.getInt(1));
while (rs.next())
{
System.out.println(", " + rs.getInt(1));
}
}
stmt.close();
conn.close();
System.out.println("Backing up test");
Backup.execute("C:\\h2\\db\\" + H2BackupTool.class.getSimpleName() + ".zip", "memFS:", "test", true);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (stmt != null)
{
try
{
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}
package h2;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.jdbcx.JdbcDataSource;
import org.h2.tools.Restore;
public class H2RestoreTool
{
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
try
{
System.out.println("Restoring test");
Restore.execute("C:\\h2\\db\\" + H2BackupTool.class.getSimpleName() + ".zip", "memFS:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", "test");
JdbcDataSource datasource = new JdbcDataSource();
datasource.setUrl("jdbc:h2:memFS:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
conn = datasource.getConnection("sa", "sa");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test.a");
System.out.print("Expected 2: ");
if (rs.next())
{
System.out.print(rs.getInt(1));
while (rs.next())
{
System.out.print(", " + rs.getInt(1));
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (stmt != null)
{
try
{
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}
@shredmaster
Copy link

shredmaster commented Nov 30, 2021

i'm running to following error.

org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "TEST" not found; SQL statement:
select * from test.a [90079-200]

the zip file has the correct statements in the file by using SCRIPT TO

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("org.h2.Driver");
        String url = "jdbc:h2:zip:~/app/H2BackupTool.zip!/test";
        Connection conn = DriverManager.getConnection(url + ";DB_CLOSE_DELAY=-1;USER=sa;PASSWORD=sa");
        Statement stat = conn.createStatement();
        stat.execute("SCRIPT TO 'H2BackupTool.sql'");
        stat.close();
        conn.close();
    }

it seems restore.execute is unable to read from "memfs:test" directory, any idea what might cause the problem?

environment

h2: v1.4.200
os: macOS
java: v1.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment