Backup and Restore H2 database ??

10,007 views
Skip to first unread message

the nigga

unread,
Nov 26, 2011, 4:09:41 PM11/26/11
to H2 Database
Hello

how to create backups h2-database in my internal application ?? I am
looking for but still not found...how to do this using Swing app ?

I see this http://www.h2database.com/h2.pdf:

Backup
java org.h2.tools.Script -url jdbc:h2:~/test -user sa -script test.zip
-options compression zi

Restore
java org.h2.tools.RunScript -url jdbc:h2:~/test -user sa -script
test.zip -options compression zip

but I dont know how to implement this in my swing app...


thanks.

cowwoc

unread,
Nov 26, 2011, 4:25:31 PM11/26/11
to h2-da...@googlegroups.com

My guess:

String[] args =
{
"-url",
"jdbc:h2:~/test",
"-user",
"sa",
"-script",
"test.zip"
};
org.h2.tools.Script.main(args);

Gili

the nigga

unread,
Nov 26, 2011, 5:02:53 PM11/26/11
to H2 Database
Gili...thanks.

I am trying this
String[] bkp = {"-url", "jdbc:h2:tcp://localhost:9001/db/mydb", "-
user", "sa", "-password","123", "-script", "/MyApp/backup/myapp.zip"};
org.h2.tools.Script.main(bkp);

Generate the file.zip but when I am trying extract using WinRar or
other return one error and not unzip the file. The error is file is
corrupted.

Any idea ???

thanks.

On 26 nov, 19:25, cowwoc <cow...@bbs.darktech.org> wrote:
> On 26/11/2011 4:09 PM, the nigga wrote:
>
>
>
>
>
>
>
>
>
> > Hello
>
> > how to create backups h2-database in my internal application ?? I am
> > looking for but still not found...how to do this using Swing app ?
>

> > I see thishttp://www.h2database.com/h2.pdf:

Igal

unread,
Nov 26, 2011, 8:09:05 PM11/26/11
to h2-da...@googlegroups.com
"The Backup tool (org.h2.tools.Backup) can not be used to create a online backup; the database must not be in use while running this program."

to Backup an "open" database you should use the SQL Command: Backup

BACKUP TO 'backup.zip'

maybe that's the reason for the corrupt zip file?

the nigga

unread,
Nov 26, 2011, 9:49:24 PM11/26/11
to H2 Database
Igal, thanks !

Afffsss....Not supported online backup??, so how to I will make backup
to my system ???
I am starting h2 database inside my app.

[code]
//begin h2 server
public static void main(String[] args{
Server s = Server.createTcpServer(new String[]{"-tcp","-
tcpAllowOthers","-tcpPort","9001","-trace"});
s.start();//starting h2 server

//backup


String[] bkp = {"-url",
"jdbc:h2:tcp://localhost:9001/db/mydb",

"-user",


"sa",
"-password",
"123",
"-script",
"/MyApp/backup/myapp.zip"};

org.h2.tools.Script.main(bkp);//backup
}
[/code]

What is this: "except if the file systems support creating
snapshots" ????

thanks.


On 26 nov, 23:09, Igal <d...@21solutions.net> wrote:
> according to the docs athttp://www.h2database.com/html/tutorial.html#upgrade_backup_restore:

Igal

unread,
Nov 26, 2011, 10:16:34 PM11/26/11
to h2-da...@googlegroups.com
I'm (very) new to H2 so don't take any advice from me at face value, but it seems to me that you can simply connect to the database and execute a BACKUP SQL statement, pseudo-code like:

public boolean backupDatabase( String path ) {

  boolean isSuccessful = false;

  Connection conn = getConnection();
  Statement stmt = conn.createStatement();
  
  String sql = "BACKUP TO " + path;

  stmt.executeUpdate( sql );

  // update isSuccessful (or maybe execute the statement in try/catch)

  return isSuccessful;
}

again -- it's just a thought -- I'm far from the point of writing/testing this out.  hope it helps.

the nigga

unread,
Nov 27, 2011, 5:08:26 AM11/27/11
to H2 Database
Hi Igal, thanks.
Looks like you I am new h2 database...I tried to do as you said but
does not work.
I tried this.
public static void main(String[] args){     String sql = "BACKUP TO /
MyApp/backup/myapp.zip"     Statement stm =
MyConnection.getConnection();     stm.executeUpdate(sql);}
Error:Nov 27, 2011 7:07:45 AM bd.Conexao mainGrave:
nullorg.h2.jdbc.JdbcSQLException: Syntax error in SQL statement
"BACKUP TO/[*]MYAPP/BACKUP/MYAPP.ZIP"; expected "NOT, EXISTS, SELECT,
FROM"; SQL statement:BACKUP TO /MYAPP/backup/MYAPP.zip [42001-161]
thanks

the nigga

unread,
Nov 27, 2011, 7:55:00 AM11/27/11
to H2 Database
There any way restore online backup ??? The backup create myapp.zip
and how to restore online ?

thanks.


On 27 nov, 01:16, Igal <d...@21solutions.net> wrote:

Ryan How

unread,
Nov 27, 2011, 8:59:32 AM11/27/11
to h2-da...@googlegroups.com
I don't think H2 has an inbuilt method to restore it... well not that I
know of. I've just used the java zipinputstream and unzipped it to where
I need it then open it with H2.

Igal

unread,
Nov 27, 2011, 2:14:33 PM11/27/11
to h2-da...@googlegroups.com
yea, after I posted the message I realized I forgot the 'single quotes' ;)

glad it worked.

to restore you should probably use the utility that you tried to use initially, since there's no database "online / in use" issue with a Restore.

good luck :)

Thomas Mueller

unread,
Nov 28, 2011, 2:16:49 AM11/28/11
to h2-da...@googlegroups.com
Hi,

I am trying this

String[] bkp = {"-url", "jdbc:h2:tcp://localhost:9001/db/mydb", "-
user", "sa", "-password","123", "-script", "/MyApp/backup/myapp.zip"};
org.h2.tools.Script.main(bkp);

Generate the file.zip but when I am trying extract using WinRar or
other return one error and not unzip the file. The error is file is
corrupted.

Any idea ???

As documented, the Script tool creates a text file that contains the SQL statements. It doesn't create a zip file by default.

To create a zip file (that contains a text file with the SQL statements), use:

String[] bkp = {"-url", "jdbc:h2:tcp://localhost:9001/db/mydb", "-
user", "sa", "-password","123", "-script", "/MyApp/backup/myapp.zip", "-options", "compression", "zip"};

Regards,
Thomas

Thomas Mueller

unread,
Nov 28, 2011, 2:17:15 AM11/28/11
to h2-da...@googlegroups.com
> I don't think H2 has an inbuilt method to restore it...

There is a Restore tool.

the nigga

unread,
Nov 27, 2011, 5:33:01 AM11/27/11
to H2 Database
Wow....BINGO.

BACKUP TO 'myapp.zip'

100% work

thanks Igal.


On 27 nov, 01:16, Igal <d...@21solutions.net> wrote:

Reply all
Reply to author
Forward
0 new messages