How to automate the site export?

  Written by The Jahia Team
 
Developers
Sysadmins
   Estimated reading time:
7.1.2.1
How to automate the site export feature?

Answer

To automate the export of your sites, you could make directly an HTTP POST on the export servlet.

The URL would be : http://localhost:8080/cms/export/default/YOUR_EXPORT_NAME.zip

The needed parameters are :

exportformat=site
live=true (or false if you want to export only the staging content)
sitebox=<the_site_key>

Here is an example iofa Jahia session:
http://demo7.jahia.com/cms/export/default/my-export.zip?exportformat=site&sitebox=acme

In the above case, the request must have open a valid user session and the user needs the rights to export the site.

But you can do it also with a login like (where you specify the user/password as a parameter):

curl -o {export_storage_path} -u username:password 'http://demo7.jahia.com/cms/export/default/my-export.zip?exportformat=site&sitebox=acme'

You can also schedule a Job to do this task. The job can be triggered on different ways for instance it can be a Quartz cron trigger. For that we suggest to put the job into a java class ( check the source code of AutoExportSiteJob.java below). So when you install this class you can add a trigger (like quartz):

When the job class is installed on your server you can simple add jobs in quartz listener, Inside this job you can configure the site, the export path and the time when the job should run. For instance daily at 2:00 am and ACMESPACE:

org.quartz.JobDetail detail = org.jahia.services.scheduler.BackgroundJob.createJahiaJob("Export site ACME-SPACE", org.jahia.job.AutoExportSiteJob.class);
org.quartz.JobDataMap dataMap = detail.getJobDataMap();
dataMap.put(org.lombard.job.AutoExportSiteJob.EXPORT_SITE_KEY, "ACMESPACE");
dataMap.put(org.lombard.job.AutoExportSiteJob.EXPORT_PATH, "C:\\Jahia\\acme.zip");
detail.setJobDataMap(dataMap);
org.quartz.CronTrigger trigger = new org.quartz.CronTrigger("siteExportACMESPACETrigger", "Maintanace", "0 00 02 * * ? *");
org.jahia.registries.ServicesRegistry.getInstance().getSchedulerService().getScheduler().scheduleJob(detail, trigger);

For sure instead this trigger and the class you can put everything in a groovy script, but you have to execute the groovy script into a Jahia context, means you can use for instance the grGroovyonsole in the jahia tools.

AutoExportSiteJob.java:

package org.jahia.job;

import java.util.ArrayList;
import java.util.List;

import org.jahia.registries.ServicesRegistry;
import org.jahia.services.content.JCRSessionFactory;
import org.jahia.services.content.JCRSessionWrapper;
import org.jahia.services.content.decorator.JCRSiteNode;
import org.jahia.services.scheduler.BackgroundJob;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Background task which starts an export for a virtuell site
 *
 * @author Werner Assek
 */
public class AutoExportSiteJob extends BackgroundJob {

    private static final Logger logger = LoggerFactory.getLogger(AutoExportSiteJob.class);

    public static final String EXPORT_PATH = "exportPath";
    public static final String EXPORT_SITE_KEY = "exportSite";

    @Override
    public void executeJahiaJob(JobExecutionContext ctx) throws Exception {
        JobDataMap jobDataMap = ctx.getJobDetail().getJobDataMap();

        String exportPath = jobDataMap.getString(EXPORT_PATH);
        if(exportPath == null) {
            logger.error("No export path in jobDataMap");
            return;
        }
        String siteToExport = jobDataMap.getString(EXPORT_SITE_KEY);
        if(siteToExport == null) {
            logger.error("No site to export configured in jobDataMap");
            return;
        }

        try{
            java.io.File file = new java.io.File(exportPath);
            if (!file.exists()) {
                file.createNewFile();
            }
            java.io.OutputStream outputStream = new java.io.FileOutputStream(file);
            java.util.Map<String, Object> params = new java.util.HashMap<String, Object>();
            params.put(org.jahia.services.importexport.ImportExportService.VIEW_ACL, Boolean.TRUE);
            params.put(org.jahia.services.importexport.ImportExportService.VIEW_CONTENT, Boolean.TRUE);
            params.put(org.jahia.services.importexport.ImportExportService.VIEW_JAHIALINKS, Boolean.TRUE);
            params.put(org.jahia.services.importexport.ImportExportService.VIEW_METADATA, Boolean.TRUE);
            params.put(org.jahia.services.importexport.ImportExportService.VIEW_VERSION, Boolean.FALSE);
            params.put(org.jahia.services.importexport.ImportExportService.INCLUDE_LIVE_EXPORT, Boolean.TRUE);
            params.put(org.jahia.services.importexport.ImportExportService.INCLUDE_USERS, Boolean.TRUE);
            params.put(org.jahia.services.importexport.ImportExportService.VIEW_WORKFLOW, Boolean.TRUE);

            List<JCRSiteNode> sites = new ArrayList<JCRSiteNode>();
            JCRSessionFactory.getInstance().setCurrentUser(ServicesRegistry.getInstance().getJahiaUserManagerService().lookupRootUser().getJahiaUser());
            JCRSessionWrapper defsession = JCRSessionFactory.getInstance().getCurrentSession("default", Locale.ENGLISH, null, false);
            JCRSiteNode siteByKey = ServicesRegistry.getInstance().getJahiaSitesService().getSiteByKey(siteToExport, defsession);
            sites.add(siteByKey);
            ServicesRegistry.getInstance().getImportExportService().exportSites(outputStream, params, sites);
        }catch (Exception ex) {
            logger.error("Error in job: ", ex);
        }
    }
}