X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.maintenance%2Fsrc%2Forg%2Fargeo%2Fmaintenance%2Fbackup%2FLogicalBackup.java;h=4f7a2cfea561e7806a867a69268b9dcd2921b86f;hb=2606b4b145577c4767c37c464e3f517e49a98100;hp=864de25be9b3fa4483bb9aee8837f15149a0c664;hpb=be5b6f089e1562db7344d70ff019c3f564b308c7;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.maintenance/src/org/argeo/maintenance/backup/LogicalBackup.java b/org.argeo.maintenance/src/org/argeo/maintenance/backup/LogicalBackup.java index 864de25be..4f7a2cfea 100644 --- a/org.argeo.maintenance/src/org/argeo/maintenance/backup/LogicalBackup.java +++ b/org.argeo.maintenance/src/org/argeo/maintenance/backup/LogicalBackup.java @@ -17,6 +17,13 @@ import java.util.Dictionary; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; import java.util.zip.ZipEntry; @@ -25,6 +32,7 @@ import java.util.zip.ZipOutputStream; import javax.jcr.Binary; import javax.jcr.Node; +import javax.jcr.NodeIterator; import javax.jcr.Property; import javax.jcr.Repository; import javax.jcr.RepositoryException; @@ -37,6 +45,7 @@ import org.apache.commons.logging.LogFactory; import org.argeo.api.NodeConstants; import org.argeo.api.NodeUtils; import org.argeo.jackrabbit.client.ClientDavexRepositoryFactory; +import org.argeo.jcr.Jcr; import org.argeo.jcr.JcrException; import org.argeo.jcr.JcrUtils; import org.osgi.framework.Bundle; @@ -53,26 +62,25 @@ public class LogicalBackup implements Runnable { private final static Log log = LogFactory.getLog(LogicalBackup.class); public final static String WORKSPACES_BASE = "workspaces/"; + public final static String FILES_BASE = "files/"; public final static String OSGI_BASE = "share/osgi/"; private final Repository repository; + private String defaultWorkspace; private final BundleContext bundleContext; private final ZipOutputStream zout; private final Path basePath; + private ExecutorService executorService; + public LogicalBackup(BundleContext bundleContext, Repository repository, Path basePath) { this.repository = repository; this.zout = null; this.basePath = basePath; this.bundleContext = bundleContext; - } -// public LogicalBackup(BundleContext bundleContext, Repository repository, ZipOutputStream zout) { -// this.repository = repository; -// this.zout = zout; -// this.basePath = null; -// this.bundleContext = bundleContext; -//} + executorService = Executors.newFixedThreadPool(3); + } @Override public void run() { @@ -80,143 +88,122 @@ public class LogicalBackup implements Runnable { log.info("Start logical backup to " + basePath); perform(); } catch (Exception e) { - e.printStackTrace(); + log.error("Unexpected exception when performing logical backup", e); throw new IllegalStateException("Logical backup failed", e); } } public void perform() throws RepositoryException, IOException { + long begin = System.currentTimeMillis(); // software backup if (bundleContext != null) - performSoftwareBackup(); + executorService.submit(() -> performSoftwareBackup(bundleContext)); // data backup Session defaultSession = login(null); + defaultWorkspace = defaultSession.getWorkspace().getName(); try { String[] workspaceNames = defaultSession.getWorkspace().getAccessibleWorkspaceNames(); workspaces: for (String workspaceName : workspaceNames) { if ("security".equals(workspaceName)) continue workspaces; - perform(workspaceName); + performDataBackup(workspaceName); } } finally { JcrUtils.logoutQuietly(defaultSession); + executorService.shutdown(); + try { + executorService.awaitTermination(24, TimeUnit.HOURS); + } catch (InterruptedException e) { + // silent + } } - + long duration = System.currentTimeMillis() - begin; + log.info("System logical backup completed in " + (duration / 60000) + "min " + (duration / 1000) + "s"); } - public void performSoftwareBackup() throws IOException { - for (Bundle bundle : bundleContext.getBundles()) { - String relativePath = OSGI_BASE + "boot/" + bundle.getSymbolicName() + ".jar"; - Dictionary headers = bundle.getHeaders(); - Manifest manifest = new Manifest(); - Enumeration headerKeys = headers.keys(); - while (headerKeys.hasMoreElements()) { - String headerKey = headerKeys.nextElement(); - String headerValue = headers.get(headerKey); - manifest.getMainAttributes().putValue(headerKey, headerValue); - } - try (JarOutputStream jarOut = new JarOutputStream(openOutputStream(relativePath), manifest)) { -// Enumeration entryPaths = bundle.getEntryPaths("/"); -// while (entryPaths.hasMoreElements()) { -// String entryPath = entryPaths.nextElement(); -// ZipEntry entry = new ZipEntry(entryPath); -// URL entryUrl = bundle.getEntry(entryPath); -// try (InputStream in = entryUrl.openStream()) { -// jarOut.putNextEntry(entry); -// IOUtils.copy(in, jarOut); -// jarOut.closeEntry(); -// } catch (FileNotFoundException e) { -// log.warn(entryPath); -// } -// } - Enumeration resourcePaths = bundle.findEntries("/", "*", true); - resources: while (resourcePaths.hasMoreElements()) { - URL entryUrl = resourcePaths.nextElement(); - String entryPath = entryUrl.getPath(); - if (entryPath.equals("")) - continue resources; - if (entryPath.endsWith("/")) - continue resources; - String entryName = entryPath.substring(1);// remove first '/' - if (entryUrl.getPath().equals("/META-INF/")) - continue resources; - if (entryUrl.getPath().equals("/META-INF/MANIFEST.MF")) - continue resources; - // dev - if (entryUrl.getPath().startsWith("/target")) - continue resources; - if (entryUrl.getPath().startsWith("/src")) - continue resources; - if (entryUrl.getPath().startsWith("/ext")) - continue resources; - - if (entryName.startsWith("bin/")) {// dev - entryName = entryName.substring("bin/".length()); - } - - ZipEntry entry = new ZipEntry(entryName); - try (InputStream in = entryUrl.openStream()) { - try { - jarOut.putNextEntry(entry); - } catch (ZipException e) {// duplicate - continue resources; - } - IOUtils.copy(in, jarOut); - jarOut.closeEntry(); -// log.info(entryUrl); - } catch (FileNotFoundException e) { - log.warn(entryUrl + ": " + e.getMessage()); - } - } + protected void performDataBackup(String workspaceName) throws RepositoryException, IOException { + Session session = login(workspaceName); + try { + nodes: for (NodeIterator nit = session.getRootNode().getNodes(); nit.hasNext();) { + Node nodeToExport = nit.nextNode(); + if ("jcr:system".equals(nodeToExport.getName()) && !workspaceName.equals(defaultWorkspace)) + continue nodes; + String nodePath = nodeToExport.getPath(); + Future> contentPathsFuture = executorService + .submit(() -> performNodeBackup(workspaceName, nodePath)); + executorService.submit(() -> performFilesBackup(workspaceName, contentPathsFuture)); } + } finally { + Jcr.logout(session); } - } - public void perform(String workspaceName) throws RepositoryException, IOException { + protected Set performNodeBackup(String workspaceName, String nodePath) { Session session = login(workspaceName); try { - String relativePath = WORKSPACES_BASE + workspaceName + ".xml"; + Node nodeToExport = session.getNode(nodePath); + String nodeName = nodeToExport.getName(); +// if (nodeName.startsWith("jcr:") || nodeName.startsWith("rep:")) +// continue nodes; +// // TODO make it more robust / configurable +// if (nodeName.equals("user")) +// continue nodes; + String relativePath = WORKSPACES_BASE + workspaceName + "/" + nodeName + ".xml"; OutputStream xmlOut = openOutputStream(relativePath); BackupContentHandler contentHandler; try (Writer writer = new BufferedWriter(new OutputStreamWriter(xmlOut, StandardCharsets.UTF_8))) { contentHandler = new BackupContentHandler(writer, session); - try { - session.exportSystemView("/", contentHandler, true, false); - if (log.isDebugEnabled()) - log.debug("Workspace " + workspaceName + ": metadata exported to " + relativePath); - } catch (SAXException e) { - throw new RuntimeException("Cannot perform backup of workspace " + workspaceName, e); - } catch (RepositoryException e) { - throw new JcrException("Cannot perform backup of workspace " + workspaceName, e); - } + session.exportSystemView(nodeToExport.getPath(), contentHandler, true, false); + if (log.isDebugEnabled()) + log.debug(workspaceName + ":/" + nodeName + " metadata exported to " + relativePath); } - for (String path : contentHandler.getContentPaths()) { + + // Files + Set contentPaths = contentHandler.getContentPaths(); + return contentPaths; + } catch (IOException | SAXException e) { + throw new RuntimeException("Cannot backup node " + workspaceName + ":" + nodePath, e); + } catch (RepositoryException e) { + throw new JcrException("Cannot backup node " + workspaceName + ":" + nodePath, e); + } finally { + Jcr.logout(session); + } + } + + protected void performFilesBackup(String workspaceName, Future> contentPathsFuture) { + Set contentPaths; + try { + contentPaths = contentPathsFuture.get(24, TimeUnit.HOURS); + } catch (InterruptedException | ExecutionException | TimeoutException e1) { + throw new RuntimeException("Cannot retrieve content paths for workspace " + workspaceName); + } + if (contentPaths == null || contentPaths.size() == 0) + return; + Session session = login(workspaceName); + try { + String workspacesFilesBasePath = FILES_BASE + workspaceName; + for (String path : contentPaths) { Node contentNode = session.getNode(path); Binary binary = contentNode.getProperty(Property.JCR_DATA).getBinary(); - String fileRelativePath = WORKSPACES_BASE + workspaceName + contentNode.getParent().getPath(); + String fileRelativePath = workspacesFilesBasePath + contentNode.getParent().getPath(); try (InputStream in = binary.getStream(); OutputStream out = openOutputStream(fileRelativePath)) { IOUtils.copy(in, out); if (log.isTraceEnabled()) log.trace("Workspace " + workspaceName + ": file content exported to " + fileRelativePath); } finally { - + JcrUtils.closeQuietly(binary); } - } - -// OutputStream xmlOut = openOutputStream(relativePath); -// try { -// session.exportSystemView("/", xmlOut, false, false); -// } finally { -// closeOutputStream(relativePath, xmlOut); -// } - - // TODO scan all binaries + if (log.isDebugEnabled()) + log.debug(workspaceName + ":" + contentPaths.size() + " files exported to " + workspacesFilesBasePath); + } catch (RepositoryException e) { + throw new JcrException("Cannot backup files from " + workspaceName + ":", e); + } catch (IOException e) { + throw new RuntimeException("Cannot backup files from " + workspaceName + ":", e); } finally { - JcrUtils.logoutQuietly(session); + Jcr.logout(session); } } @@ -286,4 +273,66 @@ public class LogicalBackup implements Runnable { return repositoryFactory.getRepository(params); } + public void performSoftwareBackup(BundleContext bundleContext) { + String bootBasePath = OSGI_BASE + "boot"; + Bundle[] bundles = bundleContext.getBundles(); + for (Bundle bundle : bundles) { + String relativePath = bootBasePath + "/" + bundle.getSymbolicName() + ".jar"; + Dictionary headers = bundle.getHeaders(); + Manifest manifest = new Manifest(); + Enumeration headerKeys = headers.keys(); + while (headerKeys.hasMoreElements()) { + String headerKey = headerKeys.nextElement(); + String headerValue = headers.get(headerKey); + manifest.getMainAttributes().putValue(headerKey, headerValue); + } + try (JarOutputStream jarOut = new JarOutputStream(openOutputStream(relativePath), manifest)) { + Enumeration resourcePaths = bundle.findEntries("/", "*", true); + resources: while (resourcePaths.hasMoreElements()) { + URL entryUrl = resourcePaths.nextElement(); + String entryPath = entryUrl.getPath(); + if (entryPath.equals("")) + continue resources; + if (entryPath.endsWith("/")) + continue resources; + String entryName = entryPath.substring(1);// remove first '/' + if (entryUrl.getPath().equals("/META-INF/")) + continue resources; + if (entryUrl.getPath().equals("/META-INF/MANIFEST.MF")) + continue resources; + // dev + if (entryUrl.getPath().startsWith("/target")) + continue resources; + if (entryUrl.getPath().startsWith("/src")) + continue resources; + if (entryUrl.getPath().startsWith("/ext")) + continue resources; + + if (entryName.startsWith("bin/")) {// dev + entryName = entryName.substring("bin/".length()); + } + + ZipEntry entry = new ZipEntry(entryName); + try (InputStream in = entryUrl.openStream()) { + try { + jarOut.putNextEntry(entry); + } catch (ZipException e) {// duplicate + continue resources; + } + IOUtils.copy(in, jarOut); + jarOut.closeEntry(); +// log.info(entryUrl); + } catch (FileNotFoundException e) { + log.warn(entryUrl + ": " + e.getMessage()); + } + } + } catch (IOException e1) { + throw new RuntimeException("Cannot export bundle " + bundle, e1); + } + } + if (log.isDebugEnabled()) + log.debug(bundles.length + " OSGi bundles exported to " + bootBasePath); + + } + }