From bf416e2c694bc81a50e894bb3fb8c377d1cccb36 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 4 Feb 2013 20:38:57 +0000 Subject: [PATCH] JCR repository backup git-svn-id: https://svn.argeo.org/slc/trunk@6070 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- dep/org.argeo.slc.dep.minimal/pom.xml | 15 +- lib/org.argeo.slc.lib.jcr/.project | 22 +++ .../.settings/org.eclipse.pde.core.prefs | 3 + .../META-INF/spring/backup.xml | 29 +++ .../META-INF/spring/osgi.xml | 16 ++ lib/org.argeo.slc.lib.jcr/build.properties | 1 + lib/org.argeo.slc.lib.jcr/pom.xml | 12 ++ lib/org.argeo.slc.lib.repo/pom.xml | 7 +- lib/pom.xml | 1 + .../META-INF/spring/common.xml | 4 + .../META-INF/spring/osgi.xml | 4 +- runtime/org.argeo.slc.repo/pom.xml | 4 +- .../java/org/argeo/slc/repo/RepoSync.java | 9 + runtime/org.argeo.slc.support.simple/pom.xml | 5 + .../slc/lib/jcr/JcrRepositoryBackup.java | 170 ++++++++++++++++++ 15 files changed, 296 insertions(+), 6 deletions(-) create mode 100644 lib/org.argeo.slc.lib.jcr/.project create mode 100644 lib/org.argeo.slc.lib.jcr/.settings/org.eclipse.pde.core.prefs create mode 100644 lib/org.argeo.slc.lib.jcr/META-INF/spring/backup.xml create mode 100644 lib/org.argeo.slc.lib.jcr/META-INF/spring/osgi.xml create mode 100644 lib/org.argeo.slc.lib.jcr/build.properties create mode 100644 lib/org.argeo.slc.lib.jcr/pom.xml create mode 100644 runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/jcr/JcrRepositoryBackup.java diff --git a/dep/org.argeo.slc.dep.minimal/pom.xml b/dep/org.argeo.slc.dep.minimal/pom.xml index 2879e53b4..680839f93 100644 --- a/dep/org.argeo.slc.dep.minimal/pom.xml +++ b/dep/org.argeo.slc.dep.minimal/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.slc @@ -65,6 +66,18 @@ ${version.argeo-commons} + + + org.argeo.tp + org.apache.commons.vfs + + + org.argeo.tp + org.apache.xmlcommons + + + + org.argeo.tp diff --git a/lib/org.argeo.slc.lib.jcr/.project b/lib/org.argeo.slc.lib.jcr/.project new file mode 100644 index 000000000..8162314cd --- /dev/null +++ b/lib/org.argeo.slc.lib.jcr/.project @@ -0,0 +1,22 @@ + + + org.argeo.slc.lib.jcr + + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + + diff --git a/lib/org.argeo.slc.lib.jcr/.settings/org.eclipse.pde.core.prefs b/lib/org.argeo.slc.lib.jcr/.settings/org.eclipse.pde.core.prefs new file mode 100644 index 000000000..f29e940a0 --- /dev/null +++ b/lib/org.argeo.slc.lib.jcr/.settings/org.eclipse.pde.core.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +pluginProject.extensions=false +resolve.requirebundle=false diff --git a/lib/org.argeo.slc.lib.jcr/META-INF/spring/backup.xml b/lib/org.argeo.slc.lib.jcr/META-INF/spring/backup.xml new file mode 100644 index 000000000..d4186fd2c --- /dev/null +++ b/lib/org.argeo.slc.lib.jcr/META-INF/spring/backup.xml @@ -0,0 +1,29 @@ + + + + + Backups a JCR repository as XML + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/org.argeo.slc.lib.jcr/META-INF/spring/osgi.xml b/lib/org.argeo.slc.lib.jcr/META-INF/spring/osgi.xml new file mode 100644 index 000000000..23ffca89c --- /dev/null +++ b/lib/org.argeo.slc.lib.jcr/META-INF/spring/osgi.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/lib/org.argeo.slc.lib.jcr/build.properties b/lib/org.argeo.slc.lib.jcr/build.properties new file mode 100644 index 000000000..5f22cdd44 --- /dev/null +++ b/lib/org.argeo.slc.lib.jcr/build.properties @@ -0,0 +1 @@ +bin.includes = META-INF/ diff --git a/lib/org.argeo.slc.lib.jcr/pom.xml b/lib/org.argeo.slc.lib.jcr/pom.xml new file mode 100644 index 000000000..4e7b17cd0 --- /dev/null +++ b/lib/org.argeo.slc.lib.jcr/pom.xml @@ -0,0 +1,12 @@ + + 4.0.0 + + org.argeo.slc + lib + 1.1.12-SNAPSHOT + .. + + org.argeo.slc.lib.repo + SLC Lib - Repository Utilities + Utilities to transfer, convert, manage software repositories + diff --git a/lib/org.argeo.slc.lib.repo/pom.xml b/lib/org.argeo.slc.lib.repo/pom.xml index 4e7b17cd0..0a974be10 100644 --- a/lib/org.argeo.slc.lib.repo/pom.xml +++ b/lib/org.argeo.slc.lib.repo/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.slc @@ -6,7 +7,7 @@ 1.1.12-SNAPSHOT .. - org.argeo.slc.lib.repo + org.argeo.slc.lib.jcr SLC Lib - Repository Utilities - Utilities to transfer, convert, manage software repositories + Utilities related to JCR repositories diff --git a/lib/pom.xml b/lib/pom.xml index 574012e0d..77f1cf6c8 100644 --- a/lib/pom.xml +++ b/lib/pom.xml @@ -11,6 +11,7 @@ SLC Standard Libs SLC execution modules for generic tasks, to be used as parent pom + org.argeo.slc.lib.jcr org.argeo.slc.lib.repo diff --git a/modules/org.argeo.slc.agent/META-INF/spring/common.xml b/modules/org.argeo.slc.agent/META-INF/spring/common.xml index 2a3f36cfb..8345118f8 100644 --- a/modules/org.argeo.slc.agent/META-INF/spring/common.xml +++ b/modules/org.argeo.slc.agent/META-INF/spring/common.xml @@ -11,4 +11,8 @@ osgibundle:agent.properties + + + \ No newline at end of file diff --git a/modules/org.argeo.slc.agent/META-INF/spring/osgi.xml b/modules/org.argeo.slc.agent/META-INF/spring/osgi.xml index 027f184d9..3858f9b31 100644 --- a/modules/org.argeo.slc.agent/META-INF/spring/osgi.xml +++ b/modules/org.argeo.slc.agent/META-INF/spring/osgi.xml @@ -6,7 +6,7 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> - + + + \ No newline at end of file diff --git a/runtime/org.argeo.slc.repo/pom.xml b/runtime/org.argeo.slc.repo/pom.xml index dd34ce07b..223810e13 100644 --- a/runtime/org.argeo.slc.repo/pom.xml +++ b/runtime/org.argeo.slc.repo/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 org.argeo.slc @@ -22,6 +23,7 @@ org.w3c.dom.*;version="0.0.0", + org.xml.sax.*;version="0.0.0", javax.xml.transform.*;version="0.0.0", javax.xml.parsers.*;version="0.0.0", javax.jcr.nodetype, diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoSync.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoSync.java index bdde722de..3bcea1228 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoSync.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/RepoSync.java @@ -279,4 +279,13 @@ public class RepoSync implements Runnable { public void setSourcePassword(char[] sourcePassword) { this.sourcePassword = sourcePassword; } + + public void setTargetUsername(String targetUsername) { + this.targetUsername = targetUsername; + } + + public void setTargetPassword(char[] targetPassword) { + this.targetPassword = targetPassword; + } + } diff --git a/runtime/org.argeo.slc.support.simple/pom.xml b/runtime/org.argeo.slc.support.simple/pom.xml index 8d9f3c69b..bdbd58956 100644 --- a/runtime/org.argeo.slc.support.simple/pom.xml +++ b/runtime/org.argeo.slc.support.simple/pom.xml @@ -40,6 +40,11 @@ org.argeo.slc.core 1.1.12-SNAPSHOT + + org.argeo.commons.server + org.argeo.server.jcr + ${version.argeo-commons} + diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/jcr/JcrRepositoryBackup.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/jcr/JcrRepositoryBackup.java new file mode 100644 index 000000000..a6b7d28ca --- /dev/null +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/jcr/JcrRepositoryBackup.java @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.argeo.slc.lib.jcr; + +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; + +import javax.jcr.Credentials; +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.Repository; +import javax.jcr.RepositoryFactory; +import javax.jcr.Session; +import javax.jcr.SimpleCredentials; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.vfs.FileObject; +import org.apache.commons.vfs.FileSystemException; +import org.apache.commons.vfs.FileSystemManager; +import org.argeo.jcr.ArgeoJcrUtils; +import org.argeo.jcr.JcrUtils; +import org.argeo.slc.SlcException; + +/** Backups a JCR repository */ +public class JcrRepositoryBackup implements Runnable { + private final static Log log = LogFactory.getLog(JcrRepositoryBackup.class); + + private String sourceRepo; + private String targetFile; + + private String sourceWksp; + + private String sourceUsername; + private char[] sourcePassword; + + private RepositoryFactory repositoryFactory; + private FileSystemManager fileSystemManager; + + public void run() { + FileObject archiveFo = null; + Session sourceDefaultSession = null; + try { + long begin = System.currentTimeMillis(); + + Repository sourceRepository = ArgeoJcrUtils.getRepositoryByUri( + repositoryFactory, sourceRepo); + Credentials sourceCredentials = null; + if (sourceUsername != null) + sourceCredentials = new SimpleCredentials(sourceUsername, + sourcePassword); + + archiveFo = fileSystemManager.resolveFile(targetFile); + FileObject archiveRoot = fileSystemManager + .createFileSystem(archiveFo); + + Map errors = new HashMap(); + sourceDefaultSession = sourceRepository.login(sourceCredentials); + for (String sourceWorkspaceName : sourceDefaultSession + .getWorkspace().getAccessibleWorkspaceNames()) { + if (sourceWksp != null && !sourceWksp.trim().equals("") + && !sourceWorkspaceName.equals(sourceWksp)) + continue; + // if (sourceWorkspaceName.equals("security")) + // continue; + // if (sourceWorkspaceName.equals("localrepo")) + // continue; + Session sourceSession = null; + OutputStream out = null; + try { + FileObject workspaceXml = archiveRoot + .getChild(sourceWorkspaceName + ".xml"); + out = workspaceXml.getContent().getOutputStream(); + sourceSession = sourceRepository.login(sourceCredentials, + sourceWorkspaceName); + backupWorkspace(sourceSession, out); + workspaceXml.close(); + } catch (Exception e) { + errors.put("Could not sync workspace " + + sourceWorkspaceName, e); + } finally { + JcrUtils.logoutQuietly(sourceSession); + IOUtils.closeQuietly(out); + } + } + + long duration = (System.currentTimeMillis() - begin) / 1000;// s + log.info("Backed-up " + sourceRepo + " in " + (duration / 60) + + "min " + (duration % 60) + "s"); + + if (errors.size() > 0) { + throw new SlcException("Sync failed " + errors); + } + } catch (Exception e) { + throw new SlcException("Cannot backup " + sourceRepo, e); + } finally { + JcrUtils.logoutQuietly(sourceDefaultSession); + if (archiveFo != null) + try { + archiveFo.close(); + } catch (FileSystemException e) { + // silent + } + } + } + + protected void backupWorkspace(Session sourceSession, OutputStream out) { + try { + if (log.isDebugEnabled()) + log.debug("Syncing " + sourceSession.getWorkspace().getName() + + "..."); + for (NodeIterator it = sourceSession.getRootNode().getNodes(); it + .hasNext();) { + Node node = it.nextNode(); + if (node.getName().equals("jcr:system")) + continue; + + sourceSession + .exportSystemView(node.getPath(), out, true, false); + if (log.isDebugEnabled()) + log.debug(" " + node.getPath()); + } + if (log.isDebugEnabled()) + log.debug("Synced " + sourceSession.getWorkspace().getName()); + } catch (Exception e) { + throw new SlcException("Cannot backup " + + sourceSession.getWorkspace().getName(), e); + } + } + + public void setSourceRepo(String sourceRepo) { + this.sourceRepo = sourceRepo; + } + + public void setSourceWksp(String sourceWksp) { + this.sourceWksp = sourceWksp; + } + + public void setRepositoryFactory(RepositoryFactory repositoryFactory) { + this.repositoryFactory = repositoryFactory; + } + + public void setSourceUsername(String sourceUsername) { + this.sourceUsername = sourceUsername; + } + + public void setSourcePassword(char[] sourcePassword) { + this.sourcePassword = sourcePassword; + } + + public void setFileSystemManager(FileSystemManager fileSystemManager) { + this.fileSystemManager = fileSystemManager; + } + +} -- 2.30.2