Improve logical backups.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / CmsDeployment.java
index a19bbaf353daf78a3f375c1bb7004a59c552cdd6..2667d986a93efc8c737a909ed0c1941c0e8f80af 100644 (file)
@@ -11,6 +11,7 @@ import java.lang.management.ManagementFactory;
 import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -47,6 +48,7 @@ import org.argeo.cms.internal.http.HttpUtils;
 import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.maintenance.backup.LogicalRestore;
 import org.argeo.naming.LdapAttrs;
 import org.argeo.osgi.useradmin.UserAdminConf;
 import org.argeo.util.LangUtils;
@@ -292,6 +294,18 @@ public class CmsDeployment implements NodeDeployment {
                // home
                prepareDataModel(NodeConstants.NODE_REPOSITORY, deployedNodeRepository, publishAsLocalRepo);
 
+               // init from backup
+               if (deployConfig.isFirstInit()) {
+                       Path restorePath = Paths.get(System.getProperty("user.dir"), "restore");
+                       if (Files.exists(restorePath)) {
+                               if (log.isDebugEnabled())
+                                       log.debug("Found backup " + restorePath + ", restoring it...");
+                               LogicalRestore logicalRestore = new LogicalRestore(bc, deployedNodeRepository, restorePath);
+                               KernelUtils.doAsDataAdmin(logicalRestore);
+                               log.info("Restored backup from " + restorePath);
+                       }
+               }
+
                // init from repository
                Collection<ServiceReference<Repository>> initRepositorySr;
                try {
@@ -320,6 +334,9 @@ public class CmsDeployment implements NodeDeployment {
                        workspaces: for (String workspaceName : initSession.getWorkspace().getAccessibleWorkspaceNames()) {
                                if ("security".equals(workspaceName))
                                        continue workspaces;
+                               if (log.isDebugEnabled())
+                                       log.debug("Copying workspace " + workspaceName + " from init repository...");
+                               long begin = System.currentTimeMillis();
                                Session targetSession = null;
                                Session sourceSession = null;
                                try {
@@ -335,8 +352,16 @@ public class CmsDeployment implements NodeDeployment {
                                                targetSession = NodeUtils.openDataAdminSession(deployedNodeRepository, workspaceName);
                                        }
                                        sourceSession = initRepository.login(workspaceName);
+//                                     JcrUtils.copyWorkspaceXml(sourceSession, targetSession);
+                                       // TODO deal with referenceable nodes
                                        JcrUtils.copy(sourceSession.getRootNode(), targetSession.getRootNode());
                                        targetSession.save();
+                                       long duration = System.currentTimeMillis() - begin;
+                                       if (log.isDebugEnabled())
+                                               log.debug("Copied workspace " + workspaceName + " from init repository in " + (duration / 1000)
+                                                               + " s");
+                               } catch (Exception e) {
+                                       log.error("Cannot copy workspace " + workspaceName + " from init repository.", e);
                                } finally {
                                        Jcr.logout(sourceSession);
                                        Jcr.logout(targetSession);
@@ -570,6 +595,7 @@ public class CmsDeployment implements NodeDeployment {
                        if (cn != null) {
                                List<String> publishAsLocalRepo = new ArrayList<>();
                                if (cn.equals(NodeConstants.NODE_REPOSITORY)) {
+//                                     JackrabbitDataModelMigration.clearRepositoryCaches(repoContext.getRepositoryConfig());
                                        prepareNodeRepository(repoContext.getRepository(), publishAsLocalRepo);
                                        // TODO separate home repository
                                        prepareHomeRepository(repoContext.getRepository());