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