| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.tools; |
| 7 |
|
| 8 |
import java.io.File; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.InputStream; |
| 11 |
import java.io.OutputStream; |
| 12 |
import java.sql.SQLException; |
| 13 |
import java.util.zip.ZipEntry; |
| 14 |
import java.util.zip.ZipInputStream; |
| 15 |
import org.h2.engine.Constants; |
| 16 |
import org.h2.message.DbException; |
| 17 |
import org.h2.store.fs.FileUtils; |
| 18 |
import org.h2.util.IOUtils; |
| 19 |
import org.h2.util.Tool; |
| 20 |
|
| 21 |
/** |
| 22 |
* Restores a H2 database by extracting the database files from a .zip file. |
| 23 |
*/ |
| 24 |
public class Restore extends Tool { |
| 25 |
|
| 26 |
/** |
| 27 |
* Options are case sensitive. Supported options |
| 28 |
* <table> |
| 29 |
* <caption>Supported options</caption> |
| 30 |
* <tr><td>[-help] or [-?]</td> |
| 31 |
* <td>Print the list of options</td></tr> |
| 32 |
* <tr><td>[-file <filename>]</td> |
| 33 |
* <td>The source file name (default: backup.zip)</td></tr> |
| 34 |
* <tr><td>[-dir <dir>]</td> |
| 35 |
* <td>The target directory (default: .)</td></tr> |
| 36 |
* <tr><td>[-db <database>]</td> |
| 37 |
* <td>The target database name (as stored if not set)</td></tr> |
| 38 |
* <tr><td>[-quiet]</td> |
| 39 |
* <td>Do not print progress information</td></tr> |
| 40 |
* </table> |
| 41 |
* |
| 42 |
* @param args the command line arguments |
| 43 |
* @throws SQLException on failure |
| 44 |
*/ |
| 45 |
public static void main(String... args) throws SQLException { |
| 46 |
new Restore().runTool(args); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void runTool(String... args) throws SQLException { |
| 51 |
String zipFileName = "backup.zip"; |
| 52 |
String dir = "."; |
| 53 |
String db = null; |
| 54 |
for (int i = 0; args != null && i < args.length; i++) { |
| 55 |
String arg = args[i]; |
| 56 |
if (arg.equals("-dir")) { |
| 57 |
dir = args[++i]; |
| 58 |
} else if (arg.equals("-file")) { |
| 59 |
zipFileName = args[++i]; |
| 60 |
} else if (arg.equals("-db")) { |
| 61 |
db = args[++i]; |
| 62 |
} else if (arg.equals("-quiet")) { |
| 63 |
// ignore |
| 64 |
} else if (arg.equals("-help") || arg.equals("-?")) { |
| 65 |
showUsage(); |
| 66 |
return; |
| 67 |
} else { |
| 68 |
showUsageAndThrowUnsupportedOption(arg); |
| 69 |
} |
| 70 |
} |
| 71 |
execute(zipFileName, dir, db); |
| 72 |
} |
| 73 |
|
| 74 |
private static String getOriginalDbName(String fileName, String db) |
| 75 |
throws IOException { |
| 76 |
|
| 77 |
try (InputStream in = FileUtils.newInputStream(fileName)) { |
| 78 |
ZipInputStream zipIn = new ZipInputStream(in); |
| 79 |
String originalDbName = null; |
| 80 |
boolean multiple = false; |
| 81 |
while (true) { |
| 82 |
ZipEntry entry = zipIn.getNextEntry(); |
| 83 |
if (entry == null) { |
| 84 |
break; |
| 85 |
} |
| 86 |
String entryName = entry.getName(); |
| 87 |
zipIn.closeEntry(); |
| 88 |
String name = getDatabaseNameFromFileName(entryName); |
| 89 |
if (name != null) { |
| 90 |
if (db.equals(name)) { |
| 91 |
originalDbName = name; |
| 92 |
// we found the correct database |
| 93 |
break; |
| 94 |
} else if (originalDbName == null) { |
| 95 |
originalDbName = name; |
| 96 |
// we found a database, but maybe another one |
| 97 |
} else { |
| 98 |
// we have found multiple databases, but not the correct |
| 99 |
// one |
| 100 |
multiple = true; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
zipIn.close(); |
| 105 |
if (multiple && !db.equals(originalDbName)) { |
| 106 |
throw new IOException("Multiple databases found, but not " + db); |
| 107 |
} |
| 108 |
return originalDbName; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Extract the name of the database from a given file name. |
| 114 |
* Only files ending with .h2.db are considered, all others return null. |
| 115 |
* |
| 116 |
* @param fileName the file name (without directory) |
| 117 |
* @return the database name or null |
| 118 |
*/ |
| 119 |
private static String getDatabaseNameFromFileName(String fileName) { |
| 120 |
if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) { |
| 121 |
return fileName.substring(0, |
| 122 |
fileName.length() - Constants.SUFFIX_MV_FILE.length()); |
| 123 |
} |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Restores database files. |
| 129 |
* |
| 130 |
* @param zipFileName the name of the backup file |
| 131 |
* @param directory the directory name |
| 132 |
* @param db the database name (null for all databases) |
| 133 |
* @throws DbException if there is an IOException |
| 134 |
*/ |
| 135 |
public static void execute(String zipFileName, String directory, String db) { |
| 136 |
InputStream in = null; |
| 137 |
try { |
| 138 |
if (!FileUtils.exists(zipFileName)) { |
| 139 |
throw new IOException("File not found: " + zipFileName); |
| 140 |
} |
| 141 |
String originalDbName = null; |
| 142 |
int originalDbLen = 0; |
| 143 |
if (db != null) { |
| 144 |
originalDbName = getOriginalDbName(zipFileName, db); |
| 145 |
if (originalDbName == null) { |
| 146 |
throw new IOException("No database named " + db + " found"); |
| 147 |
} |
| 148 |
if (originalDbName.startsWith(File.separator)) { |
| 149 |
originalDbName = originalDbName.substring(1); |
| 150 |
} |
| 151 |
originalDbLen = originalDbName.length(); |
| 152 |
} |
| 153 |
in = FileUtils.newInputStream(zipFileName); |
| 154 |
try (ZipInputStream zipIn = new ZipInputStream(in)) { |
| 155 |
while (true) { |
| 156 |
ZipEntry entry = zipIn.getNextEntry(); |
| 157 |
if (entry == null) { |
| 158 |
break; |
| 159 |
} |
| 160 |
String fileName = entry.getName(); |
| 161 |
// restoring windows backups on linux and vice versa |
| 162 |
fileName = IOUtils.nameSeparatorsToNative(fileName); |
| 163 |
if (fileName.startsWith(File.separator)) { |
| 164 |
fileName = fileName.substring(1); |
| 165 |
} |
| 166 |
boolean copy = false; |
| 167 |
if (db == null) { |
| 168 |
copy = true; |
| 169 |
} else if (fileName.startsWith(originalDbName + ".")) { |
| 170 |
fileName = db + fileName.substring(originalDbLen); |
| 171 |
copy = true; |
| 172 |
} |
| 173 |
if (copy) { |
| 174 |
OutputStream o = null; |
| 175 |
try { |
| 176 |
o = FileUtils.newOutputStream(directory + File.separatorChar + fileName, false); |
| 177 |
IOUtils.copy(zipIn, o); |
| 178 |
o.close(); |
| 179 |
} finally { |
| 180 |
IOUtils.closeSilently(o); |
| 181 |
} |
| 182 |
} |
| 183 |
zipIn.closeEntry(); |
| 184 |
} |
| 185 |
zipIn.closeEntry(); |
| 186 |
} |
| 187 |
} catch (IOException e) { |
| 188 |
throw DbException.convertIOException(e, zipFileName); |
| 189 |
} finally { |
| 190 |
IOUtils.closeSilently(in); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} |