提交 1571a992 authored 作者: Thomas Mueller's avatar Thomas Mueller

The Backup tool now supports compressing all files in a given directory.

上级 06e29708
...@@ -8,6 +8,7 @@ package org.h2.store; ...@@ -8,6 +8,7 @@ package org.h2.store;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.DbException; import org.h2.message.DbException;
...@@ -46,7 +47,7 @@ public class FileLister { ...@@ -46,7 +47,7 @@ public class FileLister {
* @param message the text to include in the error message * @param message the text to include in the error message
* @throws SQLException if it failed * @throws SQLException if it failed
*/ */
public static void tryUnlockDatabase(ArrayList<String> files, String message) throws SQLException { public static void tryUnlockDatabase(List<String> files, String message) throws SQLException {
for (String fileName : files) { for (String fileName : files) {
if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) { if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) {
FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP); FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP);
......
...@@ -11,13 +11,15 @@ import java.io.IOException; ...@@ -11,13 +11,15 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.h2.command.dml.BackupCommand; import org.h2.command.dml.BackupCommand;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.store.FileLister; import org.h2.store.FileLister;
import org.h2.store.fs.FileSystem;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.Tool; import org.h2.util.Tool;
...@@ -91,7 +93,8 @@ public class Backup extends Tool { ...@@ -91,7 +93,8 @@ public class Backup extends Tool {
* *
* @param zipFileName the name of the target backup file (including path) * @param zipFileName the name of the target backup file (including path)
* @param directory the source directory name * @param directory the source directory name
* @param db the source database name (null if there is only one database) * @param db the source database name (null if there is only one database, and
* and empty string to backup all files in this directory)
* @param quiet don't print progress information * @param quiet don't print progress information
* @throws SQLException * @throws SQLException
*/ */
...@@ -104,7 +107,13 @@ public class Backup extends Tool { ...@@ -104,7 +107,13 @@ public class Backup extends Tool {
} }
private void process(String zipFileName, String directory, String db, boolean quiet) throws SQLException { private void process(String zipFileName, String directory, String db, boolean quiet) throws SQLException {
ArrayList<String> list = FileLister.getDatabaseFiles(directory, db, true); List<String> list;
boolean allFiles = db != null && db.length() == 0;
if (allFiles) {
list = Arrays.asList(FileSystem.getInstance(directory).listFiles(directory));
} else {
list = FileLister.getDatabaseFiles(directory, db, true);
}
if (list.size() == 0) { if (list.size() == 0) {
if (!quiet) { if (!quiet) {
printNoDatabaseFilesFound(directory, db); printNoDatabaseFilesFound(directory, db);
...@@ -124,7 +133,7 @@ public class Backup extends Tool { ...@@ -124,7 +133,7 @@ public class Backup extends Tool {
ZipOutputStream zipOut = new ZipOutputStream(fileOut); ZipOutputStream zipOut = new ZipOutputStream(fileOut);
String base = ""; String base = "";
for (String fileName : list) { for (String fileName : list) {
if (fileName.endsWith(Constants.SUFFIX_PAGE_FILE)) { if (fileName.endsWith(Constants.SUFFIX_PAGE_FILE) || allFiles) {
base = IOUtils.getParent(fileName); base = IOUtils.getParent(fileName);
break; break;
} }
...@@ -134,6 +143,9 @@ public class Backup extends Tool { ...@@ -134,6 +143,9 @@ public class Backup extends Tool {
if (!f.startsWith(base)) { if (!f.startsWith(base)) {
DbException.throwInternalError(f + " does not start with " + base); DbException.throwInternalError(f + " does not start with " + base);
} }
if (f.endsWith(zipFileName)) {
continue;
}
if (IOUtils.isDirectory(fileName)) { if (IOUtils.isDirectory(fileName)) {
continue; continue;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论