Last active
December 27, 2022 16:49
-
-
Save mkgl/4c7b99426393f19843b8e36dd784293e to your computer and use it in GitHub Desktop.
Backup and Restore H2 for Magnolia/Jackrabbit workspaces and version DBs. See also https://h2database.com/html/tutorial.html#upgrade_backup_restore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
/** | |
* Run me from your magnolia home. | |
*/ | |
def repositories = new File('./repositories').canonicalFile | |
def workspaces = repositories.toPath().resolve('magnolia/workspaces').toFile() | |
def dbsToBackup = workspaces.list().collectEntries({ | |
[(it): "jdbc:h2:${workspaces.toPath().resolve(it).toFile().absolutePath}/db"] | |
}) | |
// include jackrabbit version DB too | |
dbsToBackup.put('version', "jdbc:h2:${repositories.toPath().resolve('magnolia/version').toFile().absolutePath}/db") | |
dbsToBackup.each { | |
// comment loop and uncomment below to test on single db | |
// def it = [ key: 'website', value: workspaces.toPath().resolve("website").toFile() ] | |
String db = it.key | |
String jdbcUrl = it.value | |
def script = repositories.toPath().resolve("h2-backup-${db}.zip") | |
def backupCmd = "java -cp WEB-INF/lib/h2-1.4.200.jar org.h2.tools.Script -url $jdbcUrl -script $script -options compression zip" | |
println "Will backup db: " + db.padRight(16) + "[ $jdbcUrl ]" | |
def proc = backupCmd.execute() | |
proc.waitForProcessOutput(System.out, System.err) | |
def size = script.toFile().size() | |
println "Backed up db successfully to $script (${size}KB)" | |
if (size < 200L) { | |
println "[WARNING]\tBackup script for db $db is under 200KB and might be empty. Check if this is expected or if the JDBC url is correct." | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
/** | |
* Run me from your magnolia home, after upgrading Magnolia/H2 and running the H2BackupAllDbs script. | |
*/ | |
def repositories = new File('./repositories').canonicalFile | |
def workspaces = repositories.toPath().resolve('magnolia/workspaces').toFile() | |
def dbsToRestore = workspaces.list().collectEntries({ | |
[(it): "jdbc:h2:${workspaces.toPath().resolve(it).toFile().absolutePath}/db"] | |
}) | |
// include jackrabbit version DB too | |
dbsToRestore.put('version', "jdbc:h2:${repositories.toPath().resolve('magnolia/version').toFile().absolutePath}/db") | |
//print dbsToBackup | |
dbsToRestore.each { | |
// comment loop and uncomment below to test on single db | |
// def it = [ key: 'website', value: workspaces.toPath().resolve("website").toFile() ] | |
String db = it.key | |
String jdbcUrl = it.value | |
def dbFile = new File("${jdbcUrl.takeAfter('jdbc:h2:')}.mv.db") | |
println "Will delete previous db file: $dbFile" | |
def deleted = dbFile.delete() | |
if (!deleted) { | |
println "[WARNING]\tCouldn't delete $dbFile prior to recreating db." | |
} | |
def script = repositories.toPath().resolve("h2-backup-${db}.zip") | |
def restoreCmd = "java -cp WEB-INF/lib/h2-2.1.214.jar org.h2.tools.RunScript -url $jdbcUrl -script $script -options compression zip variable_binary" | |
println "Will restore db: " + db.padRight(16) + "[ $jdbcUrl ] from $script" | |
def proc = restoreCmd.execute() | |
proc.waitForProcessOutput(System.out, System.err) | |
println "Restored db successfully: $db." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment