[opencms-dev] proposing a parameterized scheduled job backup

Marko Riedel markoriedelde at yahoo.de
Thu Jun 2 02:17:05 CEST 2011


Hi folks,

I have continued to tinker with my Java class for scheduled job backups. It is now fully parameterized. The resources that are to be backed up go into the parameters "resourceNNN" where NNN is a number. The path to and prefix of the zip file go into the parameter "pathAndPrefix."

I hope you find it useful in your own installation. Please send any feedback you may have.

Best regards,

Marko

+---------------------------------------------------+
| Marko Riedel, markoriedelde at yahoo.de              |
| http://www.mathematik.uni-stuttgart.de/~riedelmo/ |
+---------------------------------------------------+


import org.apache.commons.io.FileUtils;

import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;

import org.opencms.jsp.*;
import org.opencms.file.*;
import org.opencms.util.*;
import org.opencms.report.*;
import org.opencms.importexport.*;

import org.opencms.scheduler.I_CmsScheduledJob;

public class ScheduledExportJob implements  I_CmsScheduledJob {
    static final String pathToBackup = "/tmp/opencms-";

    public String launch(CmsObject cms, Map parameters)
	throws Exception {
	ArrayList<String> resources = new ArrayList<String>();

	for(Iterator it = parameters.keySet().iterator(); it.hasNext();){
	    Object param = it.next();
	    if(((String)param).startsWith("resource")){
		resources.add((String)parameters.get(param));
	    }
	}

	Locale locale = cms.getRequestContext().getLocale();
	final String DATE_FORMAT = "yyyy-MM-dd";
	Date today = new Date();

	String pathAndPrefix = (String)parameters.get("pathAndPrefix");
	SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT, locale);
	String backupFile = (pathAndPrefix==null ? pathToBackup : pathAndPrefix)
	    + dFormat.format(today) + ".zip";

	CmsExportParameters params = 
	    new CmsExportParameters(backupFile, // java.lang.String exportFile, 
				    null, // org.dom4j.Element moduleElement, 
				    true, // boolean exportResourceData, 
				    false, // boolean exportUserdata, 
				    false, // boolean exportProjectData, 
				    resources, // java.util.List<java.lang.String> 
				    // resourcesToExport, 
				    false, // boolean includeSystem, 
				    true, // boolean includeUnchanged, 
				    0, // long contentAge, 
				    true, // boolean recursive, 
				    false); //boolean inProject

	CmsStringBufferReport report = new CmsStringBufferReport(locale);

	try {
	    CmsExport export = new CmsExport(cms, report);
	    export.exportData(params);
	    report.print("backup job to " + backupFile + " " + resources.toString(), 
			 I_CmsReport.FORMAT_OK);
	}
	catch (Exception ex){
	    report.println(ex);
	}

	return report.toString();
    }
}




More information about the opencms-dev mailing list