View Javadoc

1   /* $Id: TestDump.java,v 1.17 2006/01/10 18:02:36 psionides Exp $ */
2   package net.sourceforge.jdbdump.connect;
3   
4   import java.io.File;
5   import java.io.FileInputStream;
6   import java.io.IOException;
7   import java.util.Properties;
8   
9   import net.sourceforge.jdbdump.dump.BinaryFileManager;
10  import net.sourceforge.jdbdump.dump.Column;
11  import net.sourceforge.jdbdump.dump.Constraint;
12  import net.sourceforge.jdbdump.dump.Dump;
13  import net.sourceforge.jdbdump.dump.DumpFileManager;
14  import net.sourceforge.jdbdump.dump.Table;
15  
16  import org.apache.log4j.Logger;
17  
18  /***
19   * A class which may be used to test the dump/restore engine from the command line.
20   * <br /><br />
21   * This is not a JUnit test, as the name could suggest. It's just a class with main() which I use
22   * to test dump creating, when I don't want to or can't use the web GUI.
23   * <br /><br />
24   * Usage: <br/>
25   * java TestDump dump|restore|print &lt;dumpfile&gt; &lt;propfile&gt;, where:
26   * <ul>
27   * <li>option dump creates the backup, restore uploads it back to the database,
28   *       and print just attempts to print some of the information from the file</li>
29   * <li>&lt;dumpfile&gt; is the dump file to be created or loaded</li>
30   * <li>&lt;propfile&gt; is a name of a property file in which database settings are stored</li>
31   * </ul>
32   * PropFile should be in classic properties format and should have such properties:
33   * <ul>
34   * <li>connector - connector class name (e.g.
35   *     net.sourceforge.jdbdump.connect.connectors.MysqlConnector)</li>
36   * <li>url - database url (e.g. jdbc:mysql://localhost/jsuder2)</li>
37   * <li>login</li>
38   * <li>password</li>
39   * <li>compression (zip, gzip or none)</li>
40   * </ul>
41   * Warning: if a dump from MySQL fails and throws an exception concerning an empty date (0000-00-00),
42   * add an option "?zeroDateTimeBehavior=convertToNull" to the end of the url.
43   * 
44   * @author jsuder
45   */
46  
47  public class TestDump {
48  
49  	/***
50  	 * Runs the command line tool with given arguments.
51  	 * @param args command line arguments
52  	 */
53  	
54  	public static void main(String[] args) {
55  		Logger logger = Logger.getLogger(TestDump.class);
56  		
57  		if (args.length != 3) {
58  			System.out.println("Use: java TestDump dump|restore|print <dumpfile> <propfile>, where:");
59  			System.out.println("- option dump creates the backup, restore uploads it back to the database, "
60  				+ "and print just attempts to print some of the information from the file");
61  			System.out.println("- <dumpfile> is the dump file to be created or loaded.");
62  			System.out.println("- <propfile> is a name of a property file in which database "
63  				+ "settings are stored");
64  			System.out.println("PropFile should be in classic properties format and should have properties:");
65  			System.out.println("- connector - connector class name (e.g. net.sourceforge.jdbdump.connect.connectors.MysqlConnector)");
66  			System.out.println("- url - database url (e.g. jdbc:mysql://localhost/jsuder2)");
67  			System.out.println("- login, password");
68  			System.out.println("- compression (zip, gzip or none)");
69  			System.out.println("Warning: if a dump from MySQL fails and throws an exception concerning "
70  				+ "an empty date (0000-00-00), add an option \"?zeroDateTimeBehavior=convertToNull\" "
71  				+ "to the end of the url.");
72  			return;
73  		}
74  		
75  		Properties props = new Properties();
76  		String option = args[0];
77  		String dumpFile = args[1];
78  		String propFile = args[2];
79  
80  		String url, login, password, conn, comp;
81  		try {
82  			props.load(new FileInputStream(new File(propFile)));
83  			url = props.getProperty("url"); 
84  			comp = props.getProperty("compression"); 
85  			conn = props.getProperty("connector"); 
86  			login = props.getProperty("login"); 
87  			password = props.getProperty("password");
88  			if (url == null) { 
89  				logger.error("Property 'url' is missing in file " + propFile + "...");
90  				return;
91  			} else if (comp == null) {
92  				logger.error("Property 'compression' is missing in file " + propFile + "...");
93  				return;
94  			} else if (conn == null) {
95  				logger.error("Property 'connector' is missing in file " + propFile + "...");
96  				return;
97  			} else if (login == null) {
98  				logger.error("Property 'login' is missing in file " + propFile + "...");
99  				return;
100 			} else if (password == null) {
101 				logger.error("Property 'password' is missing in file " + propFile + "...");
102 				return;
103 			}
104 		} catch (IOException e) {
105 			logger.error("Error reading properties from file '" + propFile + "'");
106 			return;
107 		}
108 
109 		DatabaseConnector connector = DatabaseConnectorFactory.getInstance().createConnector(conn);
110 		
111 		try {
112 			connector.connect(url, login, password);
113 			
114 			DumpFileManager saver = new BinaryFileManager();
115 			DumpFileManager.CompressionMethod cm;
116 
117 			if (comp.equals("zip")) {
118 				cm = DumpFileManager.CompressionMethod.ZIP;
119 			} else if (comp.equals("gzip")) {
120 				cm = DumpFileManager.CompressionMethod.GZIP;
121 			} else {
122 				cm = DumpFileManager.CompressionMethod.NONE;
123 			}
124 
125 			if (option.equals("dump")) {
126 				logger.info("Now creating dump...");
127 				Dump dump = connector.dump();
128 			
129 				logger.info("Saving dump...");
130 
131 				saver.exportDump(dump, new File(dumpFile), cm);
132 				logger.info("Dump saved to " + dumpFile + ".");
133 			} else {
134 				logger.info("Loading dump from " + dumpFile + "...");
135 				Dump dump = (Dump) saver.importDump(new File(dumpFile), cm);
136 			
137 				if (option.equals("print")) {
138 					logger.info("Contents of the dump:");
139 					for (Table table : dump.getTables()) {
140 						logger.info("Table '" + table.getName() + "':");
141 						for (Column column : table.getColumns()) {
142 							logger.info("- Column '" + column.getName() + "': ");
143 							logger.info("  > type: '" + column.getType() + "'");
144 							logger.info("  > length: " + column.getLength());
145 							logger.info("  > decimals: " + column.getDecimalDigits());
146 							logger.info("  > default: '" + column.getDefault() + "'");
147 							logger.info("  > nullable: " + column.getNullable());
148 							logger.info("  > nullable2: '" + column.getNullable2() + "'");
149 							logger.info("  > remarks: '" + column.getRemarks() + "'");
150 						}
151 						for (Constraint cons : table.getConstraints()) {
152 							logger.info("- Constraint '" + cons.getName() + "':");
153 							logger.info("  > type: '" + cons.getType() + "'");
154 							logger.info("  > text: " + cons.getText());
155 						}
156 						
157 						logger.info("- table data:");
158 						table.initializeData();
159 						while (true) {
160 							String[] line = table.getDataLine();
161 							if (line == null) break;
162 					
163 							String ln = "  > record: ";
164 							for (int i=0; i<line.length-1; i++) {
165 								ln = ln + "'" + line[i] + "', ";
166 							}
167 							ln = ln + "'" + line[line.length-1] + "'";
168 							logger.info(ln);
169 						}
170 					}
171 				} else if (option.equals("restore")) {
172 					logger.info("Now restoring...");
173 					connector.restore(dump);
174 				} else {
175 					logger.error("Wrong option: " + option);
176 				}
177 				dump.closeFileReader();
178 			}
179 			connector.disconnect();
180 			
181 		} catch (Exception e) {
182 			logger.error("An SQL Exception occured: " + e);
183 			e.printStackTrace();
184 		}
185 	}
186 }