Package org.h2.tools
Class DirectRecover
java.lang.Object
org.h2.util.Tool
org.h2.tools.Recover
org.h2.tools.DirectRecover
- All Implemented Interfaces:
DataHandler
Enhanced database recovery tool with streaming and compression support.
DirectRecover extends the original Recover class to provide:
- Streaming Processing: Uses pipes for memory-efficient processing of large databases
- Parallel Execution: Dump generation and SQL writing happen concurrently
- Compression Support: On-the-fly compression with GZIP, ZIP, BZIP2, or KANZI
- No Intermediate Files: Direct streaming prevents disk space issues
Usage Examples:
Basic Recovery (No Compression):
java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb
With Built-in Compression:
# GZIP compression (recommended for most cases) java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress gzip # ZIP compression (widely compatible) java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress zip
With Optional External Libraries:
# BZIP2 compression (requires Apache Commons Compress) java -cp "h2.jar:commons-compress.jar" org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress bzip2 # KANZI compression (requires Kanzi library, best compression) java -cp "h2.jar:kanzi.jar" org.h2.tools.DirectRecover -dir /path/to/db -db mydb -compress kanzi
Debug and Troubleshooting:
# Enable verbose debug output java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -db mydb -trace -compress gzip # Process all databases in directory java -cp h2.jar org.h2.tools.DirectRecover -dir /path/to/db -compress gzip
Command Line Options:
| Option | Description | Default |
|---|---|---|
| -dir <directory> | Database directory | . (current directory) |
| -db <database> | Database name (without .mv.db extension) | All databases in directory |
| -compress <type> | Compression: none, gzip, zip, bzip2, kanzi | none |
| -trace | Enable debug output and verbose logging | false |
| -help or -? | Show help information | - |
Output Files:
- No compression: database.sql
- GZIP: database.sql.gz
- ZIP: database.sql.zip
- BZIP2: database.sql.bz2
- KANZI: database.sql.knz
External Dependencies (Optional):
- Apache Commons Compress: Required for BZIP2 compression
Download: https://commons.apache.org/proper/commons-compress/ - Kanzi: Required for KANZI compression (best compression ratio)
Download: https://github.com/flanglet/kanzi-java
Performance Notes:
- Streaming: Memory usage is bounded (~256KB) regardless of database size
- Parallel: Dump generation and SQL writing happen simultaneously
- Compression: Applied on-the-fly, no temporary uncompressed files
- Large Databases: Designed to handle multi-GB databases efficiently
Error Handling:
- Missing compression libraries fall back to uncompressed output
- Corrupted databases are processed in recovery mode
- Timeouts prevent indefinite hanging
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidexecute(String dir, String db, PrintWriter writer) Public method to process database files and write to a provided writer (pipe-like).Get a list of available compression types based on classpath.Gets the current compression type.booleanCheck if a compression library is available without loading it.static voidOptions are case-sensitive.voidEnhanced runTool method with compression support.voidSets the compression type for SQL output files.Methods inherited from class org.h2.tools.Recover
checkPowerOff, checkWritingAllowed, execute, getCompareMode, getDatabasePath, getLobFileListCache, getLobStorage, getLobSyncObject, getMaxLengthInplaceLob, getTempFileDeleter, openFile, readBlobMap, readClobMap, readLobMethods inherited from class org.h2.util.Tool
getMainClassName, isOption, printNoDatabaseFilesFound, setOut, showUsage, showUsageAndThrowUnsupportedOption, throwUnsupportedOption
-
Constructor Details
-
DirectRecover
public DirectRecover()
-
-
Method Details
-
main
Options are case-sensitive.Supported options (in addition to base Recover options) [-compress <type>] Compress SQL output (none, gzip, zip, bzip2, kanzi, default: none) [-trace] Enable verbose debug output - Parameters:
args- the command line arguments- Throws:
SQLException- on failure
-
runTool
Enhanced runTool method with compression support.- Overrides:
runToolin classRecover- Parameters:
args- the command line arguments- Throws:
SQLException- on failure
-
isCompressionAvailable
Check if a compression library is available without loading it.- Parameters:
type- the compression type to check- Returns:
- true if the library is available, false otherwise
-
getAvailableCompressionTypes
Get a list of available compression types based on classpath.- Returns:
- array of available compression types
-
setCompressionType
Sets the compression type for SQL output files.- Parameters:
type- the compression type to use
-
getCompressionType
Gets the current compression type.- Returns:
- the current compression type
-
execute
Public method to process database files and write to a provided writer (pipe-like).- Parameters:
dir- the directorydb- the database name (null for all databases)writer- the output writer- Throws:
SQLException- on failure
-