File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\tools\Backup.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.tools;
8
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import java.sql.SQLException;
14 import java.util.List;
15 import java.util.zip.ZipEntry;
16 import java.util.zip.ZipOutputStream;
17 import org.h2.command.dml.BackupCommand;
18 import org.h2.engine.Constants;
19 import org.h2.message.DbException;
20 import org.h2.store.FileLister;
21 import org.h2.store.fs.FileUtils;
22 import org.h2.util.IOUtils;
23 import org.h2.util.Tool;
24
25 /**
26 * Creates a backup of a database.
27 * <br />
28 * This tool copies all database files. The database must be closed before using
29 * this tool. To create a backup while the database is in use, run the BACKUP
30 * SQL statement. In an emergency, for example if the application is not
31 * responding, creating a backup using the Backup tool is possible by using the
32 * quiet mode. However, if the database is changed while the backup is running
33 * in quiet mode, the backup could be corrupt.
34 *
35 * @h2.resource
36 */
37 public class Backup extends Tool {
38
39 /**
40 * Options are case sensitive. Supported options are:
41 * <table>
42 * <tr><td>[-help] or [-?]</td>
43 * <td>Print the list of options</td></tr>
44 * <tr><td>[-file &lt;filename&gt;]</td>
45 * <td>The target file name (default: backup.zip)</td></tr>
46 * <tr><td>[-dir &lt;dir&gt;]</td>
47 * <td>The source directory (default: .)</td></tr>
48 * <tr><td>[-db &lt;database&gt;]</td>
49 * <td>Source database; not required if there is only one</td></tr>
50 * <tr><td>[-quiet]</td>
51 * <td>Do not print progress information</td></tr>
52 * </table>
53 * @h2.resource
54 *
55 * @param args the command line arguments
56 */
57 public static void main(String... args) throws SQLException {
58 new Backup().runTool(args);
59 }
60
61 public void runTool(String... args) throws SQLException {
62 String zipFileName = "backup.zip";
63 String dir = ".";
64 String db = null;
65 boolean quiet = false;
66 for (int i = 0; args != null && i < args.length; i++) {
67 String arg = args[i];
68 if (arg.equals("-dir")) {
69 dir = args[++i];
70 } else if (arg.equals("-db")) {
71 db = args[++i];
72 } else if (arg.equals("-quiet")) {
73 quiet = true;
74 } else if (arg.equals("-file")) {
75 zipFileName = args[++i];
76 } else if (arg.equals("-help") || arg.equals("-?")) {
77 showUsage();
78 return;
79 } else {
80 showUsageAndThrowUnsupportedOption(arg);
81 }
82 }
83 try {
84 process(zipFileName, dir, db, quiet);
85 } catch (Exception e) {
86 throw DbException.toSQLException(e);
87 }
88 }
89
90 /**
91 * Backs up database files.
92 *
93 * @param zipFileName the name of the target backup file (including path)
94 * @param directory the source directory name
95 * @param db the source database name (null if there is only one database, and
96 * and empty string to backup all files in this directory)
97 * @param quiet don't print progress information
98 */
99 public static void execute(String zipFileName, String directory, String db, boolean quiet) throws SQLException {
100 try {
101 new Backup().process(zipFileName, directory, db, quiet);
102 } catch (Exception e) {
103 throw DbException.toSQLException(e);
104 }
105 }
106
107 private void process(String zipFileName, String directory, String db, boolean quiet) throws SQLException {
108 List<String> list;
109 boolean allFiles = db != null && db.length() == 0;
110 if (allFiles) {
111 list = FileUtils.newDirectoryStream(directory);
112 } else {
113 list = FileLister.getDatabaseFiles(directory, db, true);
114 }
115 if (list.size() == 0) {
116 if (!quiet) {
117 printNoDatabaseFilesFound(directory, db);
118 }
119 return;
120 }
121 if (!quiet) {
122 FileLister.tryUnlockDatabase(list, "backup");
123 }
124 zipFileName = FileUtils.toRealPath(zipFileName);
125 FileUtils.delete(zipFileName);
126 OutputStream fileOut = null;
127 try {
128 fileOut = FileUtils.newOutputStream(zipFileName, false);
129 ZipOutputStream zipOut = new ZipOutputStream(fileOut);
130 String base = "";
131 for (String fileName : list) {
132 if (fileName.endsWith(Constants.SUFFIX_PAGE_FILE) || allFiles) {
133 base = FileUtils.getParent(fileName);
134 break;
135 }
136 }
137 for (String fileName : list) {
138 String f = FileUtils.toRealPath(fileName);
139 if (!f.startsWith(base)) {
140 DbException.throwInternalError(f + " does not start with " + base);
141 }
142 if (f.endsWith(zipFileName)) {
143 continue;
144 }
145 if (FileUtils.isDirectory(fileName)) {
146 continue;
147 }
148 f = f.substring(base.length());
149 f = BackupCommand.correctFileName(f);
150 ZipEntry entry = new ZipEntry(f);
151 zipOut.putNextEntry(entry);
152 InputStream in = null;
153 try {
154 in = FileUtils.newInputStream(fileName);
155 IOUtils.copyAndCloseInput(in, zipOut);
156 } catch (FileNotFoundException e) {
157 // the file could have been deleted in the meantime
158 // ignore this (in this case an empty file is created)
159 } finally {
160 IOUtils.closeSilently(in);
161 }
162 zipOut.closeEntry();
163 if (!quiet) {
164 out.println("Processed: " + fileName);
165 }
166 }
167 zipOut.closeEntry();
168 zipOut.close();
169 } catch (IOException e) {
170 throw DbException.convertIOException(e, zipFileName);
171 } finally {
172 IOUtils.closeSilently(fileOut);
173 }
174 }
175
176 }
177