How to export packages automatically
Learn how to set up a workflow to automatically export packages.

Introduction

Setting up a workflow to automatically export packages on a regular basis (every day, every week for example) can be useful as a back-up procedure. It provides a backup of all parameters in case the database crashes or something is deleted accidentally. 

Implementation

Load package specifications

The xtk:spec file contains the specifications. Use a queryDef to load them into JavaScript.

Package specifications loading:  

var query = xtk.queryDef.create(
 <queryDef schema="xtk:specFile" operation="select">
 <select>
 <node expr="@name"/>
 <node expr="@namespace"/>
 <node expr="data"/>
 </select>
 </queryDef>
 )
var res = query.ExecuteQuery()

Create package specification

You can also specify the package with an XML document. By default, up to 1000 documents are exported.

To export more documents, set the lineCountMax attribute.

Package specification XML:

<package>
 <definition id="1" lineCountMax="10000" schema="nms:delivery">
 <where>
 <condition boolOperator="OR" expr="[campaign/@fullName] LIKE '/Campaign
Management/%'"/>
 </where>
 </definition>
</package>

Generate package

The GenerateDoc() function generates the packages from a specification. It returns an XML object. 

Packages export JS:

vars specFileXML = <specFile (...) />
var specFile = xtk.specFile.create(specfileXml)
var package = specFile.GenerateDoc()

Save package into an XML file

The package XML needs to be saved to a file and the file name must have the header and the date. The saveXmlFile function writes the contents to a file in XML format.

Save into zip file:

var package = (...) // XML contents of package
var fileName = "/tmp/myPackage.xml"
saveXmlFile(package, fileName)

Save multiple packages into a zip file

If you generate multiple packages, it is more convenient to save all files into a zip file. Files should be located under the "var" subdirectory of the instance and the file name should contain the date.

Save package to zip file:

var zip = new ZipFile(zipfilename)
for each (var s in res.specFile)
{
 var packageName = s.@namespace + ":" + s.@name
 var specFile = xtk.specFile.create(s)
 var package = specFile.GenerateDoc()
 package = "&lt;?xml version='1.0' encoding='windows-1252'?&gt;\n" + package
 var fileName = s.@namespace + s.@name + ".xml"
 var content = new MemoryBuffer();
 content.fromString(package)
 zip.setEntry(fileName, content)
 content.dispose()
}
zip.save("", true)
zip.dispose()

Setup the workflow

To set up the workflow:

  1. Create a new workflow named Daily export of packages under Administration > Production > Technical workflows.
  2. Add a scheduler that runs every day.
  3. Add a JavaScript that exports the packages.
  4. Run this workflow and monitor its execution.
  5. A file will be created every day with the contents of the packages.

Retrieve the files

You can retrieve the files using SFTP. Files are located on the application server. 

Purge the files

Package files must be purged on a regular basis to avoid congesting the file system.