KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > Backup


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools;
6
7 import java.io.BufferedWriter JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.PrintWriter JavaDoc;
10 import java.sql.Connection JavaDoc;
11 import java.sql.DriverManager JavaDoc;
12 import java.sql.ResultSet JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import java.sql.Statement JavaDoc;
15
16 import org.h2.message.Message;
17 import org.h2.util.IOUtils;
18 import org.h2.util.JdbcUtils;
19 import org.h2.util.StringUtils;
20
21 /**
22  * Creates a backup of a database by extracting the schema and data into a SQL script file.
23  *
24  * @author Thomas
25  *
26  */

27 public class Backup {
28
29     private void showUsage() {
30         System.out.println("java "+getClass().getName()
31                 + " -url <url> -user <user> [-password <pwd>] [-script <file>] [-options <option> ...]");
32     }
33
34     /**
35      * The command line interface for this tool.
36      * The options must be split into strings like this: "-user", "sa",...
37      * The following options are supported:
38      * <ul>
39      * <li>-help or -? (print the list of options)
40      * <li>-url jdbc:h2:... (database URL)
41      * <li>-user username
42      * <li>-password password
43      * <li>-script filename (default file name is backup.sql)
44      * <li>-options to specify a list of options (only for H2)
45      * </ul>
46      *
47      * @param args the command line arguments
48      * @throws SQLException
49      */

50     public static void main(String JavaDoc[] args) throws SQLException JavaDoc {
51         new Backup().run(args);
52     }
53
54     private void run(String JavaDoc[] args) throws SQLException JavaDoc {
55         String JavaDoc url = null;
56         String JavaDoc user = null;
57         String JavaDoc password = "";
58         String JavaDoc script = "backup.sql";
59         String JavaDoc options1 = null, options2 = null;
60         for(int i=0; args != null && i<args.length; i++) {
61             if(args[i].equals("-url")) {
62                 url = args[++i];
63             } else if(args[i].equals("-user")) {
64                 user = args[++i];
65             } else if(args[i].equals("-password")) {
66                 password = args[++i];
67             } else if(args[i].equals("-script")) {
68                 script = args[++i];
69             } else if(args[i].equals("-options")) {
70                 StringBuffer JavaDoc buff1 = new StringBuffer JavaDoc();
71                 StringBuffer JavaDoc buff2 = new StringBuffer JavaDoc();
72                 i++;
73                 for(; i<args.length; i++) {
74                     String JavaDoc a = args[i];
75                     String JavaDoc upper = StringUtils.toUpperEnglish(a);
76                     if(upper.startsWith("NO") || upper.equals("DROP")) {
77                         buff1.append(' ');
78                         buff1.append(args[i]);
79                     } else {
80                         buff2.append(' ');
81                         buff2.append(args[i]);
82                     }
83                 }
84                 options1 = buff1.toString();
85                 options2 = buff2.toString();
86             } else {
87                 showUsage();
88                 return;
89             }
90         }
91         if(url==null || user==null || script == null) {
92             showUsage();
93             return;
94         }
95         if(options1 != null) {
96             executeScript(url, user, password, script, options1, options2);
97         } else {
98             execute(url, user, password, script);
99         }
100     }
101     
102     /**
103      * INTERNAL
104      */

105     public static void executeScript(String JavaDoc url, String JavaDoc user, String JavaDoc password, String JavaDoc fileName, String JavaDoc options1, String JavaDoc options2) throws SQLException JavaDoc {
106         Connection JavaDoc conn = null;
107         Statement JavaDoc stat = null;
108         try {
109             org.h2.Driver.load();
110             conn = DriverManager.getConnection(url, user, password);
111             stat = conn.createStatement();
112             String JavaDoc sql = "SCRIPT " + options1 + " TO '" + fileName + "' " + options2;
113             stat.execute(sql);
114         } catch(Exception JavaDoc e) {
115             throw Message.convert(e);
116         } finally {
117             JdbcUtils.closeSilently(stat);
118             JdbcUtils.closeSilently(conn);
119         }
120     }
121     
122     /**
123      * Backs up a database to a file.
124      *
125      * @param url the database URL
126      * @param user the user name
127      * @param password the password
128      * @param script the script file
129      * @throws SQLException
130      */

131     public static void execute(String JavaDoc url, String JavaDoc user, String JavaDoc password, String JavaDoc script) throws SQLException JavaDoc {
132         Connection JavaDoc conn = null;
133         Statement JavaDoc stat = null;
134         FileWriter JavaDoc fileWriter = null;
135         try {
136             org.h2.Driver.load();
137             conn = DriverManager.getConnection(url, user, password);
138             stat = conn.createStatement();
139             fileWriter = new FileWriter JavaDoc(script);
140             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(fileWriter));
141             ResultSet JavaDoc rs = stat.executeQuery("SCRIPT");
142             while(rs.next()) {
143                 String JavaDoc s = rs.getString(1);
144                 writer.println(s + ";");
145             }
146             writer.close();
147         } catch(Exception JavaDoc e) {
148             throw Message.convert(e);
149         } finally {
150             JdbcUtils.closeSilently(stat);
151             JdbcUtils.closeSilently(conn);
152             IOUtils.closeSilently(fileWriter);
153         }
154     }
155
156 }
157
158
Popular Tags