Skip to content

Instantly share code, notes, and snippets.

@mathias-goebel
Last active October 20, 2017 08:35
Show Gist options
  • Save mathias-goebel/b1b6b1dabf41cce87a7836df8c905f90 to your computer and use it in GitHub Desktop.
Save mathias-goebel/b1b6b1dabf41cce87a7836df8c905f90 to your computer and use it in GitHub Desktop.
eXist-db restoring from XQuery
xquery version "3.1";
declare function local:do($path-external as xs:string, $path-internal as xs:string) {
let $list := file:list( $path-external )
return (
for $dir in $list/file:directory
let $name := replace(string( $dir/@name ), "\s", "_")
return(
xmldb:create-collection($path-internal, $name),
local:do($path-external || "/" || $name, $path-internal || "/" || $name)
),
for $file in $list/file:file
let $name := replace( string( $file/@name ), "\s", "_")
let $data := try{ parse-xml( file:read( $path-external || "/" || $name ) ) }
catch * {<error>Caught error {$err:code}: {$err:description}</error>}
let $log := util:log-system-out("processing "||$name)
let $error-log := if( local-name($data) = "error") then util:log-system-out("ERROR in"||$name || ":" || $data//error/text()) else ()
where $name != "__contents__.xml"
return
xmldb:store($path-internal, $name, $data)
)};
let $path-external := "/home/mgoebel/eXist-2/webapp/WEB-INF/data/export/full20171019-1144/db/data"
let $path-internal := "/db/data"
return(
util:log-system-out("Restoring starts"),
xmldb:remove( $path-internal ),
xmldb:create-collection( string-join( (tokenize($path-internal, "/")[position() != last()]), "/" ), tokenize($path-internal, "/")[last()] ),
local:do( $path-external, $path-internal ),
util:log-system-out("Restoring done.")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment