cron export

How to automate the site export?


How to automate the site export feature?


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/

The needed parameters are :

live=true (or false if you want to export only the staging content)

Here is an example iofa Jahia session:

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 ''

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 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 ="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\\");
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.

package org.jahia.job;

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

import org.jahia.registries.ServicesRegistry;
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";

    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");
        String siteToExport = jobDataMap.getString(EXPORT_SITE_KEY);
        if(siteToExport == null) {
            logger.error("No site to export configured in jobDataMap");

   file = new;
            if (!file.exists()) {
   outputStream = new;
            java.util.Map<String, Object> params = new java.util.HashMap<String, Object>();
            params.put(, Boolean.TRUE);
            params.put(, Boolean.TRUE);
            params.put(, Boolean.TRUE);
            params.put(, Boolean.TRUE);
            params.put(, Boolean.FALSE);
            params.put(, Boolean.TRUE);
            params.put(, Boolean.TRUE);
            params.put(, Boolean.TRUE);

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