KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > backupTool > dbDump > MysqlDbDumper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.outerj.daisy.backupTool.dbDump;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 public class MysqlDbDumper extends AbstractDbDumper {
24
25     private static String JavaDoc dumpExecutableName = "mysqldump";
26
27     private static String JavaDoc restoreExecutableName = "mysql";
28
29     public MysqlDbDumper(String JavaDoc dbName, String JavaDoc host, Integer JavaDoc port, String JavaDoc password, String JavaDoc username) {
30         super(dbName, host, port, password, username);
31     }
32
33     public void dump(File JavaDoc dumpFile) throws Exception JavaDoc {
34         try {
35             String JavaDoc command = dumpExecutableName + " --single-transaction" + " --databases " + " --user=" + this.username + " --password=" + this.password
36                     + " --host=" + this.host + " --result-file=" + dumpFile.getPath() + (port.intValue() > 0 ? " --port=" + port : "") + " " + this.dbName;
37             Process JavaDoc dumpProcess = Runtime.getRuntime().exec(command);
38
39             handleRuntimeProcess(dumpProcess, null, System.out);
40
41         } catch (IOException JavaDoc e) {
42             throw new Exception JavaDoc("The " + dumpExecutableName + " command was not found. Try putting this executable in your environments path variable.");
43         }
44     }
45
46     public void restore(File JavaDoc dumpFile) throws Exception JavaDoc {
47         int returnCode;
48
49         String JavaDoc dropCommand = "mysqladmin -f --user=" + username + " --password=" + password + " -h " + host + (port.intValue() > 0 ? " --port=" + port : "") + " drop " + dbName;
50         System.out.println("Running " + dropCommand);
51         Process JavaDoc dropProcess = Runtime.getRuntime().exec(dropCommand);
52         returnCode = dropProcess.waitFor();
53         if (returnCode != 0)
54             throw new Exception JavaDoc("Database drop command return with non-zero code: " + returnCode);
55
56         String JavaDoc createCommand = "mysqladmin -f --user=" + username + " --password=" + password + " -h " + host + (port.intValue() > 0 ? " --port=" + port : "") + " create " + dbName;
57         System.out.println("Running " + createCommand);
58         Process JavaDoc createProcess = Runtime.getRuntime().exec(createCommand);
59         returnCode = createProcess.waitFor();
60         if (returnCode != 0)
61             throw new Exception JavaDoc("Database create command return with non-zero code: " + returnCode);
62
63         String JavaDoc command = restoreExecutableName + " --user=" + this.username + " --password=" + this.password + " --host=" + this.host
64                 + (port.intValue() > 0 ? " --port=" + port : "") + " " + this.dbName;
65
66         System.out.println("Running " + command);
67         System.out.println("and piping database script into it.");
68         Process JavaDoc restoreProcess = Runtime.getRuntime().exec(command);
69
70         InputStream JavaDoc fis = null;
71
72         try {
73             fis = new FileInputStream JavaDoc(dumpFile);
74             handleRuntimeProcess(restoreProcess, fis, System.out);
75         } finally {
76             if (fis != null)
77                 fis.close();
78         }
79     }
80 }
Popular Tags