arjuncc
arjuncc

Reputation: 3287

Backup from H2 database as .sql file

I have an application, I need to back up the DB as .sql file. I have the following code but it backups as .zip

public void backupDatabase(Connection conn) throws SQLException {
        //to get absolute path
        String path = appConfigReader.getDbAbsolutePath();
        Statement stmt = conn.createStatement();
        String sql = "BACKUP TO '"+ path+"myApp001.zip'";
        stmt.executeUpdate(sql);
    }

Is there any ways to copy .sql file to a location inside my server itself. I know there is commands, but i need java code. Is there any way to do this with 'org.h2.tools.RunScrip' or 'Scrip' ? or any other method

Upvotes: 1

Views: 1587

Answers (1)

arjuncc
arjuncc

Reputation: 3287

Finally i got a solution like this. And its work

    /**Thank god, hopefully this will backup the database*/
    public void backupDatabase(Connection conn) throws SQLException {
        LOGGER.info("Inside backupDatabase");
        //used to get DB location
        String path = appConfigReader.getDbAbsolutePath();
        Statement stmt = conn.createStatement();
        //used to get DB url
        String dburl = AppConfigReader.getInstance().getDatabaseUrl();
        String[] bkp = {"-url", dburl, "-user", "sa", "-password","sa", "-script", path+"/myApp001.sql"};
        Script.main(bkp);
        LOGGER.info("backupDatabase method executed successfully");
    }

Upvotes: 1

Related Questions